[英]Codeigniter and Mandrill api, unable to send email
我正在嘗試使用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
我猜你錯誤地加載了mandrill類,以使其“CodeIgniter方式”做如下:
application/libraries
文件夾中)看到圖片 $this->load->library('mandrill', array($apikey));
加載類$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());
上面的代碼發送兩次相同的郵件(使用不同的加載方法); 響應&&幾個var_dump()
是:
使用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
使用$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分鍾或更短
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.