简体   繁体   English

AWS Elastic Beanstalk - MAIL(发送和接收电子邮件)

[英]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 . 我刚刚设法将我的Web应用程序从共享托管服务迁移到AWS,使用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. 我的应用程序在注册时(使用SMTP)发送验证电子邮件,并且看起来用户没有收到电子邮件。 ( I'm still using the SMTP account of the shared hosting ) (我还在使用共享主机的SMTP帐户)

Also while using the shared hosting service, I used to create mail accounts for other team member using our website domain name, for instance ( noreply@domain.com). 此外,在使用共享托管服务时,我曾使用我们的网站域名为其他团队成员创建邮件帐户,例如(noreply@domain.com)。

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. 有些人建议SES只发送电子邮件和WorkMAil来接收电子邮件。

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. 1-如何允许弹性beanstalk应用程序使用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. 2-如何在EC2实例上设置Webmail(接收和发送电子邮件),或者至少在ec2实例上设置邮件服务,以及使用其他客户端(例如outlook)发送电子邮件。

3- how to create SMTP accounts or different email accounts using the domain name of the website. 3-如何使用网站的域名创建SMTP帐户或不同的电子邮件帐户。

PS : Please answer with very clear and detailed answer so I would understand , and everyone who might have the same problem. PS:请回答非常明确和详细的答案,以便我理解,并且每个人都可能遇到同样的问题。

For email hosting on AWS you can either use WorkMail or configure a mail server on an EC2 instance. 对于AWS上的电子邮件托管,您可以使用WorkMail或在EC2实例上配置邮件服务器。 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. 您不希望在Elastic Beanstalk服务器实例上运行邮件服务器。 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. 如果要在EC2上托管邮件服务器,您可能希望创建一个不受Beanstalk控制的单独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. 要通过SMTP从Elastic Beanstalk服务器发送电子邮件,您可以使用您选择和配置的任何邮件托管服务,或使用SMTP电子邮件传递服务(如Amazon SES)或第三方服务(如SendGrid)。

As I recently came across the same issue (php mail() didn't seem to work on Beanstalk) - I'll share some insights. 因为我最近遇到了同样的问题(php mail()似乎没有在Beanstalk上工作) - 我将分享一些见解。 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. 您正在AWS Beanstalk EC2上使用PHP。 Your application uses the PHP native function called mail();. 您的应用程序使用名为mail();的PHP本机函数。 It doesn't seem to work when you upload and deploy the app. 当您上传和部署应用程序时,它似乎不起作用。

Solution
1. Use SMTP with PHPmailer. 1.在PHPmailer中使用SMTP。 (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: https://github.com/PHPMailer ,下载如果您喜欢包管理,Composer等,您可以使用它,但如果您喜欢我,只想拥有小而干净的东西,您只需要以下文件:

    • class.phpmailer.php class.phpmailer.php
    • class.smtp.php class.smtp.php
    • PHPMailerAutoload.php PHPMailerAutoload.php
  2. Put these files in your own folder structure, like vendor/phpmailer/%the three files here%. 将这些文件放在您自己的文件夹结构中,例如vendor / phpmailer /%这三个文件%。
  3. Take the following code and note that the top is "linking" to your PHPMailerAutoload.php. 请使用以下代码并注意顶部是“链接”到您的PHPMailerAutoload.php。 Put it in a file called " mail.php " (or whatever you want): 把它放在一个名为“ mail.php ”的文件中(或任何你想要的):

     <?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). (请注意,我注释了一些您可能不需要的东西,但可能会很好。请访问https://github.com/PHPMailer/PHPMailer/wiki上的PHPmailer git hub网站以获取更多调整)。

  1. Now you just need to run "mail.php" and it will send an email. 现在你只需要运行“mail.php”,它就会发送一封电子邮件。 Of course you have to use a real SMTP-server (it's also possible to use Gmail, but check PHPmailer for that). 当然,您必须使用真正的SMTP服务器(也可以使用Gmail,但请检查PHPmailer)。 Once you see it in action, you can tweak this to work throughout your php application. 一旦你看到它在行动,你可以调整它在整个PHP应用程序中工作。 You have to build a new function to use instead of PHP mail(). 您必须构建一个新函数来代替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). 在许多线程中都有一个假设,即PHP的本机函数Sendmail或mail()在EC2(或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. 运行phpinfo.php并查找Sendmail。 But it points to localhost. 但它指向localhost。 So it works the same way as on your localhost, it sends mail to your localhost. 因此它的工作方式与localhost相同,它会将邮件发送到您的localhost。 Which is not setup, so you don't see the email anyway (unless you use shell to read it, which no one does). 这不是设置,所以你不会看到电子邮件(除非你使用shell来阅读它,没有人这样做)。 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. 并且,正如一些人所指出的,你不应该设置你的(至少相同的)Beanstalk EC2作为邮件服务器,因为它有缩放和其他原因,但主要是因为它很难看。 There are other ways to solve the problem. 还有其他方法可以解决这个问题。 Using Amazon SES is one often suggested solution. 使用Amazon SES是一种经常提出的解决方案。 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. 这也是成本,但1000张邮件的当前定价为0.10美元几乎没有。 So no real argument. 所以没有真正的争论。 SES could also offer a SMTP server and can be used in the above example. SES还可以提供SMTP服务器,可以在上面的示例中使用。

I hope this helps. 我希望这有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM