使用 PHPMailer 实现邮件的实时发送
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'path/to/PHPMailer/src/Exception.php';
require 'path/to/PHPMailer/src/PHPMailer.php';
require 'path/to/PHPMailer/src/SMTP.php';
$mail = new PHPMailer(true);
try {
//Server settings
$mail->isSMTP();
$mail->Host = 'smtp.example.com';
$mail->SMTPAuth = true;
$mail->Username = 'user@example.com';
$mail->Password = 'secret';
$mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;
$mail->Port = 465;
//Recipients
$mail->setFrom('from@example.com', 'Mailer');
$mail->addAddress('to@example.com', 'Joe User');
//Content
$mail->isHTML(true);
$mail->Subject = 'Subject Text';
$mail->Body = 'Body Text';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
确保替换 'path/to/PHPMailer/src/...'
为实际的路径,以及设置正确的 SMTP 服务器、用户凭据和端口。这段代码演示了如何使用 PHPMailer 发送一封 HTML 格式的电子邮件。
评论已关闭