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

问题描述

对于Yii2框架中的项目,我使用MailGun。 在这里我想使用MailGun Batch。我已经在互联网上搜索一个示例或教程,但仍然无法正常工作,并且每一步都会收到错误消息。 这是我的发帖功能,应在其中发送电子邮件。和下面的我的错误消息。 希望你能帮助我

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( '[email protected]' );
        $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();
    }

enter image description here

解决方法

似乎正在使用mailgun/mailgun-php软件包。

如其文档here中所述,请尝试以下操作:

$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();

请记住,使用$batchMessage->addToRecipient()将消息添加到队列中。每当队列达到1000个收件人时,它将发送邮件。确保调用$batchMessage->finalize();发送队列中剩余的所有邮件。

还请注意,在使用沙箱域进行测试时,每个收件人都必须是授权的电子邮件地址。否则,您可能会遇到this "Check your inputs!"错误。