Php Codeigniter和Mandrill api,无法发送电子邮件
我试图通过使用Mandrill和CodeIgniter来发送电子邮件。我可以使用他们文档中描述的Mandrill API:Php Codeigniter和Mandrill api,无法发送电子邮件,php,codeigniter,mandrill,Php,Codeigniter,Mandrill,我试图通过使用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=\"
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));
库加载成功,发送电子邮件是我收到错误的地方
抛出错误:
Fatal error: Call to a member function send() on null
我猜您错误地加载了mandrill类,以便使其成为“CodeIgniter方式”,请执行以下操作:
应用程序/库
文件夹中)参见图片$this->load->library('mandrill',数组($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);
上述代码发送相同的电子邮件两次(使用不同的加载方法);响应和少量var\u dump()
为:
方法使用require\u一次…
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();
解决方案
为了让它工作,我改变了在Mandrill构造函数中检索API_键的方式,因为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
另一个选项是,请注意,它使用的是1.0版;安装时间约为5分钟或更短显示错误,我们是否应该猜测?单词“不能”是不够的。是的,这是我得到的错误“致命错误:在null上调用成员函数send()“你好,我找到问题了!我会重新构思我的答案。这个问题很好。嗨,Kyslik,谢谢你的详细回答。我以完全相同的方式完成了每一步,但是当我像这样使用Mendrill时没有成功:$mandrill=newmandrill('api-key');和$mandrill->messages->send($params,true);它很好用。我想知道,为什么它不能以CI的方式工作。你修复了所有的打字错误吗,因为它对我有效。我只是测试了一切。我正在使用ci2.2(刚刚下载,mandrillapi刚刚下载)。错误是否相同?请参阅我编辑的答案,正如我所说,我设法使用这两种方法发送电子邮件;作为回应,
'reject\u reason'=>null
只是让用户知道请求没有错误。谢谢,我喜欢上一个解决方案,即编辑Mandrill构造函数以使其以CI方式工作。再次感谢您如此详细的答复。
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
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
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