doutui8842 2011-05-23 20:30
浏览 46
已采纳

SwiftMailer库表现缓慢

I'm trying to implement swiftmailer into this mailing system. my client has about 300k active emails that need to be sent on a semi regular basis. the system was originally configured for sendmail and php's mail() function. I've since installed the latest version of postfix.

it may be my expectations were way too high but i was under the impression this thing can put a lot of emails into the queue FAST, which is exactly what i need. all the rate handling and throttling is done on the postfix side, so being able to queue them as fast as my postfix settings can handle would be great.

while i could implement methods to insert the contact directly into the queue, i'd rather limit the input of emails going into the queue based on various options such as global send rates for the smtp server.

the code below is just something basic to test. it loops through 30 separate mailing smtp accounts, each with it's own rate properties. i'm trying to pull the max amount of emails out of the db per cron, then send then all using batchsend(), then loop to the next smtp account, send max, etc..

technically it does work, however it's really slow. at a rate of 60/min per smtp account, it takes about 15-20 seconds each which obviously will not work and not at all what i had expected.

is there something really obvious about why this would send so slow? the smtp servers appear to be fine, no overload or anything like that. no postfix errors, nothing obvious.

once the emails make it to the queue, i let postfix work it's magic. it's getting it into the queue at a reasonable rate that's become difficult. i know swiftmailer is a magic solution to all my problems, but i'm sure it should be sending quicker than it is. any ideas or suggestions?

$query = "SELECT * FROM `smtp`";
    $result = mysql_query($query) or die(mysql_error());
    $num_rows1 = mysql_num_rows($result);
    while($row = mysql_fetch_array($result)){
        $smtp = $row['ip'];
        $login = $row['user'];
        $pass = $row['pass'];
        $smtp_domain = $row['domain'];   

    $querya = "SELECT * FROM `mailer_lists` ml JOIN `mailer_controller` mc ON ml.project_name = mc.project_name LIMIT ".$global_limit."";
    $resulta = mysql_query($querya) or die(mysql_error());
    $num_rows2 = mysql_num_rows($resulta);

    // quickly check if any mail returned query
    if ($num_rows2 < 1){
        mysql_close($connection);
        echo "Nothing to mail... 
";
        die();
        }

    while($rowa = mysql_fetch_array($resulta)){
        $email[] = $rowa['email'];
        $project_name = $rowa['project_name'];
        $from_name = $rowa['from_name'];
        $subject = $rowa['subject'];
        $body = $rowa['body'];
        $from = array($spun_from_email => $spun_from_name);
    }    

    require_once 'swift/swift_required.php';
        $transport = Swift_SmtpTransport::newInstance(''.$smtp.'', 25)
          ->setUsername($login)
          ->setPassword($pass)
          ;
        $mailer = Swift_Mailer::newInstance($transport);
        $message = Swift_Message::newInstance()
          ->setSubject($subject)
          ->setFrom($from)
         // ->addPart('add part message', 'text/html')
         // ->attach(Swift_Attachment::fromPath(''))
          ->setTo($email)
          ->setBody($body)
        ;
        $mailout = $mailer->batchSend($message);
         // ->addPart('add part message', 'text/html')
         // ->attach(Swift_Attachment::fromPath(''))
          ;  


            $queryb = "DELETE FROM `mailer_lists` WHERE `project_name` = '".$project_name."' AND `email` = '".implode('\' OR `email` = \'',$email)."'";  
            $resultb = mysql_query($queryb) or die(mysql_error());
            $c++;
            echo "sent $num_rows1 emails to smtp $c 
";
        }
  • 写回答

1条回答 默认 最新

  • dtt83024 2011-05-26 19:37
    关注

    Your problem is not the library you are using. This is being slow because you are sending a big chunk of data to the email server...so postfix is trying to process all before replying you. Try sending ONE email at the time without closing the connection, so this way the email server can process faster each email you send to it and return an answer faster too.

    • move the following line to the top of the script

    require_once 'swift/swift_required.php';

    • Make the connection to the smtp server lazy. You don't need to connect every time you want to send an email. Connect once at the top and check if the connection is alive (otherwise connect again), send the email and clear the fields to, from, etc...

    • grab your smtp settings from the database at the top then remove the giant while. That doesn't make the code readable at all

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100