Send Email from a Virtual Server in DkIT

Tutorial Difficulty Level    

The Cloud Virtualisation Platform we have built at https://xoa.comp.dkit.ie is a fantastic resource, comparable to AWS, Digital Ocean etc but yet free and importantly, secure because  it is inside the DkIT Firewall. However, this same security can prevent useful features most server can do easily, like sending email.

This tutorial details how to configure your virtual server in DkIT to send email via the Microsoft Outlook Mail Server (which is actually the other side of the firewall).

Log on to Xen Orchestra with your DkIT credentials and fire up a server in the usual manner. We are going to use an Ubuntu 18.04 Quick Instance for this tutorial.

Once it is up and running, login with the default credentials we use for all student servers (ask your lecturer if unsure) and install 2 packages, postfix and mailutils.  The first is a free and open-source mail transfer agent that routes and delivers electronic mail.  The second is the swiss army knife of electronic mail handling. It offers a rich set of utilities and daemons for processing e-mail, and will help us test that we can indeed send mail via postfix before trying to send mail from our applications (eg. using the PHP mail() function).

During installation, you will presented with a screen like this:

Use tab to highlight <OK> and press return to continue.  On the next screen, select “Satellite system”

The “mail name” can be whatever you want, including the default value.

However, the “SMTP relay host” on the next screen must be “10.108.150.26”. This a mail gateway in the Carrolls Building that is allowed to route mail on your behalf to Microsoft Outlook (it might still be marked as spam, but at least it will get to it’s destination).

Once the package finishes installing, we can move on to testing.

Tip: if you ever need to run the postfix setup again, use the following:

Remember the mailutils we installed earlier? We can now use the mail program that comes that package to test postfix is able to send mails.

Note: The gateway server at 10.108.150.26 only allows mails to be sent to an @student.dkit.ie mail account, and will append a footer stating it is not a “real” email. It also prefers mails to be sent FROM a student account (or at least appear to be… more on this in a bit).

The simplest way to use the mail command is to echo a string and pipe it into mail with the correct arguments. Here is an example:

“Just testing” is the email contents. Replace the “subject” string with your own subject line for the email. You won’t see any output from this command, the mail is just sent (or not) in the background. If you want to see if you mail was indeed sent successfully, you need to tail the mail log file on your system:

That looks good, the main thing being that status=sent appears in the log (if it was rejected by the gateway for any reason we would likely see status=bounced)

Logging in to your student email, you may notice the mail still has not arrived (if it has, count yourself very lucky). The reason for this is a combination of factors, all boiling down to the same thing – Microsoft Outlook, which our gateway relayed the mail on to, thinks your mail is spam, and with good reason too. The mail came from an unknown, newly created server on the network, that appears to be not part of the dkit.ie domain! It’s also from an odd looking email address, made up of the Linux username and the server hostname – in this case root@ubuntu

After a few hours you will likely get an email from quarantine@messaging.microsoft.com informing you of the suspected spam. The contents will be similar to this:

What you can do is “Release to Inbox” to get your mail, and also “Report as Not Junk” to (attempt to) allow all similar mails from your VM bypass quarantine in future no guaranteed here however). When you come to writing your web application, try playing with the headers, FROM field etc to make you notification emails appear more genuine, we have simply ensured mail CAN leave our server and DOES get sent to your student email account. We can’t do much at this point to stop spam filters putting it in quarantine.

By way of example, try installing PHP

and creating a simple test file test-email.php

Run this script with

Check it out  – this mail arrives in your student mailbox almost immediately, completely bypassing the spam filter! You can now start writing your web application and know that you will be able to send mail from it 🙂

But wait…. is it true to say that specifying a FROM email address in the header of the mail is enough to bypass the spam filters? Surely the mail function at the command line can include this? Try this at the command line:

Why didn’t we lead with this? Well, in the immortal words of Jim Carrey as the Riddler in Batman Forever“then you wouldn’t have learned nuthin’, would ya?”