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 hexo+github部署博客
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了
  • ¥100 H5网页如何调用微信扫一扫功能?