New FAMILUG

The PyMiers

Tuesday 18 February 2014

ssmtp: gửi gmail từ command line


Bài viết thực hiện trên:

$ lsb_release -r
Release:        12.04
Bạn có muốn post status FB qua CLI?
Chỉ cần vào tk FB, chọn setting => mobile, lấy cái email private đó. Gửi mail từ CLI đến email vừa lấy được => Xong con ong!

Để gửi mail (mặc định là Gmail) từ CLI, có nhiều cách, đơn giản nhất là cài ssmtp

$ whatis ssmtp
ssmtp (8)            - send a message using smtp
# cài ssmtp
$ apt-get install ssmtp -y


Sau khi cài xong cần cấu hình cho ssmtp ở /etc/ssmtp, cần sửa 2 file
revaliases và ssmtp.conf


/etc/ssmtp () $ tail -n +1 *
==> revaliases <==
# sSMTP aliases
#
# Format:       local_account:outgoing_address:mailhub
#
# Example: root:your_login@your.domain:mailhub.your.domain[:port]
# where [:port] is an optional port number that defaults to 25.
#

hvn:hvn@gmail.com:smtp.gmail.com:587

==> ssmtp.conf <==
#
# Config file for sSMTP sendmail
#
# The person who gets all mail for userids < 1000
# Make this empty to disable rewriting.
root=hvn

# The place where the mail goes. The actual machine name is required no
# MX records are consulted. Commonly mailhosts are named mail.domain.com
mailhub=smtp.gmail.com:587

# Where will the mail seem to come from?
#rewriteDomain=

# The full hostname
hostname=localhost
UseSTARTTLS=YES

# Are users allowed to set their own From: address?
# YES - Allow the user to specify their own From: address
# NO - Use the system generated From: address
FromLineOverride=YES
#UseTLS=True
AuthUser=hvn@gmail.com
AuthPass=xxxyyy
RewriteDomain=gmail.com

 Giờ chỉ việc gõ lệnh gửi mail:

 #user hvn
$ echo `date` | mail -s "TEST" hvnsweeting@gmail.com
sau khi lệnh chạy thành công tức là mail đã gửi.

để debug, thêm option -v cho command `mail`

$ echo `date` | mail -v -s "TEST" hvnsweeting@gmail.com
[<-] 220 xxxxxxxxxxxxx
[->] EHLO localhost
.....
[->] From:xxxxx
[->] Date: Tue, 18 Feb 2014 11:44:08 +0000
[->] To: hvnsweeting@gmail.com
[->] Subject: TEST
[->]
[->] Tue Feb 18 11:44:08 UTC 2014
[->] .
[<-] 250 2.0.0 Ok: queued as 740B53A00B5
[->] QUIT
[<-] 221 2.0.0 Bye

No comments:

Post a Comment