我需要一次向注册用户发送几百封电子邮件,我遇到了问题。只要它正在发送电子邮件,我使用的 PHP 脚本(并且以前在不同的托管服务上使用过,没有问题)就会落后于整个服务器(顺便说一下,它有 8GB RAM)。
现在,我与托管支持人员交谈,询问他们的邮件服务器是否有问题,或者是否有一些外发邮件限制或其他什么,他们说没有。事实上,他们坚决声称这是一个编码问题。我非常怀疑这一点,但是自几个月前上次使用以来,该脚本可能略有更改,因此我在下面共享该脚本,并且我将发送的典型电子邮件位于$content
变量中。
问题是,有人能看出为什么这段代码会疯狂消耗资源的原因吗?
我检查了 MySQL 日志,查询本身(从数据库中获取电子邮件)并不慢。所以它是邮寄本身。
PHP mail_sender 文件:
$content="<p style='line-height:20px;margin:10px 0;'>Hello,</p>
<p style='line-height:20px;margin:10px 0;'>This is an email to notify you etc etc.</p>
<p style='line-height:20px;margin:10px 0;'>This is line 2 of the email, it's usually not much longer than this example.</p>
<p style='line-height:20px;margin:10px 0;'>Regards,<br/>Site Name staff</p>";
$result=mysql_query("select email from members where tipster_by_email='1' ") or die(mysql_error());
while($row=mysql_fetch_assoc($result)){
sendToUser($row['email'],$admin_email,"Email Title",$content);
}
这是函数本身:
//generic HTML-formatted e-mail sender
function sendToUser($email,$admin_email,$subject,$content){
//define the receiver of the email
$to = $email;
//create a boundary string. It must be unique
//so we use the MD5 algorithm to generate a random hash
$random_hash = md5(date('r', time()));
//define the headers we want passed. Note that they are separated with \r\n
$headers="From: Site Name <$admin_email>";
$headers.="\r\nReply-To: $admin_email";
//add boundary string and mime type specification
$headers .= "\r\nMIME-Version: 1.0";
$headers .= "\r\nContent-Type: text/html; ";
//define the body of the message.
ob_start(); //Turn on output buffering
?>
<div style="width:730px;text-align:justify;margin-bottom:25px;">
<?php echo stripslashes($content); ?>
<div style='width:100%;height:1px;background:#ccc;margin:10px 0;'></div>
<div style='width:100%;color:#333;font-size:12px;'>
<p style='line-height:12px;margin-top:10px;'>Site Name is owned by Company Name</p>
<p style='line-height:12px;margin-top:10px;'>All rights reserved.</p>
<p style='line-height:12px;margin-top:10px;'><a style='color:blue;' href='facebookurl'>Like us on Facebook</a> or <a style='color:#b04141;' href='twitterurl'>follow us on Twitter</a></p>
</div>
</div>
<?php
//copy current buffer contents into $message variable and delete current output buffer
$message = ob_get_clean();
//send the email
mail( $to, $subject, $message, $headers );
}
是否有任何理由会耗尽资源或执行缓慢?服务器非常快,并且没有其他任何问题。
php.ini 中的邮件设置:
Mail SMTP Used under Windows only: host name or IP address of the SMTP server PHP should use for mail sent with the mail() function. [strikethrough]localhost[/strikethrough] **DEFAULT**, Click to Edit
Mail sendmail_from [strikethrough]me@localhost.com[/strikethrough] **DEFAULT**, Click to Edit
Mail sendmail_path /usr/sbin/sendmail -t -i
Mail smtp_port 25