Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Codeigniter and Mandrill api, unable to send email

I am trying to email by using Mandrill with CodeIgniter. I can use Mandrill API as described in their documentation:

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" => "[email protected]",
    "from_name" => "chris french",
    "subject" => "Your recent registration",
    "to" => array(array("email" => [email protected]")),
    "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; 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
like image 488
h_h Avatar asked Dec 14 '14 09:12

h_h


1 Answers

I guess you load mandrill class wrongly in order to make it "CodeIgniter way" do as follows:

  1. put class (the file mandrill.php and folder mandrill in application/libraries folder) see picture
  2. load class using $this->load->library('mandrill', array($apikey));
  3. fix all typos in $params (you are missing ", last line has two brackets ))in the end)
  4. modify mandrill.php so it accepts array as parameter in constructor; see SOLUTION part of answer
  5. use API as in tutorials from Mandrill (send, ping... whatever)

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" => "[email protected]",
        "from_name" => "chris french",
        "subject" => "Your recent registration",
        "to" => array(array("email" => "[email protected]")),
        "track_opens" => true,
        "track_clicks" => true,
        "auto_text" => true
    );

$this->mandrill->messages->send($params, true);

file structure


Edit (comments)

please note: I removed real api-key = MY_KEY, email from = EMAIL_FROM, email to = 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:

method using 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();

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

SOLUTION

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.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; installation time ~5 minutes or less

like image 158
Kyslik Avatar answered Sep 20 '22 08:09

Kyslik