https://pgmemo.tokyo/data/archives/1496.html
● Laravel で 「テキストメール」をシンプルに送信する
// テキストメール送信
$from_email = "shop@test.server.com";
$from_name = "ショップ名";
$mail_subject = "ホームページからお問い合わせがありました。";
$mail_content = "メールの本文です\nテスト";
$to_email = "customer@user.com";
\Mail::send([], [], function($message) use ($from_email, $from_name, $mail_subject, $mail_content, $to_email ) {
$message->from( $from_email, $from_name );
$message->to( $to_email );
$message->subject( $mail_subject );
$message->setBody($mail_content);
});
● Laravel で 「htmlメール」をシンプルに送信する
// htmlメール送信
$from_email = "shop@test.server.com";
$from_name = "ショップ名";
$mail_subject = "ホームページからお問い合わせがありました。";
$mail_content = "<h1>メールの本文です</h1>";
$to_email = "customer@user.com";
\Mail::send([], [], function($message) use ($from_email, $from_name, $mail_subject, $mail_content, $to_email ) {
$message->from( $from_email, $from_name );
$message->to( $to_email );
$message->subject( $mail_subject );
$message->setBody($mail_content, 'text/html');
});
plane textとhtml mailの違いは本文と
$message->setBody($mail_content, 'text/html');
の第2引数の部分の違いのようだ
メール送信方式は .env の値を参照しに行きます sendmail を使って送信する場合の設定
MAIL_FROM_ADDRESS=test@user.com
MAIL_FROM_NAME=サイト名
MAIL_DRIVER=sendmail
MAIL_HOST=localhost
● Laravel で メールの設定を動的に変更する
.env の メール設定を取得するには config() を使用します
$mailconfig = config('mail');
dump( $mailconfig );
メール設定を動的に変更する
$mailconfig = [];
$mailconfig['driver'] = 1;
$mailconfig['host'] = 2;
$mailconfig['port'] = 3;
$mailconfig['username'] = 4;
$mailconfig['password'] = 5;
$mailconfig['encryption'] = null;
// メール設定の変更
$mailconfig = config(['mail' => $mailconfig ]);