4

I am trying to get Batch Sending to work over SMTP, but even though I'm sending to multiple recipients and I've specified user variables (and the variables are getting replaced successfully in the email that is sent), every single recipient shows up in the To: field of the resulting messages at the receiver.

Per MailGun's documentation on Batch Sending...

Warning: It is important when using Batch Sending to also use Recipient Variables. This tells Mailgun to send each recipient an individual email with only their email in the to field. If they are not used, all recipients’ email addresses will show up in the to field for each recipient.

Here is an example of my SMTP headers...

To: foo@example.com, bar@example.com
X-Mailgun-Recipient-Variables: {
    "foo@example.com":
    {
        "id":"12345",
        "email":"foo@example.com",
        "first_name":"Foo"
    },
    "bar@example.com":
    {
        "id":"45678",
        "email":"bar@example.com",
        "first_name":"Bar"
    }
}

The resulting emails should only show one recipient per email in the To field. Am I missing something?

Scruffy Paws
  • 1,219
  • 1
  • 21
  • 27
  • Im curious, did you find a solution to this? – nilsi Aug 15 '16 at 02:03
  • @nilsi, not yet. MailGun claims you have to put `%recipient%` in to the "To" field, but have not been able to get that to work successfully via PHP's `mail()` function since it gets grumpy that its not an email and appends things to it before passing it off to MailGun. – Scruffy Paws Aug 31 '16 at 14:45
  • Any updates? I'm talking to mailgun here: https://wordpress.org/support/topic/sending-multiple-emails-2/#post-8614364 but no luck so far. – ejntaylor Jan 03 '17 at 18:03
  • Unfortunately I had to ditch using SMTP and had to use the API to get our project complete. I would love to know if there is ever a solution for this. – Scruffy Paws Jan 03 '17 at 18:37

3 Answers3

5

I started messing with this yesterday and I think I've found a solution.

The trick is to leave the To: addresses empty and add your recipients to the BCC line. Following that, add a custom header - To: %recipient%. $mail->send() will not complain, and the To: field in the received emails only show the individual recipient's email.

Code Sample:

$mail = new PHPMailer();

$mail->isSMTP();
$mail->Host = 'smtp.host';
$mail->SMTPAuth = true;
$mail->Username = 'yourUserName';
$mail->Password = 'yourPassword';
$mail->SMTPSecure = 'tls';

$mail->From = 'email@server.net';
$mail->FromName = 'John Doe';

$mail->addBCC('foo1@bar.com');
$mail->addBCC('foo2@bar.com');

$headerLine = $mail->headerLine('X-Mailgun-Recipient-Variables', '{"foo1@bar.com": {"first":"FooBar1", "id":1}, "foo2@bar.com": {"first":"FooBar2", "id": 2}}');
$mail->addCustomHeader($headerLine);

$headerLine = $mail->headerLine('To','%recipient%');
$mail->addCustomHeader($headerLine);

$mail->Subject = 'Hello, %recipient.first%!';
$mail->Body    = 'Hello %recipient.first%, Your ID is %recipient.id%.';

if(!$mail->send()) 
{
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} 
else 
{
    echo 'Message has been sent';
}
eckleman
  • 143
  • 1
  • 4
2

I, too, was unsuccessful in getting this to work. I put in a mailgun trouble ticket. Here's the essence of their response:

The "Warning" in our documentation is actually for API only, not SMTP. The reason for this is that when using the API we form/create the email and that allows our service to separate and create a new email per each recipient in the To: field. When using SMTP though, we simply relay the message with the content that was submitted to our service, we don't actually create the message MIME from scratch.

To work around this, you can input %recipient% in the To: field. This will create a separate message for each address specified in the "RCPT TO" during the SMTP session with our server. Now, this is where things get a bit tricky, as my unfamiliarity with ASP SMTP connector starts to show here. In my research I haven't found a way to specify a RCPT TO using the ASP SMTP connector. It seems to rely on what you input in the To and doesn't provide a way to specify a To: field and RCPT TO: field.

When I try to use %recipient% as the TO variable, its built-in method raises an error, "CDO.Message.1 error '8004020c' At least one recipient is required, but none were found." I'm not familiar with other mailers but I would be surprised if any would allow this construct.

Community
  • 1
  • 1
Len
  • 31
  • 6
  • Thanks. I got a similar response from MailGun and like you have still been unsuccessful in getting `%recipient%` to work in the To field in PHP. Sigh... – Scruffy Paws Aug 31 '16 at 14:42
0

Had the same requirement for a WordPress site, here is something I came with for those who need it :

class PHPMailerForMailgunBatch extends PHPMailer {
  public function createHeader() {
    $header = parent::createHeader();

    $header = preg_replace( "/To: .*\n/", "To: %recipient%\n", $header );

    return $header;
  }
}

and then

global $phpmailer;

$phpmailer = new PHPMailerForMailgunBatch( true );

// Config mailgun SMTP here

$mailgunBatchHeader = "X-Mailgun-Recipient-Variables: " . json_encode( $yourMailgunBatchVariables );

wp_mail( $emails, $subject, $content, [       
  $mailgunBatchHeader 
] );