Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Codeigniter SMTP Email with Amazon SES

Tags:

I think yesterday Amazon announced SMTP support for SES (Simple Email Service).

I tried to send SMTP email with Codeigniter with no luck.

I have a verified sender and everything looks good:

$this->load->library('email');  $config = array(     'protocol' => 'smtp',     'smtp_host' => 'email-smtp.us-east-1.amazonaws.com',     'smtp_user' => 'SMTP USERNAME',     'smtp_pass' => 'SMTP PASSWORD',     'smtp_port' => 465,     'mailtype' => 'html' );  $this->email->initialize($config); $this->email->print_debugger();  $this->email->from('[email protected]', 'Test From'); $this->email->to('[email protected]', 'Test To'); $this->email->subject('Test'); $this->email->message('test');  $this->email->send(); 

I tried the folowing smtp_host:

  • email-smtp.us-east-1.amazonaws.com
  • tls://email-smtp.us-east-1.amazonaws.com
  • ssl://email-smtp.us-east-1.amazonaws.com

When i echo the print_debugger() i get:

220 email-smtp.amazonaws.com ESMTP SimpleEmailService-194655181 hello: 421 Timeout waiting for data from client. 

These tests run on a mediatemple (gs) server.

like image 862
AFRC Avatar asked Dec 15 '11 20:12

AFRC


2 Answers

I got that timeout message until I added the line:-

$this->email->set_newline("\r\n"); 

I have my host set as ssl://email-smtp.us-east-1.amazonaws.com

like image 74
Stephen Avatar answered Sep 27 '22 02:09

Stephen


You need to do 3 things to get CI to work with Amazon Simple Email Service (SES)

  1. Need to set newline = \r\n or you will get a timeout.
  2. Need to set smtp_crypto to something. (New requirement)
  3. Need to make sure "from" email address is approved in Amazon SES. I made my "from" email address "[email protected]"

Additionally, you should set up DKIM for your "from" email address to prevent emails from getting put in spam folders. This involves going into Amazon SES -> Identity Management -> Email Addresses -> DKIM, hitting the enable button, and adding 3 DNS entries to your website's DNS.

No need to do anything special to set up SPF. The envelope domain amazonses.com passes SPF.

Finally, make sure to use "reply-to" if you want users to be able to reply to an e-mail address different from your approved "from" e-mail address.

Example working code:

$obj = &get_instance();  $config['protocol'] = 'smtp'; $config['smtp_host'] = 'email-smtp.us-west-2.amazonaws.com'; $config['smtp_user'] = 'USER'; $config['smtp_pass'] = 'PASS'; $config['smtp_port'] = '587'; $config['newline'] = "\r\n"; $config['smtp_crypto'] = 'tls'; $obj->email->initialize($config);  $obj->email->set_mailtype('html'); // don't html_escape email header variables $obj->email->from(MV_FROM_EMAIL, $from_name); $obj->email->reply_to($from_email, $from_name); $obj->email->to($to); $obj->email->subject($subject); $obj->email->message($obj->load->view($path, html_escape($data), true)); $obj->email->send(); 
like image 31
RedDragonWebDesign Avatar answered Sep 23 '22 02:09

RedDragonWebDesign