gpt4 book ai didi

php - MailGun SMTP 批量发送与收件人变量显示收件人字段中的所有收件人

转载 作者:行者123 更新时间:2023-12-04 01:53:46 25 4
gpt4 key购买 nike

我试图让批量发送通过 SMTP 工作,但即使我发送给多个收件人并且我已经指定了用户变量(并且变量在发送的电子邮件中被成功替换),每个收件人都会出现在接收方生成的消息的 To: 字段中。

根据 MailGun 关于 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.



这是我的 SMTP header 的示例...
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"
}
}

生成的电子邮件应仅在收件人字段中显示每封电子邮件的一个收件人。我错过了什么吗?

最佳答案

我昨天开始搞砸了,我想我已经找到了解决方案。

诀窍是将收件人:地址留空并将您的收件人添加到 BCC 行。之后,添加一个自定义标题 - 收件人:%recipient%。 $mail->send() 不会提示,收到的电子邮件中的 To: 字段只显示单个收件人的电子邮件。

代码示例:

$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';
}

关于php - MailGun SMTP 批量发送与收件人变量显示收件人字段中的所有收件人,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37948729/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com