If you are running a Debian server, you must be running some automated processes like database dump, log cleaning, data backup, …
When processing these batch treatments, it can be very convenient to get some completion reports sent by email.
Under debian, you can send mails thru command line with the help of sendmail and exim4 as a mail transfert agent. But you previously need to configure them to use an official smtp server.
This article will explain how to configure your Debian Wheezy server to be able to send emails from command line by using the smtp server provided with your Gmail account.
Configuration is given for a Gmail account, but it can obviously be adapted to any other smtp provider.
This article has been tested on a freshly installed Amd64 Debian Wheezy.
1. Configure Exim4
Exim4 is installed by default, but it needs to be reconfigured to setup a Gmail server configuration :
# dpkg-reconfigure exim4-config
You will ge thru a setup procedure. Make sure to set it up as follow :
|Configuration type||mail sent by smarthost; received via SMTP or fetchmail|
|System mail name||Any hostname like mail.yourdomain.net (localhost refused)|
|IP-addresses to listen on for incoming SMTP connections||127.0.0.1 (to refuse external connections)|
|Other destinations for which mail is accepted||leave empty|
|Machines to relay mail for||leave empty|
|IP address or host name of the outgoing smarthost||smtp.gmail.com:587|
|Hide local mail name in outgoing mail ?||no|
|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 ?||yes|
2. Declare your Gmail account
As Gmail is using authentification on its smtp servers, you need to declare your account:password for the gmail servers.
This is done in the file /etc/exim4/passwd.client where you have to declare any server gmail can use.
# password file used when the local exim is authenticating to a remote
# host as a client.
# see exim4_passwd_client(5) for more documentation
As Exim is quite touchy about the rights of its password files, it's better to explicitly setup proper ownership and permissions.
# chown Debian-exim:root /etc/exim4/passwd.client
# chmod 640 /etc/exim4/passwd.client
Finally, we need to reload exim4 for the setup to be used.
We will also configure it to do a delivery attempt for every message, whether frozen or not (-qff option).
# invoke-rc.d exim4 restart
# exim4 -qff
3. Send your first mail
Everything is now ready to send your first command line email.
To send your first mail, you now need to prepare the email body in a text file.
This email body should respect a specific structure that will provide :
- the Recipient (to:)
- the Sender (from:)
- the Subject (subject:)
- the mail text
from : Your server name
subject : Test mail
This is the first mail sent by my server's sendmail !
Now that the mail body is ready, you just need to pass it to sendmail with the -t option for it to send the mail :
# cat /root/mail-body.txt | sendmail -t
You should receive a mail like this one :
Hope it helps.