LoginSignup
7
12

More than 5 years have passed since last update.

[postfix,sendmail] phpでメールを送信するまでのまとめ

Posted at

目的

プログラムでメールを送っている仕組みを整理したい。

本当の目的

qiitaで久しぶりに何か上げたかった

役者たち

  • php
    • 今回メール送信を行う言語
  • sendmail
    • プログラムでメール送信を行うときに内部で呼び出されるコマンド
  • postfix
    • メール送信の設定を行う何か(メールの送信元とか)

実験環境

OS: OSX ver 10.13.6
php: 7.1.16

メール送信プログラム

今回実行するコード(test.php)↓(メールアドレスは匿名)

<?php
$to = 'hogehoge@example.com';
$subject = "let's php mail";
$message = 'Can you see it?';
mb_send_mail($to, $subject, $message);

メールの送信元設定

メールは送信元がないと送れない(常識)
その設定をpostfixで行う

postfixでメールの送信元を設定

$ sudo vim /etc/postfix/sasl_password

今回はgmailを利用してメールを行う。
そのためには、gmailのアプリケーションパスワードが必要である。
アプリケーションパスワードの設定はこちら(注意:gmailに紐づくアカウントが2段階認証されてないとダメ)

sasl_password(メールを送信するエージェントをgmailに設定)

[smtp.gmail.com]:587 <ここにメールアドレス>:<ここにアプリケーションパスワード>

設定をpostfixに反映

$ sudo postmap /etc/postfix/sasl_password

試しにsendmailで送ってみる

$ sendmail hoge@example.com(送信先アドレス)
testhoge
.

phpで実行

$ php test.php

まとめ

メール送信するだけでも、色々疲れちゃった

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