Codeigniter SMTP Email with Amazon SES

22,161

Solution 1

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

Solution 2

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

Solution 3

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;

    } 

Solution 4

I also needed to add the line

$config['smtp_crypto']  = 'tls';

to my config array

this is supported by CI 2.1.0 and greater

Solution 5

The setup that worked for me looks like this:

$test_config['protocol'] = 'smtp';
$test_config['smtp_host'] = 'ssl://email-smtp.us-east-1.amazonaws.com';
$test_config['smtp_port'] = '465';
$test_config['smtp_user'] = 'XXXXXXXXXXXXXXXXXXX';
$test_config['smtp_pass'] = 'YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY';
$test_config['newline']      = "\r\n"; 

$this->email->initialize($test_config);

$this->email->from('[email protected]', 'From at Test.com');
$this->email->to('[email protected]');

$this->email->subject('Email Test');
$this->email->message('Testing the email class.');

$this->email->send();

The newline character must be set to "\r\n", and can bet set in the config file, if properly set as "\r\n", not '\r\n' as noted above.

Share:
22,161
AFRC
Author by

AFRC

Updated on January 06, 2022

Comments

  • AFRC
    AFRC over 2 years

    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.

  • tim peterson
    tim peterson almost 12 years
    hi @Stephen, I added these lines to my code, but i'm still getting an error when I send to my gmail error:Email address is not verified.. It only works when I send to the same domain from which I sent the message. Can you tell me what your from and to email addresses are in each case? thanks!
  • antitoxic
    antitoxic over 11 years
    @Stephen, where did you find this out? That saved me here with FuelPHP framework as well.
  • pr1001
    pr1001 over 11 years
    Unfortunately this explicit command is required: setting it via $config['newline'] = '\r\n'; in config/email.php didn't work.
  • ChiefTwoPencils
    ChiefTwoPencils about 11 years
    Could you add some context to your answer please. Explain where the code provided by the OP went wrong and why this works.
  • Tommy McGlynn
    Tommy McGlynn over 10 years
    Using these settings, I get the following error sockopen(): SSL operation failed with code 1. OpenSSL Error messages: error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol
  • sventechie
    sventechie about 10 years
    $config['newline'] = "\r\n"; worked for me (note double quotes)
  • Mei Gwilym
    Mei Gwilym almost 8 years
    I used this set_newline method and also stripped out all \r and \n characters from the body string to get it to work.
  • RedDragonWebDesign
    RedDragonWebDesign over 6 years
    This fixed it for me. I think Amazon SES requires this now. Thank you.
  • Neo Pham
    Neo Pham over 5 years
    Work for java mail too
  • Hitesh Vala Ahir
    Hitesh Vala Ahir about 5 years
    ssl:// is work for me into codeigniter, thanks buddy