繁体   English   中英

Codeigniter和Mandrill api,无法发送电子邮件

[英]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方式”做如下:

  1. 把class(文件mandrill.php和文件夹mandrillapplication/libraries文件夹中)看到图片
  2. 使用$this->load->library('mandrill', array($apikey));加载类$this->load->library('mandrill', array($apikey));
  3. 修复$params所有拼写错误(你缺少" ,最后一行有两个括号)) ”)
  4. 修改mandrill.php,使其在构造函​​数中接受数组作为参数; 请参阅解答部分的答案
  5. 在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);

文件结构


编辑(评论)

请注意:我删除了真正的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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM