PHP 使用 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/...'
为PHPMailer库的实际路径,以及设置正确的SMTP服务器信息、用户凭据和邮件内容。
评论已关闭