我在 php 中使用 mailgun 作为 API 服务来发送电子邮件。但是我正在构建一个模块,我可以在其中向订阅者发送电子邮件。我正在使用下面的代码来实现相同的目的。$builder = new MessageBuilder();$builder->setFromAddress("updates@somedomain.com", array("first"=>"Some Domain"));foreach ($subscribers as $key => $value) $builder->addToRecipient($value['email']);$builder->setSubject($subject);$builder->setHtmlBody(include("email-templates/emailSubscribers.php"));if(strlen($attachment) > 20) $builder->addAttachment($attachment); try { $mgClient = Mailgun::create(MAILGUN_API_KEY); $result = $mgClient->messages()->send(MAILGUN_DOMAIN_NAME, $builder->getMessage()); if(strpos($result->getMessage(), 'Queued') !== false) return [ 'status' => true ]; else return [ 'status' => false, 'message' => $result->getMessage() ]; // For the ID: $response->getId() }catch(\Exception $e) { echo json_encode( [ 'type' => 'error', 'heading' => 'Could not broadcast!', 'message' => 'Some problem occurred while sending emails, kindly debug. '.$e ] ); }代码工作得很好,唯一的问题是当邮件被发送时,所有的收件人都被标记了TO并且任何人都可以看到邮件TO我想让它看起来像邮件被发送给每个订阅者作为个人而且它不应该被邮件服务器标记为垃圾邮件。
1 回答
至尊宝的传说
TA贡献1789条经验 获得超10个赞
尝试作为批量消息发送
$mgClient = Mailgun::create(MAILGUN_API_KEY);
$builder= $mgClient->messages()->getBatchMessage("somedomain.com");
//regular stuff
$builder->setFromAddress("updates@somedomain.com", array("first"=>"Some Domain"));
foreach ($subscribers as $key => $value)
$builder->addToRecipient($value['email']);
$builder->setSubject($subject);
$builder->setHtmlBody(include("email-templates/emailSubscribers.php"));
if(strlen($attachment) > 20)
$builder->addAttachment($attachment);
$builder->finalize(); //equivalent of ->send
- 1 回答
- 0 关注
- 99 浏览
添加回答
举报
0/150
提交
取消