Using MSMTP with CodeIgniter email library Using MSMTP with CodeIgniter email library codeigniter codeigniter

Using MSMTP with CodeIgniter email library


I was able to send an email through CodeIgniter and msmtp without too much trouble. In my case I used Sendgrid as I encountered authentication issues using msmtp with Gmail and Yahoo. Here's my setup (running on Ubuntu 14.04, php 5.5.9, Code Igniter latest):

msmtp config - /home/quickshiftin/.msmtprc

account sendgridhost smtp.sendgrid.netport 587auth ontls ontls_starttls ontls_trust_file /etc/ssl/certs/ca-certificates.crtuser SendGridUsernamepassword SendGridPassword

Code Igniter Controller - application/controller/Tools.php

class Tools extends CI_Controller {    public function message()    {        $this->load->library('email');        $this->email->from('some-dude@gmail.com', 'Nate');        $this->email->to('another-dude@gmail.com');        $this->email->subject('Send email through Code Igniter and msmtp');        $this->email->message('Testing the email class.');        $this->email->send();    }}

Email Library Config - application/config/email.php

$config = [    'protocol' => 'sendmail',    'mailpath' => '/usr/bin/msmtp -C /home/quickshiftin/.msmtprc --logfile /var/log/msmtp.log -a sendgrid -t',];

Sending the email via the CLI

php index.php tools message

Thoughts on your issue

  • Is /etc/msmtp/.msmtprc readable by your webserver or command line user? Is /usr/bin/msmtp executable by said user?
  • popen may be disabled in your PHP environment
  • Use a debugger to trace through the call to CI_Email::_send_with_sendmail method to determine why it's failing in your case
  • If you configure a log file for msmtp as I have you can look there after trying to send through Code Igniter to catch potential issues


My Email config is below:

<?php defined('BASEPATH') OR exit('No direct script access allowed.');// Mail engine switcher: 'CodeIgniter' or 'PHPMailer'$config['useragent']        = 'PHPMailer';// 'mail', 'sendmail', or 'smtp'$config['protocol']         = 'smtp';$config['mailpath']         = '/project-folder/sendmail';$config['smtp_host']        = 'smtp.gmail.com';$config['smtp_user']        = 'Your Gmail Email';$config['smtp_pass']        = 'Your Gmail Pass';$config['smtp_port']        = 587;// (in seconds)$config['smtp_timeout']     = 30;  // '' or 'tls' or 'ssl'                     $config['smtp_crypto']      = 'tls'; // PHPMailer's SMTP debug info level: 0 = off, 1 = commands, 2 = commands and data, 3 = as 2 plus connection status, 4 = low level data output.                      $config['smtp_debug']       = 0; // Whether to enable TLS encryption automatically if a server supports it, even if `smtp_crypto` is not set to 'tls'.                       $config['smtp_auto_tls']    = false; // SMTP connection options, an array passed to the function stream_context_create() when connecting via SMTP.                   $config['smtp_conn_options'] = array();                 $config['wordwrap']         = true;$config['wrapchars']        = 76;// 'text' or 'html'$config['mailtype']         = 'html'; // 'UTF-8', 'ISO-8859-15', ...; NULL (preferable) means config_item('charset'), i.e. the character set of the site.                  $config['charset']          = null;                     $config['validate']         = true;// 1, 2, 3, 4, 5; on PHPMailer useragent NULL is a possible option, it means that X-priority header is not set at all$config['priority']         = 3;// "\r\n" or "\n" or "\r"$config['crlf']             = "\n";// "\r\n" or "\n" or "\r"                     $config['newline']          = "\n";                     $config['bcc_batch_mode']   = false;$config['bcc_batch_size']   = 200;// The body encoding. For CodeIgniter: '8bit' or '7bit'. For PHPMailer: '8bit', '7bit', 'binary', 'base64', or 'quoted-printable'.$config['encoding']         = '8bit';