如何在PHP for循环中使用Mailgun?

时间:2016-05-10 14:48:39

标签: php mailgun

我有一个存储在数组中的电子邮件列表,我想循环浏览该列表并使用Mailgun向每个邮件发送一封电子邮件。

这是我的剧本

$mgClient = new Mailgun('snipsnip');
$domain = "sandboxe08d52a4dcd247d7a31e3eb6e672ab03.mailgun.org";

$count = count($emails);
for($i = 0; $i < $count; $i++ ){
    $to = $emails[$i];
    $mgClient->sendMessage($domain, array(
    'from'    => 'Markus from StackOverflow <funnyemail@yes.com>',
    'to'      => $to,
    'subject' => 'Open This Email',
    'text'    => 'msg'
));
}

这是我得到的错误

  

致命错误:未捕获的异常&#39; Mailgun \ Connection \ Exceptions \ MissingRequiredParameters&#39;带消息&#39;传递给API的参数无效。检查您的输入!&#39;在/home/content/63/11130563/html/sites/paidfor/includes/mailgun/vendor/mailgun/mailgun-php/src/Mailgun/Connection/RestClient.php:127堆栈追踪:#0 / home / content / 63 /11130563/html/sites/paidfor/includes/mailgun/vendor/mailgun/mailgun-php/src/Mailgun/Connection/RestClient.php(90):Mailgun \ Connection \ RestClient-&gt; responseHandler(Object(Guzzle \ Http \)消息\响应))#1 /home/content/63/11130563/html/sites/paidfor/includes/mailgun/vendor/mailgun/mailgun-php/src/Mailgun/Mailgun.php(80):Mailgun \ Connection \ RestClient - &gt; post(&#39; sandboxe08d52a4 ...&#39;,Array,Array)#2 / home / content / 63/11130563 / html / sites / paidfor / includes / mailgun / vendor / mailgun / mailgun-php /src/Mailgun/Mailgun.php(36):Mailgun \ Mailgun-&gt; post(&#39; sandboxe08d52a4 ...&#39;,Array,Array)#3 / home / content / 63/11130563 / html / sites / paidfor / bag / success / index.php(21):Mailgun \ Mailgun-&gt; sendMessage(&#39; sandboxe08d52a4 ...&#39;,A in / home / content / 63/11130563 / html / sites / paidfor /包括/ mailgun /供应商/ mailgun / mailgun-PHP / SRC / M第127行的ailgun / Connection / RestClient.php

这是使用Mailgun进行循环的正确途径吗?有什么提示吗?

数组$ emails的内容

Array
(
    [0] => tempemail@divc.com
    [1] => blue-flame@hotmail.com
)

更新:我注意到该脚本不适用于非gmail域

2 个答案:

答案 0 :(得分:0)

我已验证您的代码与我的mailgun帐户一样有效。问题似乎是授权收件人使用沙箱域。

  

$ domain =&#34; sandboxe08d52a4dcd247d7a31e3eb6e672ab03.mailgun.org&#34;;

由于它在发送到一个Gmail地址时工作正常,我假设您是mailgun上的主要帐户,默认情况下,该帐户有权从沙箱接收。

要从沙盒中接收其他电子邮件,您必须在沙箱域设置中添加验证

Click on domain settings

enter image description here

enter image description here

答案 1 :(得分:0)

我能够回答我自己的问题。我转而使用mailgun提供的沙箱网址,并使用经过验证的域名&#34; APP Base URL&#34;它解决了这个问题。