AWS Elastic Beanstalk - MAIL (Sending and receiving emails ) - email

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

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).
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
Put these files in your own folder structure, like vendor/phpmailer/%the three files here%.
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).
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.

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.

Related

cPanel 'No such user here'. Local account issue?

I have a website hosted outside the organization. The mail server to recieve the message is inside the organization. The website submits a simple form to email using PHP mail function. Works on every domain tested for the recipient except for ones internal to the domain.
So website is www.domain.com. Mail server is mail.domain.com. Hosted at separate sites. When www.domain.com wants to send a message to user#domain.com it should lookup the mx record and notice that mail.domain.com is at another location and forward the message.
If I configure it to send to user#otherdomain.com, the message is successfully sent and received. But sending to the same domain responds with 550 error 'No such user here'. So I'm wondering if the webserver thinks it is supposed to be the recipient of the mail, and thus responds with 'No user here' because, frankly there is no user configured there.
So does anyone know what cpanel/whm settings need to be added/adjusted to allow this server to know that it is not responsible for mail exchanging for this domain. I do have the mx record setup properly, but don't know if it also requires an A record for the address. Any help much appreciated.
I did notice others having the same issues, but no solutions were actually proposed, so I figured I would make my own question, and see if it get's a good answer for others too that may experience the same issue.
It looks like your routing settings are incorrect. Navigate to cPanel >> MX Entry and verify that the routing setting is Remote Mail Exchanger
As the mails are to be delivered externally, setting this as Local Mail Exchanger will make cPanel think the mails are setup locally and cPanel will try to deliver mails locally. As there are no local mail accounts configured with that name, it bounces with the mentioned error.
I would suggest avoiding sending directly to the MX. Submit your message to a local mail server and let it relay to the MX for you. If you send using PHPMailer with SMTP you will get much better feedback on deliveries. As for what cPanel is doing, check the local mail server log, usually in /var/log/mail.log.
You have to change the setting from local to "Remote Mail Exchanger" in dns zone file. and also create mail account at remote location site.

AWS SES + TLS: custom domain for mail servers

When messing around with different mail hosting options I noticed a very aggravating pattern with my Android phone. Neither the built-in mail app nor the gmail app supported email auto-configuration.
When using most mail services such as Namecheap, Zoho, Rackspace, etc. this became a real issue. I would enter my email address and password then instead of it just working like magic, it would invariably fail as it attempted to set the mail server to mail.example.com instead of mail.privateemail.com or smtp.zoho.com
I can configure a CNAME entry for my domain to redirect to these servers and successfully connect to mail.example.com.... up until I try to enable secure e-mail (STARTTLS or TLS wrapper). When I do this the domain name on the certificate does not match up to the domain name I am using to access and the whole thing fails.
Of course setting up my own mail server could be an option, but it could take months or years for my IP address to build up enough reputation to not get auto-blocked by major providers like Gmail or Yahoo. This whole past month DreamHost has been unable to send emails to any address owned by AT&T, which has been nightmarish to get resolved. Not wanting an issue like that, I would like to go with a big name for e-mail hosting.
While looking into Amazon SES to see if it would be easy to set up, I noticed this page on secure tunnels to AWS SES
I'm not super familiar with mail servers and I honestly have no idea what I'm reading on this page. Like I can follow the steps to install and configure this program and run it, but it doesn't accurately say what the purpose is of doing this. Am I right in believing that this might solve my SSL issue and allow me to send mail to mail.example.com without any issues? If so, is there any additional setup that I will require which is not adequately explained by this article?

AWS SES - Sending Emails

I have created an SES Account and I have verifed my domain - easy as the A record is already with AWS route53. I now need to send emails. Note: I have also created an smtp user account and have the keys.
Question - can I use any (lets say PHP as I like PHP) php script to send emails via SES?
I have found a couple of sites that appear to have scripts for SES like http://www.orderingdisorder.com/aws/ses/ but wanted to ask if now my domain is verified and I have user/keys etc is SES really just an SMTP gateway and I can use any (working) script to send emails via this interface?
thx
You can use their SMTP gateway or send emails via the AWS SDK for PHP programmatically. Just download the SDK, include it in your PHP file and you're good to go.
http://aws.amazon.com/about-aws/whats-new/2011/12/13/amazon-simple-email-service-gets-simpler-with-smtp/
http://docs.aws.amazon.com/AWSSDKforPHP/latest/#i=AmazonSES
http://aws.amazon.com/sdkforphp/

