Setting up a SMTP mail server on an Ubuntu Server – (Amazon EC2 Specifically!)
Want more about Amazon EC2 & Ubuntu? Check out my new blog at: www.whoisthestudent.com
weekly posts about all things Amazon EC2, Ubuntu & Moodle development.
I have been working on Moodle plugins for a number of years and never had any issues with my hosting companies. When Moodle 2.0 came onto the scene, things were a little different. A higher version of PHP was needed than my hosting company could offer.
So what did I do? I hopped over to my Amazon EC2 instance that is running a Moodle 2.0 installation.
Everything was working really well until I needed a mail server. Which I did not have set up. Needing to test some mail related libraries, this posed a bit of a problem!
So here is a little guide in getting a mail server setup for Moodle when using Ubuntu.
First thing we need to do is install postfix. Open up a command line to your EC2 instance and run the following command:
sudo apt-get install postfix
Next we need to tell PHP that we now have a mail server, navigate into the conf.d folder by typing:
Next we need to create an ini file, simply type:
To finish all we need to do is open up this file using a text editor (vim in this case):
and then finally add these lines:
sendmail_from = email@example.com
sendmail_path = /usr/sbin/sendmail -t -i -f firstname.lastname@example.org
Replace the email address with your default email address.
And that’s it! You now have a simple mail server set up. Before you get caught up in the details of the application that you are working on, we need to test this server to check that is everything is going ok.
On a directory of your web server, create a testmail.php file, and stick in the following lines:
$to = “email@example.com”;
$subject = “Test mail”;
$message = “Hello! This is a simple email message.”;
$from = “firstname.lastname@example.org”;
$headers = “From:” . $from;
echo “Mail Sent.”;
Make sure to add your details into this little blob of code!
Mail being marked as spam
One little thing to keep in mind before you finish. Make sure that you check your spam folder for the emails that are being sent. Sometimes the I.P addresses of EC2 servers have previously been identified as spam, or the domain names don’t match to where you are sending the mail from. Because of this, the mail may end up in your spam folder!
Amazon EC2 Firewall
It is important to remember that the Amazon EC2 firewall has ports locked down by default. To allow the mail server to work correctly you will need to open the mail port 25. You will notice that amazon will have port 25 as one of the default options available when creating new access rules.
To open port 25, follow these steps:
Navigate to your AWS console.
On the bottom left corner, you will see Security Groups. This will list all of the security plans you currently have set up and assigned to different instances.
Click the security group you wish to edit
Then under the “Inbound” tab you will see a list of ports currently open.
Beside “Create a new rule” select SMTP (Not SMTPS!).
To finish click “Add Rule” and then “Apply Rule Changes” and you are finished!
For security groups to work, they need to be applied to individual instances. So make sure the security group you are working with is assigned to an instance!