bcc multiple addresses with swiftmailer

Wesley Smith picture Wesley Smith · Oct 2, 2013 · Viewed 18k times · Source

Im using the below php code to send an email to one address and bcc 2 other addresses. It sends to the recipient fine but I can only get it to send to one of the 2 bcc addresses. (see comments in code for what ive tried)

Oddly enough though, $result comes back as 3 so it seems that its trying to send the second bcc email but it never comes through.

<?php


    $tracker='[email protected]';
    $subject = $_POST['subject'];
    $sender = $_POST['sender'];
    $toEmail=$_POST['toEmail'];
    $passedInEmail=stripslashes($_POST['message']);
    $passedInEmail=preg_replace('/&nbsp;/',' ',$passedInEmail);

    require_once('swiftLib/simple_html_dom.php');
    require_once('swiftLib/swift_required.php');
    $transport = Swift_MailTransport::newInstance();
    $mailer = Swift_Mailer::newInstance($transport);
    // Create the message
    $message = Swift_Message::newInstance();
    //turn the meesage into an object using simple_html_dom
    //so we can iterate through and embed each image
    $content = str_get_html($passedInEmail);

    // Retrieve all img src tags and replace them with embedded images
    foreach($content->find('img') as $e) 
        {
            if($e->src != "") 
                {
                    $value = $e->src;
                    $newValue = $message->embed(Swift_Image::fromPath($value)); 
                    $e->src = $newValue;
                }
        }

    $message->setSubject($subject);
    $message->setFrom($sender);
    $message->setTo($toEmail);



    //this is my problem
    $message->setBcc(array('[email protected]',$sender));
    //as it is above only "sender" gets the email

    //if I change it like this:

    //$message->setBcc($tracker,$sender);
    //only "tracker" gets the email


    //same if I change it like this:
    //$message->setBcc($sender);
//$message->addBcc($tracker);

    $message->setReplyTo(array('[email protected]'));
    $message->setBody($content,'text/html');


    $result = $mailer->send($message);
    if ($result=3) {
        echo 'Email Sent!';
    } 
    else {
       echo 'Error!';
    }
?>

What is the proper way to do this?

Answer

Michael Thompson picture Michael Thompson · Oct 2, 2013

You can find the swiftmailer tutorial here

example:

$message->setBcc(array(array('[email protected]' => 'The Name'),array('[email protected]' => 'Another Name')));

Try setting the names for the email addresses and see if it makes any difference.