我試圖將我的網站切換到Mandrill,但是我遇到了PHP API的一些問題。發送帶有Mandrill的電子郵件模板給我錯誤
有兩個問題:
- 首先,發送電子郵件的兩倍。底部的代碼是我擁有的所有代碼(PHP打開和關閉標記除外),我無法弄清楚爲什麼每次都會發送兩次電子郵件。
- 其次,我從cURL得到一個錯誤,說URL沒有設置。電子郵件正在發送,顯然有一個URL集。錯誤在下面。
這裏是我的代碼:
require_once './libraries/Mandrill.php';
try {
$mandrill = new Mandrill('myapikey');
$template_name = 'my-template-slug';
$template_content = '';
$message = array(
'to' => array(
array(
'email' => '[email protected]',
'name' => 'RecipientsName',
'type' => 'to'
)
),
'auto_text' => true,
'merge_vars' => array(
array(
'rcpt' => '[email protected]',
'vars' => array(
array(
'name' => 'USERNAME',
'content' => 'user1234'
),
array(
'name' => 'CONFIRM_CODE',
'content' => '19874lahg62378hwsi'
)
)
)
)
);
$result = $mandrill->messages->sendTemplate($template_name, $template_content, $message);
} catch(Mandrill_Error $e) {
echo 'A mandrill error occurred: ' . get_class($e) . ' - ' . $e->getMessage();
throw $e;
}
這裏是錯誤:
A mandrill error occurred: Mandrill_HttpError - API call to messages/send-template failed: No URL set! Fatal error: Uncaught exception 'Mandrill_HttpError' with message 'API call to messages/send-template failed: No URL set!' in /Users/Gavin/Desktop/Web/mandrill-test/libraries/Mandrill.php:126 Stack trace: #0 /Users/Gavin/Desktop/Web/mandrill-test/libraries/Mandrill/Messages.php(160): Mandrill->call('messages/send-t...', Array) #1 /Users/Gavin/Desktop/Web/mandrill-test/index.php(70): Mandrill_Messages->sendTemplate('my-template-slug', Array, Array) #2 /Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/tools/devappserver2/php/setup.php(131): require('/Users/Gavin/De...') #3 {main} thrown in /Users/Gavin/Desktop/Web/mandrill-test/libraries/Mandrill.php on line 126
拋出的異常,並在同一時間發送電子郵件兩次發送郵件?或者是在調用多次後發生?我會嘗試調試/打印,看看你的方法是不是叫了兩次。另外,在這裏:http://stackoverflow.com/questions/22647687/sending-email-with-mandrill-using-php?rq=1他們建議使用send而不是sendTemplate – marianosimone 2015-03-17 14:47:45
上面的代碼是唯一使用的代碼。是的,它拋出異常併發送電子郵件兩次。 – Gavin 2015-03-17 15:30:12
我還應該補充說,因爲這個問題正在發生,所以我轉而使用Mandrill的SMTP服務器而不是API(我的Web服務器上託管了自己的模板)並且只發送一次。 – Gavin 2015-03-17 15:39:07