Send Email from a Virtual Server in DkIT

Watch out! This tutorial is over 6 years old. Please keep this in mind as some code snippets provided may no longer work or need modification to work on current systems.
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).

sudo apt-get install postfix mailutils

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:

sudo dpkg-reconfigure postfix

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:

echo "Just testing" | mail -s "subject" d0001234@student.dkit.ie

“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:

administrator@ubuntu:~$ tail -f /var/log/mail.log 
Nov  1 15:58:18 ubuntu postfix/pickup[6150]: 8CC42A363E: uid=1000 from=<administrator@ubuntu>
Nov  1 15:58:18 ubuntu postfix/cleanup[6169]: 8CC42A363E: message-id=<20181102112818.8CC42A363E@ubuntu.dkit.ie>
Nov  1 15:58:18 ubuntu postfix/qmgr[6151]: 8CC42A363E: from=<administrator@ubuntu>, size=357, nrcpt=1 (queue active)
Nov  1 15:58:18 ubuntu postfix/smtp[6171]: 8CC42A363E: to=<d0001234@student.dkit.ie>, relay=10.108.150.26[10.108.150.26]:25, delay=0.02, delays=0.01/0/0/0.01, dsn=2.0.0, status=sent (250 OK id=1gIXcg-0006bR-Iz)
Nov  1 15:58:18 ubuntu postfix/qmgr[6151]: 8CC42A363E: removed

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

sudo apt-get install PHP

and creating a simple test file test-email.php

<?php 
    ini_set( 'display_errors', 1 );
    error_reporting( E_ALL );
    $from = "d0001234@student.dkit.ie"; //replace with YOUR email
    $to = "d0001234@student.dkit.ie"; //replace with YOUR email
    $subject = "PHP Mail Test script";
    $message = "This is a test to check the PHP Mail functionality";
    $headers = "From:" . $from;
    mail($to,$subject,$message, $headers);
    echo "\nTest email sent\n\n";
?>

Run this script with

php test-email.php

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:

echo "You should get this!" | mail -s "hello world" -aFrom:d0001234@student.dkit.ie d0001234@student.dkit.ie

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?”