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
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
echo "Sending mail..."
echo "This is the body of email" | mail -s "This is the Subject" email@example.com
echo "Mail sent"
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.
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
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.