使用数据库连接使用 phpmailer 发送邮件()的正确方法

发布于 2024-12-25 04:38:27 字数 1850 浏览 1 评论 0原文

我正在尝试修改 phpmailer db 邮件程序以使用 sendmail,下面的代码是否正确?我正在尝试将其实施到一个网站中,向大约 2-300 名联系人的客户群发送电子邮件。 谢谢 :-)

<?php
require_once('./send/class.phpmailer.php');
//include("class.smtp.php"); // optional, gets called from within class.phpmailer.php     if not already loaded

$mail = new PHPMailer(true); //defaults to using php "mail()"; the true param means it     will throw exceptions on errors, which we need to catch

 $body                = file_get_contents('contents.html');
 $body                = eregi_replace("[\]",'',$body);


$mail->SetFrom('[email protected]', 'List manager');
$mail->AddReplyTo('[email protected]', 'List manager');

$mail->Subject       = "PHPMailer Test Subject via smtp, basic with authentication";

 $query  = "SELECT full_name, email, photo FROM employee WHERE id=$id";
$result = @MYSQL_QUERY($query);

while ($row = mysql_fetch_array ($result)) {
  $mail->AltBody    = "To view the message, please use an HTML compatible email viewer!";     // optional, comment out and test
  $mail->MsgHTML($body);
  $mail->AddAddress($row["email"], $row["full_name"]);
  $mail->AddStringAttachment($row["photo"], "YourPhoto.jpg");

  if(!$mail->Send()) {
    echo "Mailer Error (" . str_replace("@", "&#64;", $row["email"]) . ') ' .         $mail->ErrorInfo . '<br>';
  } else {
    echo "Message sent to :" . $row["full_name"] . ' (' . str_replace("@", "&#64;",     $row["email"]) . ')<br>';
}
// Clear all addresses and attachments for next loop
$mail->ClearAddresses();
$mail->ClearAttachments();
}
?>    

I am trying to modify the phpmailer db mailer to use sendmail, would this code be correct below ?. I am trying to implement it into a website to email a clientbase of around 2-300 contacts.
Thanks :-)

<?php
require_once('./send/class.phpmailer.php');
//include("class.smtp.php"); // optional, gets called from within class.phpmailer.php     if not already loaded

$mail = new PHPMailer(true); //defaults to using php "mail()"; the true param means it     will throw exceptions on errors, which we need to catch

 $body                = file_get_contents('contents.html');
 $body                = eregi_replace("[\]",'',$body);


$mail->SetFrom('[email protected]', 'List manager');
$mail->AddReplyTo('[email protected]', 'List manager');

$mail->Subject       = "PHPMailer Test Subject via smtp, basic with authentication";

 $query  = "SELECT full_name, email, photo FROM employee WHERE id=$id";
$result = @MYSQL_QUERY($query);

while ($row = mysql_fetch_array ($result)) {
  $mail->AltBody    = "To view the message, please use an HTML compatible email viewer!";     // optional, comment out and test
  $mail->MsgHTML($body);
  $mail->AddAddress($row["email"], $row["full_name"]);
  $mail->AddStringAttachment($row["photo"], "YourPhoto.jpg");

  if(!$mail->Send()) {
    echo "Mailer Error (" . str_replace("@", "@", $row["email"]) . ') ' .         $mail->ErrorInfo . '<br>';
  } else {
    echo "Message sent to :" . $row["full_name"] . ' (' . str_replace("@", "@",     $row["email"]) . ')<br>';
}
// Clear all addresses and attachments for next loop
$mail->ClearAddresses();
$mail->ClearAttachments();
}
?>    

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

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

发布评论

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

评论(1

茶花眉 2025-01-01 04:38:27

这应该可行,但请确保托管公司支持此功能。

This should work, but make sure this feature is supported by the hosting company.

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