Question

I have the following code to send email using symfony2 swift_mailer

$message = \Swift_Message::newInstance()
                ->setSubject('tets')
                ->setFrom(array('request@reqa.com' => 'Test Mail'))
                ->addTo('test@test.com')
                ->addCc('test@te.com')
                ->addCc('test1@test1.com')
                ->addCc('test2@test2.com')
                ->setBody($html, 'text/html');
        $this->get('mailer')->send($message);

My problem is i am loading addCc email addresses from database, Therefore is there any method to addCC email addresses by seperating them by , like this ->addCc('test2@test2.com,test4@test4.com')

Thanks

Was it helpful?

Solution

Either use setCC directly ( documentation ) ...

$message->setCc(array(
  'person1@example.org',
  'person2@otherdomain.org' => 'Person 2 Name',
  'person3@example.org',
  'person4@example.org',
  'person5@example.org' => 'Person 5 Name'
));

... or loop over your collection of entites and execute addCC for each of them.

$receipients = $this->getDoctrine()->getRepository("...")->findBy(array(".." => ".."));

foreach ($receipients as $receipient) {
   $message->addCC($receipient->getMail());
}

$this->get('mailer')->send($message);

... or if you only have the ,-separated string with the cc adresses available just explode the string to an array. and afterwards loop over the array elements or pass the array directly to setCC as shown above.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top