Help us understand the problem. What is going on with this article?

【サルが書く】CakePHP3でGmailアカウントを使って簡単にメールを送ろう

More than 1 year has passed since last update.

GmailのSMTPサーバを使って、CakePHP3でメールを送る設定をするよ。

アプリケーションのEmailTransportを設定しよう

applicationのconfig/app.phpにEmailTransportとういう項目がある。
そこを設定することで、Gmailでのメール送信を実装できる。

app/config/app.php
    'EmailTransport' => [
        'default' => [
            'className' => 'Smtp',
            'host' => 'smtp.gmail.com',
            'port' => 587,
            'timeout' => 30,
            'username' => 'Gmailのメールアドレス',
            'password' => 'Gmailのパスワード',
            'tls' => true,
            'url' => env('EMAIL_TRANSPORT_DEFAULT_URL', null),
        ],
    ],

Controllerでメールを送る設定をしよう

hogeController.php
// クラスをよみこむ
use Cake\Mailer\Email;

    public function beforeFilter()
    {
        $this->email = new Email('default');
    }

    public function send()
    {
        $this->email->setFrom(['送り元メールアドレス' => '送り元名'])
            ->setTo('送り先メールアドレス')
            ->setSubject('表題')
            ->send('本文');
    }

CakePHP3.4.0で仕様が変わりました: 代わりに setFrom() 、 setTo() 、 setCc() 、 setBcc() そして、 setSubject() を使用してください。

送れないと思ったら・・・

Gmail側の設定で安全性の低いアプリからのアカウントへのアクセスを許可するの設定が必要です。

https://support.google.com/accounts/answer/6010255

参考文献

https://hacknote.jp/archives/27182/
https://qiita.com/frost_star/items/4a56cec1f5de5eccbd09

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした