Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1790050
  • 博文数量: 297
  • 博客积分: 285
  • 博客等级: 二等列兵
  • 技术积分: 3006
  • 用 户 组: 普通用户
  • 注册时间: 2010-03-06 22:04
个人简介

Linuxer, ex IBMer. GNU https://hmchzb19.github.io/

文章分类

全部博文(297)

文章存档

2020年(11)

2019年(15)

2018年(43)

2017年(79)

2016年(79)

2015年(58)

2014年(1)

2013年(8)

2012年(3)

分类: LINUX

2016-11-30 14:37:52

轉自:

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 :

Option Choice 
 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 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.

/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'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).

# 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 :
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 :

debian-sendmail-message

 

Hope it helps.

阅读(1396) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~