【Laravel】Gmail のSMTPを利用してメール送付

なんとなくやる機会があったので
いちおう忘れないようにメモメモ

まずは composer で phpmailer をインストール

composer require phpmailer/phpmailer
GitHub - PHPMailer/PHPMailer: The classic email sending library for PHP
The classic email sending library for PHP. Contribute to PHPMailer/PHPMailer development by creating an account on GitHub.

あとはこんな感じ

use PHPMailer\PHPMailer\PHPMailer;

        $mail = new PHPMailer(true);

        $host = 'smtp.gmail.com';
        $userName = 'xxx@gmail.com';
        $password = 'xxxxx';

        $from = 'xxxxxx';
        $fromname = 'xxxxx';
        $to = 'xxxxxxxxx';

        $subject = '件名';
        $body = '本文';

        $mail->SMTPDebug = 2;
        $mail->isSMTP();
        $mail->SMTPAuth = true;
        $mail->Host = $host;
        $mail->Username = $userName;
        $mail->Password = $password;
        $mail->SMTPSecure = 'tls';
        $mail->Port = 587;
        $mail->CharSet = "utf-8";
        $mail->Encoding = "base64";
        $mail->setFrom($from, $fromname);
        $mail->addAddress($to);
        $mail->Subject = $subject;
        $mail->Body    = $body;

        $mail->send();

コメント

タイトルとURLをコピーしました