我试图将我的网站切换到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