我需要一次向注册用户发送几百封电子邮件,我遇到了问题。我使用的PHP脚本(之前已经使用过,在不同的托管服务上没有问题),只要它发送电子邮件就会落后整个服务器(顺便说一下,它有8GB RAM)。
现在,我与托管支持人员交谈,询问他们的邮件服务器是否有问题,或者是否存在某些外发邮件限制等等,他们说没有。事实上,他们坚决声称这是一个编码问题。我非常怀疑,但是自从上次使用几个月以来,脚本可能会略有改动,所以我在下面分享这个脚本,我要发送的典型电子邮件是$content
变量。 / p>
问题是,有人可以看到为什么这段代码会像疯了一样吃掉资源吗?
我检查了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
答案 0 :(得分:0)
@Pitchinnate提到ob_*()
函数效果不佳。我无法对此进行明确的评论,但听起来似乎有道理。更好的选择是取代:
ob_start(); //Turn on output buffering
?>
<!-- HTML -->
<?php
//copy current buffer contents into $message variable and delete current output buffer
$message = ob_get_clean();
使用:
$message = <<<_EOI_
<!-- HTML -->
_EOI_;
这称为'HEREDOC' syntax,您可以像使用双引号字符串一样使用$variables
。
除此之外,我无法真正看到任何会导致服务器上的MTA出现问题的内容。如果删除输出缓冲位并且脚本仍然导致几百条消息出现问题,我将不得不说邮件服务器配置不是最佳的。例如。它们允许太多的sendmail守护进程在后台启动,它正在吞噬服务器内存。
此外,在所有自动邮件中,您应该始终包含某种取消订阅机制,无论是链接还是有关如何退出的说明。那,并确保它及时工作。 :P