AWS Elastic Beanstalk - MAIL (Sending and receiving emails )

拈花ヽ惹草 提交于 2019-12-05 21:52:39

For email hosting on AWS you can either use WorkMail or configure a mail server on an EC2 instance. Those are your only options unless you look to third party mail hosting service. There are plenty of tutorials out there for either option, so I won't go into that here.

You do not want to run a mail server on an Elastic Beanstalk server instance. That would result in duplicate mail servers being created if your application scales up, mail server(s) being deleted every time you update your application, and generally all sorts of issues. You would want to create a separate EC2 instance that isn't controlled by Beanstalk if you want to host a mail server on EC2.

For sending email via SMTP from your Elastic Beanstalk servers you would either use whatever mail hosting service you have chosen and configured, or use an SMTP email delivery service such as Amazon SES, or a third party service like SendGrid.

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('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';
    }
    ?>
    

(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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!