Question

I have seen lots of discussion on the problem of sending mail from an amazon EC2 instance using php's mail function. None of the suggestions have worked with me.

Here is my setup:

  • I have ubuntu EC2 instance.
  • I followed the steps on Amazon's SES to install postfix using the credentials that were created for me. I tried both stunnel and direct methods.
  • I used swiftmail transport to send my mail according to this. The transport function for sending a mail returns 0 delivered.
  • I used php mail just by itself and it returns true. However, no mail is delivered not even in spam.

By the way I'm not using the production service with SES. The limits are fine with me, I just want it to work.

I suspect I'm not being authenticated properly and I don't see why. I tried to telnet ...amazonaws.com 25 and it gets connected. But when I tried the command Mail From: ... in telnet, it says authentication is required.

I have already thought of an alternative: sendgrid. Too expensive for my use.

Any thoughts?

Was it helpful?

Solution

Here is how I used Sendgrid to send mail from an EC2 instance using PHP:

  1. Sign up for a Sendgrid account. You will receive a username and password to use for sending emails. Also, Sendgrid will manually verify your account (possibly to prevent spam).
  2. Install PHP curl library in ubuntu: sudo apt-get install php5-curl.
  3. Use this PHP code to send email:

    $url = 'http://sendgrid.com/';
    $user = 'sendgrid_user';
    $pass = 'sendgrid_password'; 
    
    $params = array(
        'api_user'  => $user,
        'api_key'   => $pass,
        'to'        => $dest_addr,
        'subject'   => $subject,
        'html'      => $body,
        //'text'      => 'testing body',
        'from'      => $from_addr,
      );
    
    $request =  $url.'api/mail.send.json';
    
    $session = curl_init($request);
    // Tell curl to use HTTP POST
    curl_setopt ($session, CURLOPT_POST, true);
    // Tell curl that this is the body of the POST
    curl_setopt ($session, CURLOPT_POSTFIELDS, $params);
    // Tell curl not to return headers, but do return the response
    curl_setopt($session, CURLOPT_HEADER, false);
    curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
    
    // obtain response
    $response = curl_exec($session);
    
    //If the result is {"message":"success"}, then the mail is sent.  
    curl_close($session);
    

OTHER TIPS

One alternative to postfix is to use "simple smtp" (ssmtp) which will provide a working sendmail that other program/frameworks may use. In this example, ssmtp will send emails via a gmail account.

  1. First open a gmail account if you don't already have one
  2. make sure you absolutely NOT have a concurrent mail app like xmail or postfix already installed or it will interfere
  3. then install ssmtp : sudo apt-get install ssmtp
  4. then edit /etc/ssmtp/ssmtp.conf (see below)
  5. then edit /etc/ssmtp/revaliases (see below)
  6. then test : echo message content | sendmail -v test@something.com
  7. (optional) look at the log if it doesn't work : ll /var/log/mail.* and cat ...

Content of ssmtp.conf should be : (taken from my puppet module, replace <%= %> sections with your data)

root=<%= email %>
mailhub=smtp.googlemail.com:465
AuthUser=<%= email %>
AuthPass=<%= password %>
FromLineOverride=YES
UseTLS=YES

Warning : conf file should have unix eols.

And content of revaliases should be :

root:<%= email %>:smtp.googlemail.com:465

This technique is ultra-simple, but I guess it would not scale if you need to send hundreds of emails.

Another excellent link (in french) : http://doc.ubuntu-fr.org/ssmtp

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top