轉自:http://bernaerts.dyndns.org/linux/75-debian/278-debian-sendmail-gmail-account

If you are running a Debian server, you must be running someautomated 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 :

OptionChoice 
 Configuration type mail sent by smarthost; received via SMTP or fetchmail
 System mail name localhost
 IP-addresses to listen on for incoming SMTP connections 127.0.0.1 (to refuse external connections)
Other destinations for which mail is acceptedleave empty
Machines to relay mail forleave empty
IP address or host name of the outgoing smarthostsmtp.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 mailmbox 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.

/etc/exim4/passwd.client

# password file used when the local exim is authenticating to a remote
# host as a client.
#
# see exim4_passwd_client(5) for more documentation
#
# Example:
### target.mail.server.example:login:password
gmail-smtp.l.google.com:your.account@gmail.com:yourpassword
*.google.com:your.account@gmail.com:yourpassword
smtp.gmail.com:your.account@gmail.com:yourpassword

As Exim is quite touchy about the rights of its password files, it'sbetter 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).

# update-exim4.conf
# 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 :

  1. the Recipient (to:)
  2. the Sender (from:)
  3. the Subject (subject:)
  4. the mail text

/root/mail-body.txt

to : your.account@gmail.com
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.

10-12 10:44
查看更多