从 PHP/Mysql 发送包含内容的电子邮件

发布于 2024-09-16 05:48:10 字数 206 浏览 9 评论 0原文

我想发送电子邮件,其中数据来自 php/mySQL 查询。

我知道 html 会在电子邮件中呈现,但我不认为 php 代码会呈现。

那么有什么方法可以发送包含从 mySQL DB 查询的内容的电子邮件吗?

我已经在这里搜索了,有一个主题涵盖了它,但建议的人建议导出 pdf 或使用第三方工具,但在我的情况下不适用。

谢谢你们的帮助:)

I want to send emails where the data is from a php/mySQL query.

I know that html will render in the email but i don't think the php codes will.

So is there any way where i can send emails with content queried from a mySQL DB ?

I already search here, there is one topic that covers it but the person who advised suggested a pdf export or to use a 3rd party tool which in my case are not applicable.

Thank you for the help guys :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

开始看清了 2024-09-23 05:48:10

使用 PHPMailer 在服务器上生成电子邮件。它使得生成多部分消息(纯文本 + html,带有附件和嵌入/内联图像)变得非常容易。基本上:

// set up PHPMailer
$mail = new PHPMailer();
$mail->SetFrom('[email protected]');
$mail->AddReplyTo('[email protected]');
$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);
}

Use PHPMailer to generate the email on the server. It makes it very easy to generate multi-part messages (plaintext + html, with attachments and embedded/inline images). Basically:

// set up PHPMailer
$mail = new PHPMailer();
$mail->SetFrom('[email protected]');
$mail->AddReplyTo('[email protected]');
$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);
}
﹂绝世的画 2024-09-23 05:48:10

为了避免垃圾邮件问题,您可以将 PHPMailer 包装在一个类中,并在从数据库表中读取的每个电子邮件地址处实例化该类。

对于每个电子邮件地址,您可以创建一个新实例并执行 ->setMailAddr($mail,$fullName) 类型,并在发送电子邮件后销毁该实例。

理想的情况是在每个 POST 处放置一个新实例。在这种情况下,您可以将 FORM 放入页面中。

To avoid spam issues you can wrap the PHPMailer in a class and instantiate that at every e-mail address read from the database table.

For each email address you can create a new instance and do kind of a ->setMailAddr($mail,$fullName) and after send the e-mail destroy this instance.

The ideal is to place a new instance at every POST. In this case, you can put a FORM into the page.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文