Codeigniter SMTP Email with Amazon SES
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
You need to do 3 things to get CI to work with Amazon Simple Email Service (SES)
- Need to set
newline = \r\n
or you will get a timeout. - Need to set
smtp_crypto
to something. (New requirement) - Need to make sure "from" email address is approved in Amazon SES. I made my "from" email address "no-reply@mydomain.com"
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();
public function enviar_email($para, $assunto, $mensagem, $formato='html'){ $this->CI->load->library('email'); $config['mailtype'] = $formato; $config['useragent'] = 'Post Title'; $config['protocol'] = 'smtp'; $config['smtp_host'] = 'tls://email-smtp.us-east-1.amazonaws.com'; $config['smtp_user'] = 'smtpuser'; $config['smtp_pass'] = 'smtppass'; $config['smtp_port'] = '465'; $config['wordwrap'] = TRUE; $config['newline'] = "\r\n"; $this->CI->email->initialize($config); $this->CI->email->from('Your Verified Sender Email', 'Post Title'); $this->CI->email->to($para); $this->CI->email->subject($assunto); $this->CI->email->message($mensagem); if($this->CI->email->send()): return TRUE; else: $this->CI->email->print_debugger(); endif; }