在Yii 2中使用MailGun API -批量发送

gcuhipw9  于 2022-11-09  发布在  其他
关注(0)|答案(1)|浏览(204)

对于一个在Yii2框架的项目,我使用MailGun。在这里我想使用MailGun批处理。我已经在互联网上搜索了所有的例子或教程,但它仍然不工作,我在每一步都得到一个错误消息。这是我的后函数,电子邮件应该发送。我的错误消息如下。希望你能帮助我

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

anhgbhbe

anhgbhbe1#

看来是用的mailgun/mailgun-php包。
如其文档中所述,请尝试以下操作:

$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!"错误。

相关问题