我想发送数据来自 php/mySQL 查询的电子邮件。
我知道 html 会在电子邮件中呈现,但我认为 php 代码不会。
那么有什么方法可以发送包含从 mySQL DB 查询的内容的电子邮件?
我已经在这里搜索过,有一个主题涵盖了它,但建议的人建议导出 pdf 或使用在我的情况下不适用的第 3 方工具。
谢谢你们的帮助:)
我想发送数据来自 php/mySQL 查询的电子邮件。
我知道 html 会在电子邮件中呈现,但我认为 php 代码不会。
那么有什么方法可以发送包含从 mySQL DB 查询的内容的电子邮件?
我已经在这里搜索过,有一个主题涵盖了它,但建议的人建议导出 pdf 或使用在我的情况下不适用的第 3 方工具。
谢谢你们的帮助:)
使用PHPMailer在服务器上生成电子邮件。它使生成多部分消息(纯文本 + html,带有附件和嵌入/内嵌图像)变得非常容易。基本上:
// set up PHPMailer
$mail = new PHPMailer();
$mail->SetFrom('you@yourserver.com');
$mail->AddReplyTo('you@somewhereelse.com');
$mail->Subject('Your profile');
$mail->IsHTML(TRUE);
// do your database query
$con = connect_to_database();
$stmt = run_database_query($con, "SELECT ... FROM ...");
$data = fetch_from_database($stmt);
// set the email address
$mail->AddAddress($data['email'], $data['fullname']);
// html content for smart email clients
$html = <<<EOL
<h1>Welcome</h1>
<p>Your username is {$data['username']}.</p>
EOL;
// plain text alternate content
$text = <<<EOL
Welcome
Your username is {$data['username']}.
EOL;
// add the content to the mail
$mail->MsgHTML($html);
// add alternate content
$mail->AltBody($text);
// send the mail
if ($mail->Send()) {
// mail sent correctly
} else {
die("Uhoh, could not send to {$mail['email']}:" . $mail->ErrorInfo);
}
为了避免垃圾邮件问题,您可以将 包装PHPMailer
在一个类中,并在从数据库表中读取的每个电子邮件地址处实例化它。
对于每个电子邮件地址,您可以创建一个新实例并执行某种操作,->setMailAddr($mail,$fullName)
然后在发送电子邮件后销毁该实例。
理想的情况是在每个 POST 中放置一个新实例。在这种情况下,您可以将 FORM 放入页面中。