使用 mailgun 消息生成器向单个收件人发送批量电子邮件

Using mailgun message builder to send bulk email to individual recipients

我在 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 中的邮件我想让它看起来像邮件作为个人发送给每个订阅者,并且邮件服务器不应将其标记为垃圾邮件。

尝试批量发送消息

$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