If you’re using bash scripts to automate tasks on your Ubuntu desktop but don’t want to have to wade through logs to find out what’s happening, you can setup a Local Only SMTP server and have the scripts mail you their output.
Being Linux, there are several ways to do this.
What follows is how I managed to set this up on my Ubuntu 24_04 desktop (minus all the mistakes and swearing).
Specifically we’ll be looking at :
installing Dovecot
installing and configuring Postfix
installing mailx
configuring Thunderbird to handle local emails
As AI appears to be the “blockchain du jour” I thought I should make some attempt to appear up-to-date and relevant. Therefore, various AI entities from Ian M. Banks’ Culture will be making an appearance in what follows…
Software Versions
It’s probably useful to know the versions I’m using for this particular exercise :
Ubuntu 24.04.1 LTS
Thunderbird 128.10.0esr (64-bit)
Postfix 3.8.6
Dovecot 2.3.21
mailx 8.1.2
If you happen to have any of these installed already, you can check the version of Postfix by running…
…Dovecot by running..
…and mailx by running :
The Ubuntu version can be found under Settings/System/About
The Thunderbird version can be found under Help/About.
Before we get to installing or configuring anything, we need to…
Specify a domain for localhost
We need a domain for Postfix. As wer’re only flinging traffic around the current machine, it doesn’t have to be known to the wider world, but it does need to point to localhost.
So, in the Terminal :
…and edit the line for 127.0.0.1 so it includes your chosen domain name.
Conventionally this is something like localhost.com, but “It’s My Party and I’ll Sing If I Want To”…
Once we’ve saved the file, we can test the new entry by running :
Install Dovecot
Simply install the package by running :
The installation will complete automatically after which, in this instance, we don’t need to do any configuration at all.
Install and configure Postfix
You can install postfix by running :
As part of the installation you will be presented with a configuration menu screen :
Using the arrow keys, select Local Only then hit Return
In the next screen set the domain name to the one you setup in /etc/hosts ( in my case that’s culture.org ).
Hit Return and the package installation will complete.
Note that if you need to re-run this configuration you can do so by running :
Next we need to create a virtual alias map, which we do by running :
and populating this new file with two lines that look like :
In my case the lines in the file are :
Now we need to tell Postfix to read this file so :
…and add this line at the bottom of the file :
To activate the mapping :
…and restart the postfix service…
Once that’s done, we can confirm that the postfix service is running :
Installing mailx
As with dovecot, we don’t need to do any more than install the package :
Testing the Configuration
Now we have mailx, we can test our configuration :
To check if we’ve received the email, run :
Actually having to cat the inbox seems a lot of effort.
If I’m going to be on the receiving end of condescending sarcasm from my own laptop I should at least be able to read it from the comfort of an email client.
Thunderbird
Currently, Thunderbird is the default mail client in Ubuntu and comes pre-installed.
If this is the first time you’ve run Thunderbird, you’ll be prompted to setup an account. If not then you can add an account by going to the “hamburger” menu in Thunderbird and selecting Account Settings. On the following screen click the Account Actions drop-down near the bottom of the screen and select Add Mail Account :
Fill in the details (the password is that of your os account) :
…and click Continue.
Thunderbird will now try and pick an appropriate configuration. After thinking about it for a bit it should come up with something like :
…which needs a bit of tweaking, so click the Configure Manually link and make the following changes :
Incoming Server
Outgoing Server
If you now click the Re-test button, you should get this reassuring message :
If so, then click Done.
You will be prompted to add a security exemption for the Incoming SMTP server
Click Confirm Security Exception
NOTE – you may then get two pop-ups, one after the other, prompting you to sign in via google.com. Just dismiss them. This shouldn’t happen when you access your inbox via Thunderbird after this initial configuration session.
You should now see the inbox you’ve setup, complete with the message sent earlier :
You should also be able to read any mail sent to mike@localhost. To test this :
Incidentally, the first time you send a mail, you’ll get prompted to add a security exception for the Outgoing Mail server. Once again, just hit Confirm Security Exception.
Once you do this, you’ll get a message saying that sending the mail failed. Dismiss it and resend and it will work.
Once again, this is a first-time only issue.
After a few seconds, you’ll see the mail appear in the inbox :
As you’d expect, you can’t send mail to an external email address from this account with this configuration :
Sending an email from a Shell Script
The whole point of this exercise was so I could get emails from a shell script. To test this, I’ve created the following file – called funny_it_worked_last_time.sh
If I make this script executable and then run it :
…something should be waiting for me in the inbox…
Further Reading
Part of the reason for writing this was because I couldn’t find one place where the instructions were still applicable on the latest versions of the software I used here.
The links I found most useful were :
This askUbuntu question
This very useful GitHub gist by Rael Gugelmin Cunha
Finally, for those of a geeky disposition, here’s a list of Culture space craft.