2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

raspbian(rpi0w)にpostfixを入れて、gmail経由でメール送信する

Last updated at Posted at 2019-04-08

自宅サーバからアラートメールを出したいので、gmailを送信サーバとして利用し、外部(プロバイダ)宛にメールを出す。

gmailで送信
gmailで安全性の低いアプリを許可する

install

sudo apt-get install postfix
sudo apt-get install mailutils
sudo apt-get install sasl2-bin libsasl2-modules

saslauthd

vi /etc/default/saslauthd

START=yes

configuration-postfix

vi /etc/postfix/main.cf

myhostname = xxxx.yourdomaim     # アラートを出すサーバ
mydomain = yourdomain
alias_maps = hash:/etc/aliases
alias_database = hash:/etc/aliases
myorigin = /etc/mailname
mydestination = $myhostname, xxxx, localhost.localdomain, localhost.$mydomain    # このあて先には、ローカルメールとして出す
mynetworks = 127.0.0.0/8 192.168.0.0/24  # NWアドレスは適宜変更
mailbox_size_limit = 0
recipient_delimiter = +
inet_interfaces = all
inet_protocols = ipv4
# default_transport = error
# relay_transport = error
home_mailbox = Maildir/  # ローカルメールの配信先ディレクトリ

relayhost = [smtp.gmail.com]:587  # gmail経由で送信、送信サーバ
smtpd_sasl_auth_enable = yes
smtpd_sasl_local_domain = $myhostname
smtpd_relay_restrictions =
 permit_mynetworks
 permit_sasl_authenticated
 defer_unauth_destination

smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_sasl_security_options = noanonymous
smtp_sasl_tls_security_options = noanonymous
smtp_sasl_mechanism_filter = plain
#tls setting
smtp_use_tls = yes

vi /etc/postfix/master.cf

smtp       inet  n       -       y       -       -       smtpd
#smtp      inet  n       -       y       -       1       postscreen
#smtpd     pass  -       -       y       -       -       smtpd
#dnsblog   unix  -       -       y       -       0       dnsblog
#tlsproxy  unix  -       -       y       -       0       tlsproxy
submission inet n       -       y       -       -       smtpd  # コメント削除
#  -o syslog_name=postfix/submission
#  -o smtpd_tls_security_level=encrypt
  -o smtpd_sasl_auth_enable=yes  # コメントを削除

vi ~/.profile

export MAIL=$HOME/Maildir

sasl_passwd

vi /etc/postfix/sasl_passwd

GmailのIDとパスワードを設定

[smtp.gmail.com]:587 yyyy@gmail.com:yyyyyyyyyy

設定を更新したら、DBも更新

postmap hash:/etc/postfix/sasl_passwd

aliases

vi /etc/aliases

alias:  yyy@gmail.com

設定を更新したら、DBも更新

newaliases 

postfix mail command

送信

## alias指定でメール
echo “test mail” | mail alias
 hogehoge
 foo foo
.
EOT(Ctrl+d)

## アドレス指定でメール
echo “test mail” | mail yyyy@gmail.com
 hogehoge
 foo foo
.
EOT(Ctrl+d)

受信

vi /etc/skel/.bash_profile

export MAIL=$HOME/Maildir

mail client

 $ mail
 > nn       nnのメールを読む
 > d nn     nnのメールを削除
 > d *      ワイルドカード

queue操作

want to do command
キューの確認 postqueue -p
mailq
キュー削除 postsuper -d キューID
postsuper -d ALL
強制配信 postfix flush
2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?