简体   繁体   English

使用 MailGun API - Yii2 中的批量发送

[英]Using MailGun API - Batch Sending in Yii2

For a project in Yii2 framework I use MailGun.对于 Yii2 框架中的项目,我使用 MailGun。 Here I want to use MailGun Batch.这里我想使用MailGun Batch。 I have searched all over the internet for an example or tutorial but it still doesn't work and I get an error message at every step.我已经在互联网上搜索了一个示例或教程,但它仍然不起作用,而且我在每一步都收到一条错误消息。 This is my post function where the emails should be sent.这是我的邮件发送功能。 and my error message below.和我的错误信息如下。 Hopefully you can help me with it希望你能帮我解决

public function actionEmail() {
        $dataCollection = Trace::getOrderItemCollection();

        $configurator = new HttpClientConfigurator();
        $configurator->setApiKey( 'key-******API-KEY*******' );
        $configurator->setDebug( true );
        $mgClient     = new Mailgun( $configurator, new ArrayHydrator() );
        $batchMessage = $mgClient->messages()->getBatchMessage( "************MAilAdres******************.mailgun.org" );

        $batchMessage->setFromAddress( 'info@myemail.com' );
        $batchMessage->setSubject( 'Test-Subject' );
        $batchMessage->setHtmlBody( '<html>This is a test HTML text.</html>' );
        $batchMessage->setTextBody( "This is a test body text." );

        foreach ( Trace::getData( $dataCollection ) as $item ) {
            $batchMessage->addToRecipient( $item['email'] );
        }

        $batchMessage->finalize();
    }

在此处输入图片说明

It seems are using mailgun/mailgun-php package.似乎正在使用mailgun/mailgun-php包。

As decribed in its documentation here try this:正如此处的文档中所述,请尝试以下操作:

$mg = Mailgun::create( '**API-KEY**' );

// Setup batch
$batchMessage = $mg->messages()->getBatchMessage( "**DOMAIN**" );
$batchMessage->setFromAddress( "**EMAIL-FROM**");
$batchMessage->setSubject( "A Batch Message from the PHP SDK!" );
$batchMessage->setHtmlBody( '<html>This is the text body of the message!</html>' );
$batchMessage->setTextBody( "This is the text body of the message!" );

// Add receivers
foreach($receivers as $receiverEmail) {
    $batchMessage->addToRecipient( $receiverEmail);
}

// Send all that remain in queue
$batchMessage->finalize();

Remember, using $batchMessage->addToRecipient() adds a message to the queue.请记住,使用$batchMessage->addToRecipient()将一条消息添加到队列中。 Every time the queue hits 1000 recipients it will send the mails.每当队列达到 1000 个收件人时,它就会发送邮件。 Make sure to call $batchMessage->finalize();确保调用$batchMessage->finalize(); to send all mails that are remaining in the queue.发送队列中剩余的所有邮件。

Also note that when testing this with a sandbox domain, every receiver must be an authorized email address.另请注意,在使用沙盒域进行测试时,每个接收者都必须是经过授权的电子邮件地址。 Or else you may run into this "Check your inputs!"否则你可能会遇到这个“检查你的输入!” error.错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM