2016-05-10 44 views
0

我有一個存儲在一個數組中的電子郵件列表,我想遍歷該列表併發送一封電子郵件給每一個Mailgun。如何在PHP for循環中使用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 <[email protected]>', 
    'to'  => $to, 
    'subject' => 'Open This Email', 
    'text' => 'msg' 
)); 
} 

這是錯誤我得到

Fatal error: Uncaught exception 'Mailgun\Connection\Exceptions\MissingRequiredParameters' with message 'The parameters passed to the API were invalid. Check your inputs!' in /home/content/63/11130563/html/sites/paidfor/includes/mailgun/vendor/mailgun/mailgun-php/src/Mailgun/Connection/RestClient.php:127 Stack trace: #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 on line 127

這對於Mailgun循環正確的道路?有小費嗎?數組$電子郵件

內容

Array 
(
    [0] => [email protected] 
    [1] => [email protected] 
) 

更新:我注意到,因爲是我的mailgun帳戶的腳本不與​​非Gmail的域

+0

放置在這張貼片段的開始部分,您可以請發佈'print_r($ emails)' –

+0

@JeffPuckettII的輸出我將它添加到我的帖子底部 –

+0

只是爲了澄清,您是說這個腳本確實爲gmail域名工作? –

回答

0

我已經驗證了您的代碼工作的工作。該問題似乎與授權收件人與沙箱域。

$domain = "sandboxe08d52a4dcd247d7a31e3eb6e672ab03.mailgun.org";

,自發送到一個Gmail地址,當它工作,我假設這是對mailgun自己的主帳戶,並授權其在默認情況下,從沙箱中接收。

爲了得到其他的電子郵件從沙箱收到,那麼你將不得不加入他們驗證在沙箱中域設置

Click on domain settings


enter image description here


enter image description here

+1

我想通了!但是,按照你的步驟幫助我找到需要的東西,讓事情擺平:) –

0

我能回答我的問題。我轉而使用由mailgun提供的沙箱網址,並使用已驗證的域名「APP Base URL」,並解決了問題。