Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using MSMTP with CodeIgniter email library

Has anyone successfully been able to send email using the standard CodeIgniter email library when using msmtp?

I'm running Ubuntu and I've successfully installed and configured MSMTP. I've been able to send email from the command line and also using the default PHP mail() function.

My application/config/email.php file looks like this

$config = array(
    'protocol' => 'sendmail',
    'mailpath' => '/usr/bin/msmtp -C /etc/msmtp/.msmtprc -t',
    'smtp_host' => 'smtp.gmail.com',
    'smtp_user' => '[email protected]',
    'smtp_pass' => 'xxxxxxxx',
    'smtp_port' => 587,
    'smtp_timeout' => 30,
    'smtp_crypto' => 'tls',
);

But this doesn't work. If anyone's been successful it would be good to know how you did it. Ideally I'd like to use CodeIgniter's email library as it has a lot of good functionality that I don't want to have to write myself.

like image 513
Pattle Avatar asked Nov 09 '16 21:11

Pattle


People also ask

How to send email automatically in CodeIgniter?

Simply create a new file called the email. php, add the $config array in that file. Then save the file at config/email. php and it will be used automatically.


1 Answers

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 sendgrid
host smtp.sendgrid.net
port 587
auth on
tls on
tls_starttls on
tls_trust_file /etc/ssl/certs/ca-certificates.crt
user SendGridUsername
password SendGridPassword

Code Igniter Controller - application/controller/Tools.php

class Tools extends CI_Controller {

    public function message()
    {
        $this->load->library('email');

        $this->email->from('[email protected]', 'Nate');
        $this->email->to('[email protected]');

        $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
like image 195
quickshiftin Avatar answered Oct 19 '22 01:10

quickshiftin