LoginSignup
3
6

More than 5 years have passed since last update.

Alpine LinuxでGmailにメール送信する方法(ついでにPHPからも)

Last updated at Posted at 2018-03-02

概要

Alpine Linuxを使ってmailコマンドを実行する
ついでにPHPからも送信する

参考にしたサイト

環境

項目 バージョン
Docker 17.12.0-ce
docker-compose 1.18.0
コンテナイメージ php7.2-alpine3.7

構成

$ tree
.
├── docker
│   ├── Dockerfile
│   └── ssmtp.conf
└── docker-compose.yml

Dockerfile
FROM php7.2-alpine3.7

RUN apk update \
 && apk add ssmtp mailx

COPY ssmtp.conf /etc/ssmtp/ssmtp.conf
ssmtp.conf
root=${自身のGmailメールアドレス}
mailhub=smtp.gmail.com:587
rewriteDomain=gmail.com
hostname=gmail.com
AuthUser=${自身のGmailメールアドレス}
AuthPass=${自分のGmailログインパスワード}
AuthMethod=LOGIN
UseSTARTTLS=YES
FromLineOverride=YES
docker-compose.yml
version: '3'
services:
  mail:
    container_name: mail
    build: ./docker
    image: mail:latest

事前作業

以下のURLから「安全性の低いアプリ」を許可する
https://myaccount.google.com/lesssecureapps
※即時反映されるわけではないので、1分程度時間を置いたほうが良い

手順

  • ビルド
$ docker-compose build
  • ashの場合
$ docker run --rm -it mail:latest ash
$ echo 'body' | mail -s 'subject' ${自身のGmeilメールアドレス}
  • PHPの場合
$ docker-compose run mail
php > mail("${自身のGmailメールアドレス}", "subject", "body");
  • Gmail宛てにメールが届いていることを確認

エラー

ログインできなかった場合、以下のようなエラーが出力される

sendmail: Authorization failed (534 5.7.14  https://support.google.com/mail/answer/xxxx xxxx.xxxx - gsmtp)
3
6
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
3
6