我正在尝试使用Mandrill和CodeIgniter发送电子邮件。我可以使用Mandrill API,如文档中所述:
require_once(mandrill/Mandrill.php);
$Mandrill = new Mandrill($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
);
$Mandrill->messages->send($params, true));
这很简单,但是当我尝试通过CodeIgniter发送Mandrill邮件时,我收到错误结果;这是我的代码:
$this->load->library('mandrill/Mandrill');
$this->Mandrill->apikey($apikey);
//...
//All other options
$this->Mandrill->messages->send($params, true));
图书馆加载成功,发送电子邮件是我收到错误的地方。
抛出错误:
Fatal error: Call to a member function send() on null
答案 0 :(得分:7)
我猜你错误地加载了mandrill类才能使它成为&#34; CodeIgniter方式&#34;做如下:
application/libraries
文件夹中)见图片$this->load->library('mandrill', array($apikey));
$params
中的所有拼写错误(您遗失"
,最后一行最后有两个括号))
) 有效代码
$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);
请注意:我删除了真正的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());
上面的代码发送两次相同的电子邮件(使用不同的加载方法);响应&amp;&amp;少数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()
Mandrill.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分钟