How do I send transactional email on AWS?

Asked

Viewed 1,877 times

4

I use an Ubuntu server on AWS and cannot run the PHP mail() function. You need to install something on the server or configure something on the AWS Console?

  • Try to install a local email server, below link with postfix installation/configuration tutorial; http://wiki.ubuntu-br.org/Postfix

3 answers

7

Limitations related to sending emails from EC2 instances

If you are trying to send emails directly from EC2 instances -- that is, without using the SES (Simple Email Service) --service, you will probably have the shots blocked after a certain amount of emails.

This is because AWS is very interested in not allowing users to "tarnish the reputation" of public Ips if any user tries to send SPAM from EC2 instances, as this would be harmful to all other customers.

The recommendation is to actually use the SES service, as it brings several advantages briefly listed below.

See more information on EC2 FAQ.

Sending emails through SES (Simple Email Service)

SES facilitates several tasks related to email triggering. Using SES, you have:

  • notifications of bounces;
  • notifications of complaints;
  • notifications of rejects;
  • easy to configure SPF and transparent DKIM (if using Route53)

There are two main ways to use SES: directly through API calls (e.g., Sendmail), or through SMTP endpoints.

Behold this page of the documentation (in English) that shows an example of PHP code that fires emails through SES. This page links to another that teaches the configure Postfix to fire emails through SES.

Another way to perform the shots, without the need to configure Postfix, is through the use of Phpmailer. You can specify an SMTP server that Phpmailer will use to trigger emails (in contrast to the mail() function, which will use a local SMTP). Thus, you can configure the SES SMTP endpoint directly in Phpmailer, without the need to install and configure Postfix. See other sample code responses using Phpmailer.

When you start using SES, you will be in a sandbox and can only fire up to 200 emails per day only to "test" recipients (who must confirm that they accept to receive their emails through a notification from SES itself).

To get out of the sandbox and win Production access, you must fill out a simple form. In general, the process is fast. When you leave the sandbox, initially you can send 5000 emails per day. If you stay close to this limit, and keep the rates of ounce and complaints, the limit will be automatically increased, little by little, up to 1M/day. If you need to fire more than 1M emails, the process is manual and you should explain your use case for support.

See more information on page of SES and in the User Guide (DKIM) - in English.

Sending emails directly via EC2 instances

There are some use cases where it is really necessary to fire emails from EC2 instances. If you really have this need, some steps are needed:

  1. Allocate Elastic Ips to instances that will trigger emails.
  2. Ask for support to set up reverse DNS for these Eips.
  3. Ask support to increase the limit for triggering emails from EC2 instances.

It will be necessary to explain to the support your use case to have your order met. Note that the configuration of SPF and DKIM cannot be facilitated by SES/Route53 in this case, and will be your responsibility -- it is not mandatory, but the delivery rate may be heavily impaired without the correct configuration and implementation of these two SPAM protection mechanisms.

The link to the form can be found on EC2 FAQ linked earlier.

2

You need to install an SMTP server on the server or use the SES AWS service that you can configure in the AWS Console. I recommend using the component Phpmailer instead of the mail() function as it is easier to configure

2

You have this video that explains how to proceed (minute 48:30).

If you don’t want to use the SES service on Amazon AWS, you need an SMTP server and library Phpmailer.

Using the Phpmailer public only needs to check what data your email server has. For example:

require("phpmailer/class.phpmailer.php");

$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPDebug = 0; // 1 para aparecer erros e mensagens relacionados com o SMTP, 0 para não aparecerem erros nem mensagens, 2 para mensagens apenas
$mail->SMTPAuth = true;
$mail->SMTPSecure = "http";
$mail->Host = "smtpout.secureserver.net"; //neste caso uso a minha conta na Godaddy
$mail->Port = 80; 

$mail->Username = "<seu nome>";
$mail->Password = "<sua senha>";
$mail->From = "<seu email>";
$mail->FromName = "<seu nome>";
$mail->AddAddress("<email destinatário>", "<nome destinatário>");
$mail->AddReplyTo("<seu email>", "<seu nome>");

$mail->WordWrap = 50; // set word wrap to 50 characters

$anexo = "pasta/all.zip"; //caso pretenda enviar anexos

$mail->AddAttachment($anexo, "dados.zip"); // caso queira adicionar um nome diferente ao arquivo de anexo
$mail->IsHTML(true); // se pretender enviar no formato HTML

$mail->AddEmbeddedImage('img/logo.png', 'logo'); //para enviar como logotipo de assinatura
$mail->Subject = "<Assunto>";
$mail->Body    = "A sua mensagem... <a href=\"http://www.seusite.com.br\"><img src=\"cid:logo\" alt=\"Título de seu site\"></a>";
if($mail->Send())
  echo "Enviado com sucesso";
else
  echo "Erro ao enviar email";
  • Thanks friends!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.