HOWTO: Sending emails from shell scripts in Debian Linux
Purpose: There are times when you would like to send out emails from your Linux machine using a shell script. Another example would be that suppose you install a web application like mediawiki or mantis on your machine and both of them have features like sending emails to the users upon registration, etc. In order for these application to send out email you should have your mail system configured properly in Debian otherwise these application will fail to send out emails.
Environments in which this method will work:
The method that I am going to highlight here will work in almost any kind of environment like:
1. Home LAN (ISP->Modem->Wireless Router) where your computer gets IP addresses like 192.168.0.1
2. Office LAN/Domain environment
3. Single computer connected directly to ISP modem
Fundamentals/ Basic Concept:
In order to send out emails from a Linux system you need the following to types of programs:
- MTA – Mail Transport/Transfer Agent – A kind of daemon/server
- MUA – Mail User Agent – An e-mail client which interacts with MTA
There are different kinds of MTA like exim4 (default), sendmail, etc. Similarly there are many MUA like bsd-mailx, sendemail (not sendmail), etc in Debian Lenny.
What we will use:
For this tutorial we will use the following software components:
sendmail – MTA
bsd-mailx – MUA
Why not Exim4?
You may ask since exim4 is the default MTA why not use that? Yes, you are right, but unfortunately I have not been able to get it working and it requires a lot of configuration and is a bit complex. The method I am going to show is very simple and you will be sending emails in minutes.
So let’s get started…
Step 1: Install bsd-mailx
The first step is to install MUA
# apt-get install bsd-mailx
This package will give you your “mail” command and also it will install exim4 as your default MTA which will get removed automatically in the next step
Step 2: Install sendmail
Now we need to install sendmail as your preferred MTA
# apt-get install sendmail-bin
As mentioned before this will remove exim4.
Step 3: Prepare a script to send email
We are almost there. The following sample script (email.sh) can be used to send emails
#!/bin/bash
echo "Sending mail..."
echo "This is the body of email" | mail -s "This is the Subject" test@abc.com
echo "Mail sent"
exit 0;
Finally execute the above script:
# sh email.sh
Go and check your mailbox and you should see an email waiting to be opened.
That’s it. Congratulations you have successfully configured your system to send emails.
Additional Notes:
a) Step 2 does remove the exim4 package but it does not remove it’s configuration files which is evident from the following command:
debian:~# dpkg -l | grep exim4
rc exim4 4.69-9 metapackage to ease Exim MTA (v4) installati
rc exim4-base 4.69-9 support files for all Exim MTA (v4) packages
rc exim4-config 4.69-9 configuration for the Exim MTA (v4)
rc exim4-daemon-light 4.69-9 lightweight Exim MTA (v4) daemon
debian:~#
The word “rc” indicates that the configuration files are still present. In order to keep our system clean we need to remove those files. The following step is *not* a required step but is highly recommended:
dpkg -l | grep exim4 | cut -d' ' -f3 | xargs dpkg --purge
b) Note this tutorial is only for those who would like to *send* emails. Receiving emails will require additional steps and configuration which I might cover later on if I find out how to do it. Alternatively you can suggest (in comments) how to do that if you know and I will make sure I will update the post and give you the credit..:)
c) If you are looking for sending attachments also along with the message then please see this post.
Happy emailing!


Email Subscription









October 14th, 2010 at 9:52 am
[...] usually use the sendmail program to send out emails from my Debian system. Recently I tried to do some changes to my /etc/mail/sendmail.mc file and [...]
October 16th, 2010 at 2:46 pm
[...] In one our previous post we saw how to send emails using shell script. In this blog post we will extend that concept and see how we can send [...]
September 7th, 2011 at 8:13 pm
Sweet! It worked. I’ve been looking all over for this. Everything I read just said to use the mail command, but nobody said which program to install to actually make that command available. But now it works. Thanks!
Reply to this comment
October 6th, 2011 at 5:51 pm
would this work instead for something like this:
Configuring the Email Server
The NGCP installer will install mailx (which has Exim4 as MTA as a default dependency) on the system, however the MTA is not configured by the installer. If you want to use the Voicemail-to-Email feature of the Voicebox, you need to configure your MTA properly. If you are fine to use the default MTA Exim4, execute the following command:
dpkg-reconfigure exim4-config
Depending on your mail setup in your environment (whether to use a smarthost or not), configure Exim accordingly. In the most simple setup, apply the following options when prompted for it:
General type of mail configuration: internet site; mail is sent and received directly using SMTP
System mail name: the FQDN of your server, e.g. ce.yourdomain.com
IP-addresses to listen on for incoming SMTP connections: 127.0.0.1
Other destinations for which mail is accepted: the FQDN of your server, e.g. ce.yourdomain.com
Domains to relay mail for: leave empty
Machines to relay mail for: leave empty
Keep number of DNS-queries minimal (Dial-on-Demand)? No
Delivery method for local mail: mbox format in /var/mail/
Split configuration into small files? No
[Important]
You are free to install and configure any other MTA (e.g. postfix) on the system, if you are more comfortable with that.
Reply to this comment
March 2nd, 2012 at 10:55 am
This proposed installation doesn’t work on my linux.voyage version 8.0.
I did put my own address in the email.sh script and I haven’t received any email form by Alix based platform.
Reply to this comment