Bài 16B: MSMTP

MSMTP

msmtp là một smtp client

M1. Cài đặt

apt-get install msmtp msmtp-mta

msmtp-mta giả lập msmtp như ứng dụng sendmail của hệ thống

M2. Soạn thảo file cấu hình

Có 2 file cấu hình ở mức hệ thống và ở mức user, xem thí dụ ở /usr/share/doc/msmtp/examples/ msmtprc-system.example/usr/share/doc/msmtp/examples/ msmtprc-user.example

+ File cấu hình user ~/.msmtprc có dạng sau

# Set default values for all following accounts.
defaults
auth           on
tls            on
tls_trust_file /etc/ssl/certs/ca-certificates.crt
logfile        ~/.msmtp.log
# Gmail
account        gmail
host           smtp.gmail.com
port           587
from           username@gmail.com
user           username
password       plain-text-password

# Mailgun
account        mailgun
host           smtp.mailgun.org
port           587
from           username@subdomain.mailgun
user           username
password       plain-text-password
# Set a default account 
account default : gmail

+ /etc/aliases

M3. chmod file cấu hình system và user

chmod 0644 /etc/msmtprc

File cấu hình user chỉ được phép đọc/ghi bởi owner

chmod 600 ~/.msmtprc

M4. Gởi mail

  • Gởi từ dòng lệnh
echo -e "Subject: Test Mail\r\n\r\nThis is a test mail" |msmtp --debug --from=default -t username@gmail.com
  • Gởi từ PHP

Mở file /etc/php5/fpm/php.ini, sửa dòng sendmail_path thành

sendmail_path = '/usr/bin/msmtp -t

Khởi động lại PHP và dùng hàm mail của PHP để gới mail

Chú thích:

Mã hóa mật khẩu

Trong file cấu hình msmtprc, thay vì dùng

password plain-text-password

có thể dùng

passwordeval gpg -d ~/.msmtp-gmail.gpg

trong đó mật khẩu đã được mã hóa trong file msmtp-gmail.gpg bằng cách dùng gpg.

+ Trước hết khởi tạo encryption key bằng lệnh (chỉ cần làm một lần)

gpg --gen-key

lần lượt gõ name, emailcomment (tự đặt ra) khi được hỏi.

Please select what kind of key you want:
   (1) RSA and RSA (default)
   (2) DSA and Elgamal
   (3) DSA (sign only)
   (4) RSA (sign only)
Your selection? -> Enter
RSA keys may be between 1024 and 4096 bits long.
What keysize do you want? (2048) -> Enter
Requested keysize is 2048 bits
Please specify how long the key should be valid.
         0 = key does not expire
      <n>  = key expires in n days
      <n>w = key expires in n weeks
      <n>m = key expires in n months
      <n>y = key expires in n years
Key is valid for? (0) -> Enter
Key does not expire at all
Is this correct? (y/N) y

You need a user ID to identify your key; the software constructs the user ID
from the Real Name, Comment and Email Address in this form:
    "Heinrich Heine (Der Dichter) <heinrichh@duesseldorf.de>"

Real name: RPI-11
Email address: lnt@lyle.info
Comment: Ly Anh Tuan
You selected this USER-ID:
    "RPI-11 (Ly Anh Tuan) <mailman@lyle.info>"

Change (N)ame, (C)omment, (E)mail or (O)kay/(Q)uit? o
You need a Passphrase to protect your secret key.

We need to generate a lot of random bytes. It is a good idea to perform some other action (type on the keyboard, move the mouse, utilize the disks) during the prime generation; this gives the random number
generator a better chance to gain enough entropy.

Không cần nhập passphrase, enter cho qua.

Sau khi gõ O (OK), liên tục di chuyển chuột hay gõ phím để chương trình có thể lấy đủ các số ngẫu nhiên dùng tạo key. Cuối cùng, màn hình kết quả tương tự như sau

gpg: /root/.gnupg/trustdb.gpg: trustdb created
gpg: key 0782ACA7 marked as ultimately trusted
public and secret key created and signed.

gpg: checking the trustdb
gpg: 3 marginal(s) needed, 1 complete(s) needed, PGP trust model
gpg: depth: 0  valid:   1  signed:   0  trust: 0-, 0q, 0n, 0m, 0f, 1u
pub   2048R/0782ACA7 2014-10-13
      Key fingerprint = CB79 2A41 EC30 0690 4B5C  66EC FD26 4A69 0882 ADA7
uid                  RPI-11 (Ly Anh Tuan) <lnt@lyle.info>
sub   2048R/15BC3BEE 2014-10-13

+ Ghi nhớ uid và thay vào uid trong câu lệnh mã hóa mật khẩu sau đây, lần lượt mã hóa tất cả mật khầu email với tên file .gpg khác nhau

echo -e "password_cần_mã_hóa\n" | gpg -r "uid" --encrypt -o .msmtp-gmail.gp

Leave a Comment

Filed under Software

Leave a Reply

Your email address will not be published. Required fields are marked *