[英]Codeigniter and Mandrill api, unable to send email
I am trying to email by using Mandrill with CodeIgniter. 我正在尝试使用Mandrill和CodeIgniter发送电子邮件。 I can use Mandrill API as described in their documentation:
我可以按照文档中的描述使用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));
This is pretty straight forward, but when I try to send Mandrill mail via CodeIgniter, I get error as result; 这很简单,但当我尝试通过CodeIgniter发送Mandrill邮件时,我得到错误结果; here is my code:
这是我的代码:
$this->load->library('mandrill/Mandrill');
$this->Mandrill->apikey($apikey);
//...
//All other options
$this->Mandrill->messages->send($params, true));
Library is loading successfully, sending email is where I get error. 库正在加载成功,发送电子邮件是我收到错误的地方。
Error thrown: 抛出错误:
Fatal error: Call to a member function send() on null
I guess you load mandrill class wrongly in order to make it "CodeIgniter way" do as follows: 我猜你错误地加载了mandrill类,以使其“CodeIgniter方式”做如下:
application/libraries
folder) see picture application/libraries
文件夹中)看到图片 $this->load->library('mandrill', array($apikey));
$this->load->library('mandrill', array($apikey));
加载类$this->load->library('mandrill', array($apikey));
$params
(you are missing "
, last line has two brackets ))
in the end) $params
所有拼写错误(你缺少"
,最后一行有两个括号))
”) valid code 有效代码
$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);
please note: I removed real api-key = MY_KEY
, email from = EMAIL_FROM
, email to = EMAIL_TO
请注意:我删除了真正的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());
Above code sends identical email twice (using different load methods); 上面的代码发送两次相同的邮件(使用不同的加载方法); response && few
var_dump()
are: 响应&&几个
var_dump()
是:
method using require_once...
使用
require_once...
方法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
method using $this->load->library();
使用
$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
In order to make it work I changed the way how API_KEY is retrieved in Mandrill constructor, since CodeIgniter's loader is sending parameter to constructor as array()
为了使其工作,我改变了在Mandrill构造函数中检索API_KEY的方式,因为CodeIgniter的加载器将构造函数作为
array()
发送给构造函数
Mandrill.php 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
Another option is CI-Mandrill , note that it uses version 1.0; 另一种选择是CI-Mandrill ,请注意它使用1.0版; installation time ~5 minutes or less
安装时间约5分钟或更短
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.