Memo

メモ > サーバ > 各論: エトセトラ > PHPMailerを使ってSMTPでメールを送信する

■PHPMailerを使ってSMTPでメールを送信する
SMTPでの送信を一から書くのは大変なので、PHPMailerなどを使うといい ローカルのXAMPPで常に接続エラーになる場合、適当なレンタルサーバに設置して試すと大丈夫なことがある 以下ページ内の「v5.2.28」からダウンロードすると、class.phpmailer.php と class.smtp.php を入手できる 以降のメモでは、このファイルを使用している Tags - PHPMailer/PHPMailer https://github.com/PHPMailer/PHPMailer/tags ■外部のSMTPで送信
<?php require_once 'PHPMailer/class.phpmailer.php'; require_once 'PHPMailer/class.smtp.php'; // SMTPサーバ: ホスト define('SMTP_HOST', 'smtp.refirio.net'); // SMTPサーバ: メールアカウント define('SMTP_USERNAME', 'example@refirio.net'); // SMTPサーバ: メールパスワード define('SMTP_PASSWORD', 'xxxxxxxxxx'); // SMTPサーバ: プロトコル (ssl または tls) define('SMTP_SECURE', 'ssl'); // SMTPサーバ: 送信ポート (ssl:465, tls:587) define('SMTP_PORT', '465'); // メール送信準備 $mail = new PHPMailer(); $mail->isSMTP(); $mail->SMTPAuth = true; $mail->Host = SMTP_HOST; $mail->Username = SMTP_USERNAME; $mail->Password = SMTP_PASSWORD; $mail->SMTPSecure = SMTP_SECURE; $mail->Port = SMTP_PORT; // メール内容定義 $mail->CharSet = 'UTF-8'; $mail->Encoding = 'base64'; $mail->setFrom('from@example.com', 'メール送信者'); $mail->addAddress('to@example.com', 'メール受信者'); $mail->Subject = 'SMTPからの送信テスト'; $mail->Body = "テスト。\r\nこれはテストです。"; // メール送信 if (!$mail->send()) { exit('Error: ' . $mail->ErrorInfo); } exit('complete');
外部のSMTPがメールを送信するので、/var/log/maillog には送信ログが記録されなかった ■ローカルのSMTPで送信
<?php require_once 'PHPMailer/class.phpmailer.php'; require_once 'PHPMailer/class.smtp.php'; // SMTPサーバ: ホスト define('SMTP_HOST', 'localhost'); // SMTPサーバ: 送信ポート define('SMTP_PORT', '25'); // メール送信準備 $mail = new PHPMailer(); $mail->isSMTP(); $mail->SMTPAuth = false; $mail->Host = SMTP_HOST; $mail->Port = SMTP_PORT; // メール内容定義 $mail->CharSet = 'UTF-8'; $mail->Encoding = 'base64'; $mail->setFrom('from@example.com', 'メール送信者'); $mail->addAddress('to@example.com', 'メール受信者'); $mail->Subject = 'ローカルSMTPからの送信テスト'; $mail->Body = "テストメール。\r\nこれはローカルSMTPからの送信テストです。"; // メール送信 if (!$mail->send()) { exit('Error: ' . $mail->ErrorInfo); } exit('complete');
/var/log/maillog には以下のような送信ログが記録された
Jun 7 11:44:05 aws-terraport sendmail[24154]: x572i5mI024154: from=<from@example.com>, size=587, class=0, nrcpts=1, msgid=<68af5fe5c0a0bf037ad9cb157165d3b1@monitor.example.com>, proto=ESMTP, daemon=MTA, relay=localhost [127.0.0.1] Jun 7 11:44:06 aws-terraport sendmail[24156]: STARTTLS=client, relay=gmail-smtp-in.l.google.com., version=TLSv1/SSLv3, verify=FAIL, cipher=ECDHE-RSA-AES128-GCM-SHA256, bits=128/128 Jun 7 11:44:07 aws-terraport sendmail[24156]: x572i5mI024154: to=<to@example.com>, delay=00:00:02, xdelay=00:00:02, mailer=esmtp, pri=120587, relay=gmail-smtp-in.l.google.com. [74.125.204.27], dsn=2.0.0, stat=Sent (OK 1559875447 s15si706778pgj.350 - gsmtp)~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ 3130,1 末尾
■添付ファイルを送信する メール送信の前部分に、以下のコードを追加すれば添付ファイルを送信できる 添付ファイルは、もちろん指定の場所に存在している必要がある addAttachmentは、複数回実行して複数のファイルを添付することもできる
// ファイル添付 $mail->addAttachment('files/sample.pdf');
また、addAttachment で第2引数を指定すれば、そのファイル名で添付することができる 例えば以下の場合、「test.pdf」という名前で添付ファイルが送信される
// ファイル添付 $mail->addAttachment('../files/sample.pdf', 'test.pdf');
■参考 メール送信がうまくいかないときに読む記事(そういう質問をされたときに読ませる記事) - Qiita https://qiita.com/ShibuyaKosuke/items/309c0a7d969baf0ea8d1

Advertisement