Mail server redirect to another server

I have a site, which has a server with "Parallels Plesk Panel" installed. I want to send an email from that site a "Contact Us" message to info#domain.com email.
The problem is that this email was already created by one of the programmers using the google mail system (apparently you can create accounts there with a domain name different from gmail.com).
So now, the server rejects my message, telling me that it can't find an email with this name. It works fine when I send to any other domain, but when sending to the same one, it fails. I've created another email info2#domain.com and sent emails there and it works.
My question now is, how do I send emails to the existent info#domain.com which is already created in gmail without making the server block me. One of the options I saw at this panel is to redirect the request for that email to another mailing system (and to specify its IP). Maybe that would help if I would to put there gmails IP?
Thanks.
EDIT:
Using my contact us form I am sending an email to info#domain.com. I get an SMTP error 550, can't find the mail box. When sending to anything but #domain.com it works. When adding that email to my server, it is also fine.
Now, the previous programmer already created info#domain.com, but not with our plesk panel, but using gmail server. Apparently, using gmail you can create an email of the type info#domain.com and not just info#gmail.com. The obvious problem is then that I try to send to this email. It sees that the server is domain.com and tries to find it there (same domain as the site from which I send the message). It fails and gives me the 550 error.
I want the server to send the email with that message to info#domain.com which is actually on gmail.
if I understand correctly, your problem is that two servers think they host the maildomain: your plesk server and gmail.
solution: disable local mail delivery for that domain on the plesk server and make sure plesk can correctly resolve the mx records of that domain , runing dig mx +short domain.com on the plesk server should return a google owned hostname, not the local hostname.
I don't own a plesk server, so I can't tell how how exactly to disable the mail domain, but a quick google search returns: http://www.serveridol.com/2011/03/16/disabling-email-service-for-a-domain-in-plesk/
http://search.yahoo.com/search?p=email+form+service&ei=UTF-8&fr=chr-greentree_ff&type=827316
try a remote email form service. most hosting companies' mail servers are local. to do this, you would have to make your own .htaccess file which contains php.ini mail server settings. i THINK this is correct. you can install php yourself to see what those settings are.
this is something you will probably have to do through the web hosting control panel.
and by the way, XHTML is served up as HTML unless you configure the server to serve XHTML up as XHTML. so use HTML when possible unless you know how to do that. here's how.
http://jesusnjim.com/web-design/setup-test-server.html

Sending emails from localhost ASP.net

I am trying to send a confirmation mail after a user has registered on my website. I am using Webmatrix and ASP.NET to implement this.
I followed the code on this website http://www.asp.net/web-pages/tutorials/email-and-search/11-adding-email-to-your-web-site
For creating the SMTP server i used the IIS manager in Windows 7.
But its not working.
I changed the settings to
WebMail.SmtpServer = "localhost";
WebMail.SmtpPort = 25;
WebMail.EnableSsl = false;
WebMail.UserName = "name";
WebMail.From = "---#gmail.com";
WebMail.Password = "pass";
What do i put as my username and password for this? I am trying to test this on localhost. I dont have a server account.
Please help
great tutorial how to do this is
Sending email in .NET through Gmail
i have used it myself.
for sure i can see one error:
WebMail.SmtpServer = "localhost"; <-- is the provider you are using to send email
localhost is the PC what are you using (unless you have your own smtp server)
the link will help as its only small change you need to do and you can after investigate
To send an e-mail you need an e-mail account. Your application will contact that server to send e-mails (if you're using GMail remember you need SSL so set it to true). That's why you can't use localhost as SMPT server: it's not an e-mail server!
Configuration can be done in the web.config file. See this article for more details about how to configure and use SmtpClient class.