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:
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?
Here is how I used Sendgrid to send mail from an EC2 instance using PHP:
sudo apt-get install php5-curl. 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);
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.
sudo apt-get install ssmtp/etc/ssmtp/ssmtp.conf (see below)/etc/ssmtp/revaliases (see below)echo message content | sendmail -v [email protected]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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With