AWS Elastic Beanstalk - MAIL (send and receive email)

Well, I just managed to migrate my web application from shared hosting to AWS using Elastic beanstalk. However, I am struggling with the email service.

Well My Application sends an email confirmation when registering (using SMTP), and it looks like users are not receiving emails. (I still use the SMTP account for shared hosting)

Also, when using the shared hosting service, I used, for example, mail accounts for another team member using our domain name of our site (noreply@domain.com).

Well, I tried to find a good answer to my question, but none of the questions fully meets my needs.

some people recommend that SES only send emails and WorkMAil to receive emails.

Well, in my case, I do not want to use other services. Since my site is really small, so I want someone to be able to clearly answer the following questions:

1- How to allow the elastic beanstalk application to send emails using smtp.

2- how to set up webmail on an EC2 instance (receive and send emails) or at least set up a mail service on an ec2 instance and send emails using other clients such as Outlook.

3- How to create SMTP accounts or different email accounts using the website domain name.

PS: Please answer with a very clear and detailed answer so that I understand, and everyone who may have the same problem.

+1
email amazon-web-services amazon-ec2 elastic-beanstalk smtp
Jul 25 '15 at 16:12
source share
2 answers

To host email in AWS, you can either use WorkMail or configure your mail server on an EC2 instance. These are your only options if you are not looking at third-party email hosting. There are many tutorials for any option, so I won’t go into it.

You do not want to run the mail server on an Elastic Beanstalk server instance. This will lead to duplicate mail servers, if your application is scaled, the mail server is deleted every time you update the application, and, as a rule, all kinds of problems. You want to create a separate instance of EC2 that is not controlled by Beanstalk if you want to host a mail server on EC2.

To send e-mail via SMTP from Elastic Beanstalk servers, you will use any mail hosting service that you have selected and configured, or use an SMTP e-mail delivery service, such as Amazon SES, or a third-party service, such as SendGrid.

+4
Jul 25 '15 at 16:48
source share

As I recently encountered the same problem (php mail () did not seem to work on Beanstalk) - I will talk about some problems. Perhaps this has already been said here, but then consider it as a working final solution.

Problem
You are using PHP on AWS Beanstalk EC2. Your application uses a built-in PHP function called mail () ;. This does not seem to work when downloading and deploying the application.

Decision
1. Use SMTP with PHPmailer. (I will explain below why).

  1. https://github.com/PHPMailer , download it If you like the management pack, Composer, etc., you can use it, but if I like you, I just want to have something small and clean, you only need the following files:

    • class.phpmailer.php
    • class.smtp.php
    • PHPMailerAutoload.php
  2. Put these files in your own folder structure, for example vendor / phpmailer /%, in the three files here%.
  3. Take the following code and note that the vertex is "linked" to your PHPMailerAutoload.php. Put it in a file called " mail.php " (or whatever):

    <?php require '../vendor/phpmailer/PHPMailerAutoload.php'; $mail = new PHPMailer; //$mail->SMTPDebug = 3; // Enable verbose debug output $mail->isSMTP(); // Set mailer to use SMTP $mail->Host = 'smtp.hostname.com'; // Specify main and backup SMTP servers $mail->SMTPAuth = true; // Enable SMTP authentication $mail->Username = 'username'; // SMTP username $mail->Password = 'password'; // SMTP password $mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted $mail->Port = 587; // TCP port to connect to $mail->SMTPOptions = array( 'ssl' => array( 'verify_peer' => false, 'verify_peer_name' => false, 'allow_self_signed' => true ) ); $mail->setFrom('hello@example.com', 'Name'); $mail->addAddress('to@example.com', 'To Name'); // Add a recipient // Name is optional $mail->addReplyTo('hello@example.com', 'Name'); //$mail->addCC('cc@example.com'); //$mail->addBCC('bcc@example.com'); //$mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments //$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name $mail->isHTML(true); // Set email format to HTML $mail->Subject = 'Here is the subject'; $mail->Body = 'This is the HTML message body <b>in bold!</b>'; $mail->AltBody = 'This is the body in plain text for non-HTML mail clients'; if(!$mail->send()) { echo 'Message could not be sent.'; echo 'Mailer Error: ' . $mail->ErrorInfo; } else { echo 'Message has been sent'; } ?> 

(Please note that I commented on some things that you might not need, but might be fine. Check out the PHPmailer git website at https://github.com/PHPMailer/PHPMailer/wiki for additional settings).

  1. Now you just need to run "mail.php" and it will send an email. Of course, you must use a real SMTP server (it is also possible to use Gmail, but for this you need to check PHPmailer). Once you see this in action, you can configure it to work throughout your php application. You must create a new function to use instead of PHP mail (). What for? I will move on to this in the next paragraph.

Background / Description
There is an assumption, said in many threads, that the native PHP function is called Sendmail, or mail () does not work on EC2 (or Beanstalk). Not true. He works. He just sits and does not know what to do. Run phpinfo.php and find Sendmail. But this points to localhost. Thus, it works the same as on your localhost, it sends mail to your localhost. Which is not configured, so you still don’t see the message (unless you use a shell to read that no one does). And, as some have pointed out, you should not configure (at least one) Beanstalk EC2 as a mail server due to scaling and other reasons, but mainly because it is ugly. There are other ways to solve the problem. Using Amazon SES is a frequently suggested solution. Fair enough, if you want to send thousands of emails and make sure that it works. It also costs, but it’s almost nothing with a current price of $ 0.10 per 1000 letters. So no real arguments. SES can also offer an SMTP server and can be used in the above example.

Hope this helps.

+2
May 6 '17 at 4:47
source share



All Articles