PHP 邮件模板是一种用于生成动态电子邮件内容的工具或方法。它允许开发者将邮件内容分为静态部分(如 HTML 结构)和动态部分(如变量、数据),从而简化邮件内容的生成和维护。
以下是一个使用 PHPMailer 发送 HTML 邮件模板的简单示例:
<?php
require 'vendor/autoload.php';
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
$mail = new PHPMailer(true);
try {
// 邮件服务器设置
$mail->isSMTP();
$mail->Host = 'smtp.example.com';
$mail->SMTPAuth = true;
$mail->Username = 'your_email@example.com';
$mail->Password = 'your_password';
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
$mail->Port = 587;
// 发件人
$mail->setFrom('from@example.com', 'Mailer');
// 收件人
$mail->addAddress('to@example.com', 'Receiver');
// 邮件内容
$mail->isHTML(true);
$mail->Subject = 'Here is the subject';
$mail->Body = file_get_contents('email_template.html'); // 加载 HTML 模板
$mail->Body .= '<p>这里是动态数据。</p>'; // 添加动态内容
$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
?>
通过以上方法,您可以有效地使用 PHP 邮件模板来发送动态电子邮件内容,并解决可能遇到的问题。