我一直在使用以下内容来获取和通过电子邮件发送数据库中的人员。现在的问题是,数据库有超过500个成员,脚本速度变慢,并在TO:字段中显示每个成员的电子邮件地址。我尝试在另一个网站上建议使用BCC,但我想知道是不是有办法改变这个以单独发送电子邮件?
$sql = "SELECT * FROM users WHERE system = '101' AND mailing_list = 'yes'";
$result = mysql_query($sql) or die("Unable to execute<br />$sql<br />".mysql_error());
$row = mysql_fetch_array($result);
var_dump($row);
$to .= $row['email'] . "\r\n";
//send email
答案 0 :(得分:1)
php的mail()非常低效,我建议使用像phpmailer
这样的东西来自手册:
注意:
值得注意的是mail()函数不适合更大的 循环中的电子邮件量。此功能可打开和关闭SMTP 每个电子邮件的套接字,效率不高。
要发送大量电子邮件,请参阅»PEAR :: Mail和» PEAR :: Mail_Queue包。
答案 1 :(得分:0)
您需要使用PHPMailer,因为它仅用于此情况。 mail()的问题在于它会在每封电子邮件后打开和关闭连接。您显然想要打开1个连接,发送所有电子邮件(逐个)并关闭连接。
像below:
require("class.phpmailer.php");
$mail = new phpmailer();
$mail->From = "list@example.com";
$mail->FromName = "List manager";
$mail->Host = "smtp1.example.com;smtp2.example.com";
$mail->Mailer = "smtp";
@MYSQL_CONNECT("localhost","root","password");
@mysql_select_db("my_company");
$query = "SELECT full_name, email, photo FROM employee WHERE id=$id";
$result = @MYSQL_QUERY($query);
while ($row = mysql_fetch_array ($result))
{
// HTML body
$body = "Hello <font size=\"4\">" . $row["full_name"] . "</font>, <p>";
$body .= "<i>Your</i> personal photograph to this message.<p>";
$body .= "Sincerely, <br>";
$body .= "phpmailer List manager";
// Plain text body (for mail clients that cannot read HTML)
$text_body = "Hello " . $row["full_name"] . ", \n\n";
$text_body .= "Your personal photograph to this message.\n\n";
$text_body .= "Sincerely, \n";
$text_body .= "phpmailer List manager";
$mail->Body = $body;
$mail->AltBody = $text_body;
$mail->AddAddress($row["email"], $row["full_name"]);
$mail->AddStringAttachment($row["photo"], "YourPhoto.jpg");
if(!$mail->Send())
echo "There has been a mail error sending to " . $row["email"] . "<br>";
// Clear all addresses and attachments for next loop
$mail->ClearAddresses();
$mail->ClearAttachments();
}