Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS Elastic Beanstalk - MAIL (Sending and receiving emails )

Well I have just managed to migrate my web application from shared hosting service to AWS, Using Elastic beanstalk . However I m struggling with emails service.

Well My Application sends verification email upon registration (using SMTP) , and it looks that users are not receiving emails. ( I'm still using the SMTP account of the shared hosting )

Also while using the shared hosting service, I used to create mail accounts for other team member using our website domain name, for instance ( [email protected]).

Well I tried to look for a good answer regarding my question, but none of the question answers fully my needs.

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

Well in My case I don't want to use other services.since my website is really small, so I wish someone can answer clearly the following questions:

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

2- how to setup a webmail on the EC2 instance ( to receive and send emails ), or at least setting up the mail service on the ec2 instance , and sending emails using other clients like outlook for instance.

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

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

like image 704
Sam Ben Avatar asked Dec 15 '22 12:12

Sam Ben


1 Answers

As I recently came across the same issue (php mail() didn't seem to work on Beanstalk) - I'll share some insights. Perhaps it's already been said in here, but then see it as working end solution.

Problem
You are using PHP on AWS Beanstalk EC2. Your application uses the PHP native function called mail();. It doesn't seem to work when you upload and deploy the app.

Solution
1. Use SMTP with PHPmailer. (I'll explain below why).

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

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

    <?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('[email protected]', 'Name');
    $mail->addAddress('[email protected]', 'To Name');     // Add a recipient                              // Name is optional
    $mail->addReplyTo('[email protected]', 'Name');
    //$mail->addCC('[email protected]');
    //$mail->addBCC('[email protected]');
    
    //$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';
    }
    ?>
    

(Note that I commented out some things you may not need, but could be good. Check PHPmailer git hub website on https://github.com/PHPMailer/PHPMailer/wiki for more tweaks).

  1. Now you just need to run "mail.php" and it will send an email. Of course you have to use a real SMTP-server (it's also possible to use Gmail, but check PHPmailer for that). Once you see it in action, you can tweak this to work throughout your php application. You have to build a new function to use instead of PHP mail(). Why? I'll go to that in the next paragraph

Background/description
There is an assumption said in many threads that PHP's native function called Sendmail, or mail() doesn't work on EC2 (or Beanstalk). Not true. It does work. It's just sitting there and doesn't know what to do. Run a phpinfo.php and look for Sendmail. But it points to localhost. So it works the same way as on your localhost, it sends mail to your localhost. Which is not setup, so you don't see the email anyway (unless you use shell to read it, which no one does). And, as some has pointed out, you shouldn't setup your (the same at least) Beanstalk EC2 as a mail server, because of scaling and other reasons, but mainly because it's ugly. There are other ways to solve the problem. Using Amazon SES is one often suggested solution. Fair enough, if you want to send thousands of mails and make sure it works. It also costs, but that's almost nothing with current pricing at $0.10 for 1000 mails. So no real argument. SES could also offer a SMTP server and can be used in the above example.

I hope this helps.

like image 148
Rbbn Avatar answered Dec 26 '22 17:12

Rbbn