まずは、composerを入れて、パッケージをダウンロード
[vagrant@localhost mail]$ curl -sS https://getcomposer.org/installer | php All settings correct for using Composer Downloading... Composer (version 1.6.3) successfully installed to: /home/vagrant/mail/composer.phar Use it: php composer.phar [vagrant@localhost mail]$ php composer.phar require phpmailer/phpmailer Using version ^6.0 for phpmailer/phpmailer
続いて、コードを書いていきます。mail->Passwordはgmailのアプリパスワード。
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'vendor/autoload.php';
$mail = new PHPMailer(true);
try{
$mail->SMTPDebug = 2;
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = 'fuga@gmail.com';
$mail->Password = 'google app pass';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->setFrom('fuga@gmail.com');
$mail->addAddress('fuga@gmail.com');
$mail->addCC('foo@hotmail.com');
$mail->addBCC('hoge@yahoo.co.jp');
$mail->addAttachment('test.gif');
$mail->isHTML(true);
$mail->Subject = 'Here we comes!';
$mail->Body = 'This is the HTML message body <b>in bold</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML';
$mail->send();
echo 'messeage has been sent';
} catch (Exception $e){
echo "Message could not be send, Mailer Error:", $mail->ErrorInfo;
}
なんじゃこりゃ、postfixとmb_send_mail()で頑張ってたのが、一瞬でできたぞ。











