# 在 Ubuntu 18.04 透過 postfix 來使用 Gmail 寄信
###### tags: `Ubuntu`
參考 https://kifarunix.com/configure-postfix-to-use-gmail-smtp-on-ubuntu-18-04/
## 首先安裝一下 postfix 與 mailutils
`sudo apt install postfix mailutils`
安裝過程設定使用 Internet site: Mail is sent and received directly using SMTP.
## 安裝後修改 /etc/postfix/main.cf 加入
```
relayhost = [smtp.gmail.com]:587
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_sasl_security_options = noanonymous
smtp_tls_security_level = encrypt
smtp_tls_CAfile = /etc/ssl/certs/ca-certificates.crt
```
## 修改 /etc/postfix/sasl_passwd 加入密碼
```[smtp.gmail.com]:587 userid@gmail.com:password```
應用程式密碼可以在 https://myaccount.google.com/apppasswords 產生
## 使用 postmap 產生 db
`$ sudo postmap /etc/postfix/sasl_passwd`
```
$ ls /etc/postfix/sasl_passwd*
/etc/postfix/sasl_passwd /etc/postfix/sasl_passwd.db
```
## 變更 /etc/postfix/sasl_passwd* 權限
`$ sudo chown root:root /etc/postfix/sasl_passwd*`
`$ sudo chmod 600 /etc/postfix/sasl_passwd*`
## 設定完成後重啟 postfix
`$ service postfix reload`
## 最後寫一封信寄給自己看看有沒有成功收到
`$ echo "This is a test mail." | mail -s "test mail" userid@gmail.com`