gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 13:19:18 31 4
gpt4 key购买 nike

我正在尝试通过将 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. 将类(文件mandrill.php and folder mandrill放在application/libraries文件夹中)见图片
  2. 使用 $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);

file structure


编辑(评论)

请注意:我删除了真实的 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());

以上代码两次发送相同的电子邮件(使用不同的加载方法); response && few 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() 发送给构造函数

山魈.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分钟以内

关于php - Codeigniter 和 Mandrill api,无法发送电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27467977/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com