「Php/laravel/laravel5/mail」の版間の差分
提供: 初心者エンジニアの簡易メモ
(→設定) |
(→設定) |
||
| (同じ利用者による、間の1版が非表示) | |||
| 行7: | 行7: | ||
==設定== | ==設定== | ||
| − | + | パスはアプリ認証用に生成。 | |
| − | https:// | + | https://myaccount.google.com/apppasswords |
| + | |||
| + | 2段階認証は必須。 | ||
vi .env | vi .env | ||
2025年10月2日 (木) 18:38時点における最新版
未完成
gmailでメールを送る
事前設定として
gmailの設定/imapを無効から有効に
設定
パスはアプリ認証用に生成。 https://myaccount.google.com/apppasswords
2段階認証は必須。
vi .env MAIL_DRIVER=smtp MAIL_HOST=smtp.gmail.com MAIL_PORT=587 MAIL_ENCRYPTION=tls MAIL_FROM_ADDRESS=xxxxxxxx@gmail.com MAIL_FROM_NAME=xxxxxxxx MAIL_USERNAME=xxxxxxxx@gmail.com MAIL_PASSWORD=xxxxxxxxxxxxxxxx MAIL_PRETEND=false
vi config/mail.php
'from' => [
'address' => env('MAIL_FROM_ADDRESS', null),
'name' => env('MAIL_FROM_NAME', null)
],
実行
php artisan tinker
Mail::raw('Test Mail', function($message) { $message->to('xxxxxxxxxxxxxxxx@gmail.com')->subject('test'); });
以下が出れば成功
=> 1
以下エラーが出る場合はmail設定が読み込まれているか確認する
Swift_TransportException with message 'Expected response code 250 but got code "530", with message "530 5.7.1 Authentication required
mail設定読み込み確認
dd(\Config::get('mail'));
echo \Config::get('mail.username');
読み込まれてなければキャッシュなどをクリアなどしてみる
$ php artisan config:clear $ php artisan config:cache
envを利用しない場合
configファイルに直書き
vi config/mail.php
return [
'driver' =>'smtp',
'host' => 'smtp.gmail.com',
'port' => 587,
'from' => [
'address' => 'xxxxxxxx@gmail.com',
'name' => 'xxxxxxxx'
],
'encryption' => 'tls',
'username' => 'xxxxxxxx@gmail.com',
'password' => 'xxxxxxxxxxxxxxxx',
'sendmail' => '/usr/sbin/sendmail -bs',
];
テンプレを使う
resources/views/mails/regist.blade.phpのテンプレが適用される
\Mail::send('mails/regist', [], function ($m) {
$m->from('from@example.com', 'Your Application');
$m->to("to@example.com", "to")->subject('Your Reminder!');
});
