Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用PHP发送的邮件花费了太多时间_Php_Performance_Phpmailer - Fatal编程技术网

使用PHP发送的邮件花费了太多时间

使用PHP发送的邮件花费了太多时间,php,performance,phpmailer,Php,Performance,Phpmailer,我用PHP写了一个类,我用它来发送邮件,使用Gmail帐户。该类反过来使用PHPMailer库。安装程序是Windows Vista上的WAMP 2.4。使用PHP中的microtime()函数,我发现发送一封邮件需要5到6秒的时间。PHP脚本运行在这样的设置上是正常的吗?一封邮件发送需要花费5-6秒的时间。下面是这个类的代码 <?php require_once("phpmailer/class.phpmailer.php"); require_once("phpmailer/clas

我用PHP写了一个类,我用它来发送邮件,使用Gmail帐户。该类反过来使用PHPMailer库。安装程序是Windows Vista上的WAMP 2.4。使用PHP中的
microtime()
函数,我发现发送一封邮件需要5到6秒的时间。PHP脚本运行在这样的设置上是正常的吗?一封邮件发送需要花费5-6秒的时间。下面是这个类的代码

<?php

require_once("phpmailer/class.phpmailer.php");
require_once("phpmailer/class.smtp.php");

class Mailer {

    // Needs to be set per object
    public $subject;
    public $message;
    public $to_name;
    public $to;

    private $mail; // This is the main mail object that'll be initialized 

    public function __construct() {

        // Need to create a PHPMailer object in the constuctor and return it for use in this class. 

        $mail = new PHPMailer();

        $from_name = "bleh";
        $from = "bleh@gmail.com";
        $username = "bleh";
        $password = "bleh";

        $mail->FromName = $from_name;
        $mail->From = $from;
        $mail->Username = $username;
        $mail->Password = $password;

        $mail->IsSMTP();
        $mail->Host = "smtp.gmail.com";
        // $mail->Port = 587; // Turns out, I dont need this one. 
        $mail->SMTPAuth = true; // gmail requires this
        $mail->SMTPSecure = 'tls'; // gmail requires this

        $this->mail = $mail;


    }


    function send() {

        $mail = $this->mail; // The mail object 

        $mail->Subject = $this->subject;
        $mail->Body = $this->message;
        $mail->AddAddress($this->to, $this->to_name);

        $result = $mail->Send();
        return $result;

    }
}
?>

正如我在评论中提到的,Gmail可能会限制你的收费。也可能是你与Gmail的网络通信的某些方面导致了这个问题

您可以从命令行手动开始与Gmail的SMTP对话。观察每一步需要多长时间,并检查Gmail返回的任何指示问题的代码/消息

有关如何创建手动SMTP对话的详细信息,请参阅

返回的消息将按照该答案中的指示进行Base64编码。您可以使用转换回纯文本


注意:该链接显示Linux的说明。如果您没有Linux服务器可供测试,您可以使用Cygwin(适用于windows)或不需要完整Cygwin安装的软件包。SMTP花费很长时间是很常见的,它甚至可以作为一种反垃圾邮件措施使用。在交通开始前允许最多5分钟的延迟。SMTP不用于交互使用(因为在这种情况下它不能排队),因此在提交网页期间通过SMTP发送可能会受到影响。通过异步进程发送邮件或SMTP可以避免此问题

在PHPMailer中,您可以启用SMTP调试输出,它将向您显示正在发生的事情,以便您能够看到所花费的时间:

$mail->SMTPDebug = 2;

我也遇到过同样的问题,大约30多岁才能发邮件

问题是运行PHP服务器的系统在发送电子邮件之前被挂起

我尝试了许多解决方案,但最简单的方法是在不同的端口上本地启动另一个PHP服务器来处理邮件发送

主PHP服务器(系统运行服务器)将把邮件发送请求移交给邮件PHP服务器并继续该过程


发送电子邮件仍然需要30秒,但不会影响主PHP服务器。

任何SMTP对话都需要5-6秒,这是不正常的。如果是这样的话,我们的垃圾邮件就少得多了————如果你认为你的发送活动可疑,Gmail就是限制你的垃圾邮件。不确定他们是否会那样做,但他们可以。我现在已经分享了我的代码,任何人都可以告诉我我的代码是否减慢了速度。我在你的PHP代码中没有看到任何东西会导致SMTP对话花费那么长的时间。5-6秒的计时从哪里开始和结束?如果只使用PHP中的简单
mail()
,需要多长时间?我知道这不是你想要达到的。但这正常吗?我无法在设置中使用mail()函数,因为它会尝试使用sendmail之类的二进制文件来发送实际邮件。我的设置中没有sendmail。确定。我会将时间与手动SMTP对话进行比较。
$mail->SMTPDebug = 2;