bcc multiple addresses with swiftmailer
Asked Answered
E

2

5

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?

Endocrinotherapy answered 2/10, 2013 at 12:55 Comment(5)
Do you have any joy using AddBcc instead?Straus
Try some pnrbuilder.com address as the second email. Maybe it's because of server configuration.Sightless
@Straus nope addBcc() gives the same results (added to my question above)Endocrinotherapy
@Sightless using an email address with a different domain does work and both are sent, anything I can do to get it to work when the emails are the same domain?Endocrinotherapy
For everyone else like me: double check to not having typed addBcc() instead of setBcc(). (You have to at least setup the first one by setBcc before you can use addBcc).Eulalie
E
1

This ended up being an issue on the server side, I contacted my hosting provider (GoDaddy) who were able to make some changes on their end fixing the problem. Thank you to all those who tried to help!

Endocrinotherapy answered 5/10, 2013 at 5:36 Comment(0)
H
9

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.

Horsemint answered 2/10, 2013 at 16:47 Comment(1)
Thanks . Two parenthesis at the end, there must be three.Ketty
E
1

This ended up being an issue on the server side, I contacted my hosting provider (GoDaddy) who were able to make some changes on their end fixing the problem. Thank you to all those who tried to help!

Endocrinotherapy answered 5/10, 2013 at 5:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.