I need to send a few hundred emails at a time to registered users and I'm running into a problem. The PHP script I use (and have used before with no issues on a different hosting service) lags the entire server (which has 8GB RAM by the way) for as long as it is sending emails.

我需要一次向注册用户发送几百封电子邮件,我遇到了问题。我使用的PHP脚本(并且以前使用过,在不同的托管服务上没有问题)只要发送电子邮件就会落后于整个服务器(顺便说一下,它有8GB RAM)。

Now, I talked to the hosting support people, asking if something's wrong with their mail server, or if there are some outgoing mail limitations or whatever, and they said no. In fact, they are adamantly claiming that it's a coding issue. I highly doubt that, but it's possible the script was slightly altered since its last use a few months back, so I'm sharing the script below, and a typical email I would be sending is in the $content variable.

现在,我与托管支持人员交谈,询问他们的邮件服务器是否有问题,或者是否存在某些外发邮件限制等等,他们说不。事实上,他们坚决声称这是一个编码问题。我非常怀疑,但是自从几个月前的最后一次使用以来,脚本可能会略有改动,所以我在下面分享脚本,我将发送的典型电子邮件是$ content变量。

The question is, can someone see a reason why this code would eat up resources like crazy?

问题是,有人可以看到为什么这些代码会像疯了一样吃掉资源?

I have checked the MySQL logs, and the query itself (which gets emails from the database) isn't slow. So it's the mailing itself.

我检查了MySQL日志,查询本身(从数据库中获取电子邮件)并不慢。所以这是邮件本身。

PHP mail_sender file:

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);
}

And this is the function itself:

这就是功能本身:

//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 );
}

Is there any reason whatsoever for this to use up resources or have a slow execution? The server is very fast and doesn't have a problem with anything else.

是否有任何理由使用资源或执行速度慢?服务器非常快,并没有任何其他问题。

Mail settings in php.ini:

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

1 个解决方案

#1


0

@Pitchinnate mentioned that ob_*() functions aren't the best performing. I can't definitively comment one way or another on that, but it sounds plausible. A better option would be replacing:

@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();

With:

$message = <<<_EOI_

<!-- HTML -->

_EOI_;

This is called 'HEREDOC' syntax and you can use $variables it in just like a double-quoted string.

这称为'HEREDOC'语法,您可以像使用双引号字符串一样使用$ variables。

Aside from that I can't really see anything that would cause problems for the MTA on the server. If you remove the output buffering bit and the script still causes problems with a few hundred messages I would have to say that the mail server configuration is not optimal. eg. They allow too many sendmail daemons to fire up in the background and it's eating away at the server memory.

除此之外,我无法真正看到任何会导致服务器上的MTA出现问题的内容。如果删除输出缓冲位并且脚本仍然导致几百条消息出现问题,我将不得不说邮件服务器配置不是最佳的。例如。它们允许太多的sendmail守护进程在后台启动,它正在吞噬服务器内存。

Also, in all automated mailings you should always include some sort of unsubscribe mechanism whether it's a link or some instructions on how to opt out. That, and make sure that it works in a timely fashion. :P

此外,在所有自动邮件中,您应该始终包含某种取消订阅机制,无论是链接还是有关如何退出的说明。那,并确保它及时工作。 :P

更多相关文章

  1. PHP将邮件发送到多个电子邮件地址
  2. 如何在发送之前更改SOAP请求中的名称空间前缀(皇家邮件 - 发送请
  3. PHP使用gmail发邮件
  4. thinkphp框架里怎么用linux的crontab写php的定时脚本
  5. 锁定表是否可以通过包含的脚本访问
  6. 确定脚本所在的服务器以及PHP中的配置的最佳方法是什么?
  7. imagecreatetruecolor()在symfony中不起作用,但如果作为php脚本运行
  8. php-fpm通过request_slowlog_timeout检查哪个脚本执行时间长
  9. 使用GMail SMTP服务器从PHP页面发送电子邮件

随机推荐

  1. android market过滤规则研究 - 第二届 Go
  2. Android创建和使用数据库详细指南(7)
  3. 【Android 设计】:启航_ 创作意图 | 设计
  4. Android实现网络视频播放
  5. android程序实现简单拨号器功能
  6. Android简单音乐播放器
  7. 按powerkey唤醒启动上层Andord
  8. android:layout_gravity 和 android:grav
  9. android studio创建新项目color.xml文件
  10. java.lang.NoClassDefFoundError: com.go