Send email from debian server using gmail

[Server] Send email from debian server using gmail

I am using a debian OS on a QNAP TS-419P NAS to backup data daily from website and workstation. Sometimes, I need to send email from the server to let myself know some task is finished or probably I need to send out email to customers. However, build a mail server with dynamic IP address is always not a good idea. Even if you have a static IP, you might be banned from the receiver’s mail server easily. Anyway, this article talks about using gmail server to send emails from your own server.

Under debian, you can send emails through command line with the help of sendmail and exim4 as a mail transfer agent. But before you can send out emails you need to configure them to use an smtp server.

  1. Configure exim4 using the command
dpkg-reconfigure exim4-config

Here are the options you need to choose:

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 (This refuses 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
Keep number of DNS-queries minimal (Dial-on-Demand) ?: no
Split configuration into small files ?yes
  1. Add your gmail account in /etc/exim4/passwd.client
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

Note, you can use your google app account as well, just replace your.account@gmail.com with your.account@your_domain.com. It works well!

Since you are put the plain-text password here, it’s a good idea to setup proper ownership and permissions to the password file:

chown Debian-exim:root /etc/exim4/passwd.client
chmod 640 /etc/exim4/passwd.client
  1. Now reload exim4:
update-exim4.conf
invoke-rc.d exim4 restart
exim4 -qff

Good, you are ready to send out the first email!

save the following to a txt file /tmp/myfirstmail.txt

to : your.receiver@gmail.com
from : The name display as sender's name
subject : Test mail

This is the first mail sent by my server’s sendmail !

and pass it to sendmail with -t option

cat /root/mail-body.txt | sendmail -t

You should receive the mail it send out.

Leave a Comment

This post is created on September 13, 2014 and last updated on January 22, 2024