我猜你错误地加载了 mandrill 类以使其成为“CodeIgniter 方式”,如下所示:
- 放类(mandrill.php and folder mandrill文件夹中的文件
application/libraries)见图
- 使用
$this->load->library('mandrill', array($apikey));加载类
- 修正
$params中的所有错别字(你缺少",最后一行有两个括号))最后)
- 修改mandrill.php,使其在构造函数中接受数组作为参数;请参阅解决方案部分答案
- 在 Mandrill 的教程中使用 API(发送、ping...随便)
有效代码
$this->load->library('mandrill', array($apikey)); //load mandrill and provide apikey
$params = array(
"html" => "<p>\r\n\tHi Adam,</p>\r\n<p>\r\n\tThanks for <a href=\"http://mandrill.com\">registering</a>.</p>\r\n<p>etc etc</p>",
"text" => null,
"from_email" => "xxx@xxx.example.com",
"from_name" => "chris french",
"subject" => "Your recent registration",
"to" => array(array("email" => "xxx@yyy.example.com")),
"track_opens" => true,
"track_clicks" => true,
"auto_text" => true
);
$this->mandrill->messages->send($params, true);
编辑(cmets)
请注意:我删除了真正的 api-key = MY_KEY,电子邮件来自 = EMAIL_FROM,电子邮件至 = EMAIL_TO
$msg = array(
"html" => "The Message",
"text" => null,
"from_email" => "EMAIL_FROM",
"from_name" => "John Doe",
"subject" => "Acme",
"to" => array(array("email" => "EMAIL_TO")),
"track_opens" => true,
"track_clicks" => true,
"auto_text" => true
);
require_once APPPATH.'libraries/Mandrill.php';
$mandrill = new Mandrill('MY_KEY');
var_dump($mandrill->messages->send($msg, true));
//var_dump($mandrill->users->ping());
$this->load->library('Mandrill', array("MY_KEY")); //load mandrill and provide apikey
var_dump($this->mandrill->messages->send($msg, true));
//var_dump($this->mandrill->users->ping());
以上代码两次发送相同的电子邮件(使用不同的加载方法);回复 && 少数var_dump() 是:
使用require_once...的方法
object(Mandrill)[14] //setup
public 'apikey' => string 'MY_KEY' (length=22)
public 'ch' => resource(40, curl)
public 'root' => string 'https://mandrillapp.com/api/1.0/' (length=32)
public 'debug' => boolean false
array (size=1) //this is response
0 =>
array (size=4)
'email' => string 'EMAIL_TO' (length=24)
'status' => string 'sent' (length=4)
'_id' => string 'f7af72b1e1364a58a2eb302e94a1dc1e' (length=32)
'reject_reason' => null
使用$this->load->library();的方法
object(Mandrill)[30] //setup
public 'apikey' => string 'MY_KEY' (length=22)
public 'ch' => resource(41, curl)
public 'root' => string 'https://mandrillapp.com/api/1.0/' (length=32)
public 'debug' => boolean false
array (size=1) //this is response
0 =>
array (size=4)
'email' => string 'EMAIL_TO' (length=24)
'status' => string 'sent' (length=4)
'_id' => string '5965091637fa42dd98b40a934523022e' (length=32)
'reject_reason' => null
解决方案
为了让它工作,我改变了在 Mandrill 构造函数中检索 API_KEY 的方式,因为 CodeIgniter 的加载器将参数发送到构造函数为 array()
山魈.php
public function __construct($apikey=null) {
if(is_array($apikey)) $apikey = $apikey[0]; //added this line
if(!$apikey) $apikey = getenv('MANDRILL_APIKEY');
if(!$apikey) $apikey = $this->readConfigs();
//... rest of the file
另一个选项是CI-Mandrill,注意它使用的是1.0版本;安装时间 ~5 分钟或更短