Created
August 6, 2018 01:09
-
-
Save sola-msr/de77757ca3608f30334cfa6881c259ba to your computer and use it in GitHub Desktop.
【Lumen(Laravel)】Lumenでメールを送信するのにあえてPHPMailerを使用する ref: https://qiita.com/sola-msr/items/417965108e9ebb53b2bb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
MAIL_HOST= | |
MAIL_PORT= | |
MAIL_USERNAME= | |
MAIL_PASSWORD= | |
MAIL_ENCRYPTION=tls | |
[email protected] | |
MAIL_FROM_NAME=System | |
MAIL_TO_ADDRESS= | |
MAIL_TO_NAME= | |
MAIL_CC_01_ADDRESS= | |
MAIL_CC_01_NAME= | |
MAIL_CC_02_ADDRESS= | |
MAIL_CC_02_NAME= |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
composer require phpmailer/phpmailer |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use PHPMailer\PHPMailer; | |
// ~~ 略 ~~ | |
/** | |
* メールを送信する | |
* | |
* @param string $subject メールタイトル | |
* @param string $body メール本文 | |
* @return void | |
*/ | |
public function sendMail(string $subject, string $body) | |
{ | |
$mail = new PHPMailer\PHPMailer(); | |
$mail->isSMTP(); | |
// $mail->SMTPDebug = 1; | |
$mail->SMTPAuth = true; | |
$mail->Host = env('MAIL_HOST'); // メインのSMTPサーバーを指定する | |
$mail->Port = env('MAIL_PORT'); // 接続するTCPポート | |
$mail->Username = env('MAIL_USERNAME'); // SMTPユーザー名 | |
$mail->Password = env('MAIL_PASSWORD'); // SMTPパスワード | |
$mail->SMTPSecure = env('MAIL_ENCRYPTION'); // TLS暗号化 | |
// メール内容設定 | |
$mail->CharSet = "UTF-8"; | |
$mail->Encoding = "base64"; | |
//FROM用メールアドレスと宛先名 | |
$mail->setFrom(env('MAIL_FROM_ADDRESS'), env('MAIL_FROM_NAME')); | |
// TO用メールアドレスと宛先名 | |
$mail->addAddress(env('MAIL_TO_ADDRESS'), env('MAIL_TO_NAME')); | |
// CC用メールアドレスと宛先名 | |
$mail->addCC(env('MAIL_CC_01_ADDRESS'), env('MAIL_CC_01_NAME')); | |
$mail->addCC(env('MAIL_CC_02_ADDRESS'), env('MAIL_CC_02_NAME')); | |
$mail->Subject = $subject; | |
// HTMLフォーマットの有効 | |
//$mail->isHTML(true); | |
$mail->Body = $body; | |
if ($mail->send() === false) { | |
echo "Mail sending failed!! Mailer Error: {$mail->ErrorInfo}" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment