0
0

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.

PHP+postfix with Gmail on El Capitan

Last updated at Posted at 2016-05-06

OSX上でphp -S localhost:8080なんぞして、mb_send_mailなアプリを作ってたんですが、メール送信テストしたくてもまーなかなか上手く行かないもので。やっとの思いで一通したのでここに眠らせる。R.I.P.

ベースとなった環境

  • OSX El Capitan 10.11.4
  • postfix (mail_version = 2.11.0)
  • PHP 5.6.21 (cli) (built: Apr 29 2016 02:31:44)
    • これだけhomebrewによる。

用意する各種設定ファイル

  • postfix 設定ファイル
/etc/postfix/main.cf
# :
# : 前略
# :

# CUSTOM ########
# Minimum Postfix-specific configurations.
mail_owner = _postfix
setgid_group = _postdrop

myhostname = smtp.gmail.com
relayhost=[smtp.gmail.com]:587
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_sasl_tls_security_options = noanonymous
smtp_use_tls = yes
smtp_sasl_mechanism_filter = plain
  • postfix パスワードファイル (/etc/postfix/sasl_passwd)

    [smtp.gmail.com]:587 hoge@hoge.com:hogehogehoge
    
    • ここは、自分(送信元)のGoogleアカウントのメールアドレスとパスワードをコロンつなぎで記入する
    • Googleアカウントで二段階認証を使ってる場合、アプリパスワードを用意して使う必要がある
  • PHP 設定ファイル

    /usr/local/etc/php/5.6/php.ini
    ;
    ; 前略
    ;
    [mail function]
    ; For Win32 only.
    ; http://php.net/smtp
    SMTP = smtp.gmail.com
    ; http://php.net/smtp-port
    smtp_port = 587
    
    ; For Win32 only.
    ; http://php.net/sendmail-from
    sendmail_from = hoge@hoge.com
    
    ; For Unix only.  You may supply arguments as well (default: "sendmail -t -i").
    ; http://php.net/sendmail-path
    sendmail_path =/usr/sbin/sendmail -t -i
    
    ; Force the addition of the specified parameters to be passed as extra parameters
    ; to the sendmail binary. These parameters will always replace the value of
    ; the 5th parameter to mail().
    ;mail.force_extra_parameters =
    
    ; Add X-PHP-Originating-Script: that will include uid of the script followed by the filename
    mail.add_x_header = On
    
    • sendmail_from のところは、さきと同じくGoogleアカウントのアドレスを設定すること
    • homebrewでインストールしたPHPはこのパスに設定ファイルがある。
    • それ以外の方法でインストールしたPHPについては知らぬ!

設定ファイルを用意した上で、やるべきこと

  • postfix用のパスワードファイルをハッシュ化して、postfixを再起動する

    sudo postmap hash:/etc/postfix/sasl_passwd
    sudo postfix reload
    sudo postfix stop
    sudo postfix start
    

試す

sample.php
<?php
date_default_timezone_set('Asia/Tokyo');
mb_send_mail(
    "hoge@hoge.com",
    "test for postfix",
    "test for PHP+postfix with Gmail on El Capitan",
    "From: hoge@hoge.com"
);
?>
  • ここのFromは先のグーグルアカウントを使っておくこと。

余談

メール関連は面倒が多すぎるので、Mailgunみたいな外部サービスに投げたほうが健全だなぁとおもいます。まる

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?