发送带有Mandrill的电子邮件模板给我错误
我试图将我的网站切换到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
山魈劫持等环节该链接是通过他们的服务器改道注入了自己的网址。用户在浏览右侧页面之前在其浏览器中看到了mandrill URL,结果如下
您帐户中的Sending Defaults页面上有一个用于点击跟踪的选项(应该是一个下拉菜单,靠近顶部,就在跟踪打开的复选框下面)。除非您为点击跟踪提供了不同的每封邮件设置,否则您选择的内容会应用于所有邮件的默认选项。使用SMTP,您可以使用自定义SMTP标头在每封邮件的基础上设置点击跟踪。有关使用SMTP标头进行定制的更多信息,请参见Mandrill KB here。
<?php
require_once 'Mandrill.php';
$mandrill = new Mandrill('MY API KEY IS USUALLY HERE');
$message = array(
'subject' => 'Test message',
'from_email' => '[email protected]',
'from_name' => 'Sender person',
'html' => '<p>this is a test message with Mandrill\'s PHP wrapper!.</p>',
'to' => array(array('email' => '[email protected]', 'name' => 'Recipient 1')),
'merge_vars' => array(array(
'rcpt' => '[email protected]',
'vars' =>
array(
array(
'name' => 'FIRSTNAME',
'content' => 'Recipient 1 first name'),
array(
'name' => 'LASTNAME',
'content' => 'Last name')
))));
//print_r($mandrill->messages->sendTemplate($template_name, $template_content, $message));
echo ("hello");
?>
和
$mandrill->messages->send($message, $async=false, $ip_pool=null, $send_at=null);
尝试使用新的当前源文件删除位置。 https://packagist.org/packages/mandrill/mandrill – 2015-03-24 12:40:13
抛出的异常,并在同一时间发送电子邮件两次发送邮件?或者是在调用多次后发生?我会尝试调试/打印,看看你的方法是不是叫了两次。另外,在这里: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