LoginSignup
5
8

More than 3 years have passed since last update.

Gmail サーバ を使って Raspberry Pi から メール送信 (「安全性の低いアプリの許可」を使う方法)

Last updated at Posted at 2019-05-16

環境

  • Raspberry Pi 3 Model B
  • Raspbian Stretch Lite 2018-11-13 版

このほか、Gmailのアカウント設定を用意しておきます。

MTA として exim4 , 送信メールサーバとして smtp.gmail.com , ポート番号として 587 を使います。

smtpサーバとしてGmailを使う

Gmailのメールサーバを使ってメール送信をするためには、
「安全性の低いアプリの許可」を有効にする方法と
「2段階認証プロセス」を使う方法があります。

今回は簡単に動作確認を済ますため、
「安全性の低いアプリの許可」を有効にする方法を使いました。

動作確認が終わったら「2段階認証プロセス」を使う方法に変更するのが推奨です。


sudo apt-get install exim4
sudo dpkg-reconfigure exim4-config

として、Mail Configurationで設定していく。

## General type of mail configuration: 

mail sent by smarthost; received via SMTP or fetchmail

を選ぶ。


## System mail name:   
                                                                                      利用者のドメイン。今回は nanbu.com とした。

## IP-addresses to listen on for incoming SMTP connections:                                                       

は127.0.0.1 ; ::1のまま


## Other destinations for which mail is accepted:                                                                 

は空白にする。


## Machines to relay mail for:   

は空白にしてみた。


## IP address or host name of the outgoing smarthost:       

は smtp.gmail.com::587 (実際はここで間違えて smtp.gmail.com:587 と入力したらしい)

というように設定が続いていく。しばらくデフォルトで進めていって、


## Root and postmaster mail recipient:     

は管理者として自分のメールアドレスを入れた

メール送信テスト(設定ミスで失敗編)


 echo "Test" | mail -s "test" (送信先メールアドレス)

としてメールが届いたか確認してみる。
うーん、届かない? ログ ( /var/log/exim4/mainlog ) を見てみる。


2019-03-08 19:37:07 1h2CsF-0000IJ-AJ <= pi@raspberrypi U=pi P=local S=367
2019-03-08 19:37:07 1h2CsF-0000IJ-AJ H=gmail-smtp-msa.l.google.com [2404:6800:4008:c06::6c] Network is unreachable
2019-03-08 19:37:07 1h2CsF-0000IJ-AJ H=gmail-smtp-msa.l.google.com [64.233.188.109] No route to host
2019-03-08 19:37:07 1h2CsF-0000IJ-AJ H=587 [0.0.2.75] Invalid argument
2019-03-08 19:37:07 1h2CsF-0000IJ-AJ == (送信先メールアドレス)R=smarthost T=remote_smtp_smarthost defer (22): Invalid argument

うーん。そもそも設置しているネットワークから gmail の SMTP サーバにアクセス可能だろうか。ファイアウォールなどで遮断されていないかどうかチェック。


pi@raspberrypi:~ $ sudo nmap gmail-smtp-msa.l.google.com

Starting Nmap 7.40 ( https://nmap.org ) at 2019-03-08 19:41 JST
Nmap scan report for gmail-smtp-msa.l.google.com (64.233.188.109)
Host is up (0.00033s latency).
Other addresses for gmail-smtp-msa.l.google.com (not scanned): 2404:6800:4008:c06::6c
rDNS record for 64.233.188.109: tk-in-f109.1e100.net
Not shown: 998 filtered ports
PORT    STATE SERVICE
80/tcp  open  http
587/tcp open  submission

Nmap done: 1 IP address (1 host up) scanned in 200.27 seconds

空いてますね

設定を見なおしてみます。



$ cat /etc/exim4/update-exim4.conf.conf 
# /etc/exim4/update-exim4.conf.conf
#
# Edit this file and /etc/mailname by hand and execute update-exim4.conf
# yourself or use 'dpkg-reconfigure exim4-config'
#
# Please note that this is _not_ a dpkg-conffile and that automatic changes
# to this file might happen. The code handling this will honor your local
# changes, so this is usually fine, but will break local schemes that mess
# around with multiple versions of the file.
#
# update-exim4.conf uses this file to determine variable values to generate
# exim configuration macros for the configuration file.
#
# Most settings found in here do have corresponding questions in the
# Debconf configuration, but not all of them.
#
# This is a Debian specific file

dc_eximconfig_configtype='smarthost'
dc_other_hostnames=''
dc_local_interfaces='127.0.0.1 ; ::1'
dc_readhost=''
dc_relay_domains=''
dc_minimaldns='false'
dc_relay_nets=''
dc_smarthost='smtp.gmail.com:587'
CFILEMODE='644'
dc_use_split_config='false'
dc_hide_mailname='false'
dc_mailname_in_oh='true'
dc_localdelivery='mail_spool'
pi@raspberrypi:~ $ 

ということで、

dc_smarthost='smtp.gmail.com::587'

が正しいのですが、

::が:になってました。

修正して、


sudo update-exim4.conf

で反映します。

メール送信テスト(本番編)

これを直して、再度


 echo "Test" | mail -s "test" (自分のメールアドレス)

とすると、

image.png

「重大なセキュリティ通知」として「ログインをブロックしました」というメールが飛んできます。
今回は「安全性の低いアプリの許可」を有効にする方法を使うので、Googleアカウント画面で以下のように有効にしました。
image.png

受信できました。

image.png

参考にしたページ

ayame.space「Raspberry Pi (Raspbian)でメール送信できるようにする」
https://ayame.space/2017/06/setup-send-email-by-exim4-for-raspberry-pi/

5
8
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
5
8