【问题标题】:How can I used Mailgun within a PHP for loop?如何在 PHP for 循环中使用 Mailgun?
【发布时间】:2016-09-05 15:13:29
【问题描述】:

我有一个存储在数组中的电子邮件列表,我想遍历该列表并使用 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'
));
}

这是我得到的错误

致命错误:未捕获的异常“Mailgun\Connection\Exceptions\MissingRequiredParameters”与消息“传递给 API 的参数无效。检查您的输入!在 /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->responseHandler(Object(Guzzle\Http\Message \Response)) #1 /home/content/63/11130563/html/sites/paidfor/includes/mailgun/vendor/mailgun/mailgun-php/src/Mailgun/Mailgun.php(80): Mailgun\Connection\RestClient- >post('sandboxe08d52a4...', Array, Array) #2 /home/content/63/11130563/html/sites/paidfor/includes/mailgun/vendor/mailgun/mailgun-php/src/Mailgun/Mailgun.php (36): Mailgun\Mailgun->post('sandboxe08d52a4...', Array, Array) #3 /home/content/63/11130563/html/sites/paidfor/bag/success/index.php(21): Mailgun\Mailgun->sendMessage('sandboxe08d52a4...', A in /home/content/63/11130563/html/sites/paidfor/includes/mailgun/vendor/mailgun/mailgun-php/src/Mailgun/Connection/RestClient .php 上第 127 行

这是使用 Mailgun 循环的正确路径吗?有什么建议吗?

数组$emails的内容

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

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

【问题讨论】:

  • 放在本贴sn-p的开头,能不能把print_r($emails)的输出贴出来
  • @JeffPuckettII 我将它添加到我的帖子底部
  • 澄清一下,你是说这个脚本确实适用于 gmail 域?
  • @JeffPuckettII 我是正确的。如果数组有多个电子邮件,则脚本不起作用..无论扩展名如何。

标签: php mailgun


【解决方案1】:

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

$domain = "sandboxe08d52a4dcd247d7a31e3eb6e672ab03.mailgun.org";

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

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



【讨论】:

  • 我想通了!但是按照你的步骤帮助我找到了解决问题所需的菜单:)
【解决方案2】:

我能够回答我自己的问题。我不再使用 mailgun 提供的沙盒 URL,而是使用经过验证的域“APP Base URL”,它解决了这个问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-01-06
    • 2019-03-12
    • 1970-01-01
    • 2015-01-28
    • 1970-01-01
    • 1970-01-01
    • 2015-04-19
    • 2013-06-12
    相关资源
    最近更新 更多