Page 1 of 1

it takes very long to send a few mails (15 secs - 3 emails)

Posted: Sat May 31, 2008 6:41 pm
by pauloportu
hi,

everything seems to work perfect, however, it takes very long to send a few emails to the list of persons in my MySql database.

A test to send emails to only 3 different email addresses took 15 seconds and 1 of them even failed to pass. A small mailinglist of not even 100 members took almost one hour, where -again - one third failed delivery / acceptance.

I checked the documentation, tutorials and this forum, but unfortunately I am still stuck with this problem. I am new here, but eager to learn of course.

- how long should it normally take to send a small mailinglist?
- as I think that my above mentioned situation is not how it is supposed to work, I must overlook
something, but what?

to give an idea of the situation, here some more info:
- website runs on a Linux server (shared)
- Apache 2.0.61
- PHP 4.4.8
- MySql 4.1.22

a piece of (relevant) code:

Code: Select all

 
define("TEST_CONFIG_PATH", dirname(__FILE__));
define("DEFAULT_WRITABLE_PATH", TEST_CONFIG_PATH . "/tmp");
$WRITABLE_PATH = DEFAULT_WRITABLE_PATH;
 
 
require_once "./swift_mail/Swift.php";
require_once "./swift_mail/Swift/Connection/SMTP.php"; 
require_once "./swift_mail/Swift/Plugin/AntiFlood.php"; require_once "./swift_mail/Swift/Plugin/VerboseSending.php"; require_once "./swift_mail/Swift/Plugin/Throttler.php";
require_once "./swift_mail/Swift/Plugin/Decorator.php"; 
 
//Start Swift
$swift =& new Swift(new Swift_Connection_SMTP("mail.xxxxxx.com")); 
 
 
//use Disk Caching:
Swift_CacheFactory::setClassName("Swift_Cache_Disk");
Swift_Cache_Disk::setSavePath($WRITABLE_PATH); //   /tmp-folder chmod 777
//-------------------------------------------------------------------------------------
$throttler =& new Swift_Plugin_Throttler();
$throttler->setBytesPerMinute(20000000); 
$swift->attachPlugin($throttler, "throttler");
//-------------------------------------------------------------------------------------
$view =& new Swift_Plugin_VerboseSending_DefaultView(); 
$swift->attachPlugin(new Swift_Plugin_VerboseSending($view), "verbose");
//-------------------------------------------------------------------------------------
$swift->attachPlugin(new Swift_Plugin_AntiFlood(100, 10), "anti-flood");
//------------------------------------------------------------------------------------------------------
 
$message =& new Swift_Message($_POST[subject]); 
$img1 = & new Swift_Message_Image(new Swift_File("./swift_files/banner.gif"));
$src1 = $message->attach($img1);
 
 
$content = nl2br(stripslashes($_POST['content']));
$contentplain = str_replace("<br />","\n",$content);
 
//Create the plain text message
$body_txt =            "Dear {first_name},\n\n".
            "$contentplain\n\n\n".
            "Best regards,\n".
            "etc...\n\n";
 
//Create the html text message
$body_html = "<br>".    
"<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">".
//  ---------- BODY -------------
"Dear {first_name},<br><br>".
"$content<br><br><br>".
"Best regards,<br>".
"xxxxx<br>".
//  ---------- BODY -------------
ETC.ETC.ETC.";
 
 
$message->attach(new Swift_Message_Part($body_txt)); 
$message->attach(new Swift_Message_Part($body_html, "text/html")); 
 
class Replacements extends Swift_Plugin_Decorator_Replacements {
    function getReplacementsFor($address) {
        $query = "SELECT
        first_name as '{first_name}', id as '{id}'
        FROM members
        WHERE email_address = '" . mysql_real_escape_string($address) . "'";
        $result = mysql_query($query);
        if (mysql_num_rows($result) > 0)
        {
            return mysql_fetch_assoc($result);
        }
    }
}
 
//Load the plugin with the extended replacements class
$swift->attachPlugin(new Swift_Plugin_Decorator(new Replacements()), "decorator");
 
$count_emails = 0;
 
$recipients =& new Swift_RecipientList();
 
// selected clients ...
foreach($_POST[clients] AS $id => $mail){
$recipients->addTo($mail);
$count_emails = $count_emails+1;
}
 
if ($swift->batchSend($message, $recipients, new Swift_Address($from, $from_name))){
echo "<p class=\"txb\">message Sent!";
}else{
echo "<p class=\"txb\">message Failed ...";
}
 
echo 'Email sent to '.$count_emails.' members </p>';
 
$swift->disconnect(); 
 

I hope I explained clear enough - some advise here is much appreciated:
why is it running slow?
where to look first?

thank you.

Paul

Re: it takes very long to send a few mails (15 secs - 3 emails)

Posted: Sat May 31, 2008 6:52 pm
by Weirdan
pauloportu wrote: why is it running slow?
where to look first?
Anytime you have performance problems a profiler is your best bet. It will allow you to see what functions take the most time in your script (or third party library for that matter). xdebug + kcachegrind(wincachegrind if you're developing on windows) would give you the answer (unless the script is waiting in the external (C) function, like dns lookup or connecting a socket).

Re: it takes very long to send a few mails (15 secs - 3 emails)

Posted: Sat May 31, 2008 10:01 pm
by Chris Corbyn
This is network related. Swift is really fast itself. Something is slowing things down over the network between the SMTP server and the HTTP server. I suspect you're on a shared host who's mail server is being abused.

Re: it takes very long to send a few mails (15 secs - 3 emails)

Posted: Sun Jun 01, 2008 8:33 am
by pauloportu
thank you for your quick response.

@Weirdan: I will check the profilers

@Chris: yes I'm on a shared host. I do not know if the mailserver is being abused as I'm not hosting myself. I could ask my host if they have restricted (ab)use of their mailserver, etc. So in this case I am dependant on them and I am not sure if they would change settings for one client (me).

I understand that this is not a matter of me overlooking something (to double check I will see if Weirdan's profiler could bring me further), but my destiny is in third party's hands, which would mean that I could not have full advantage of Swift if my host is not willing to cooperate?

Anyway, thanks again for your visions, I'll let you know when I'm further in my 'investigation'... of course I'll keep on having an open mind to any suggestion (read: I'm afraid that there will be no solution if it's about Chris's point of view and my host doesn't want to cooperate...)

Paul

Re: it takes very long to send a few mails (15 secs - 3 emails)

Posted: Sun Jun 01, 2008 9:11 am
by pauloportu
Chris,

I realise that you meant that the mailserver must be working some overtime, however, I forgot to mention that before I was using Swift, I have been sending my emails with a simple class using php's mail() function - this had always worked fine, and sending emails (my mailinglist) was done within no time...

the downside of my former simple class was, that it could not (yet) parse HTML-emails, etc., which was for me the main reason to check out Swift.

Forgive me if I ask something stupid (I do not know much about emailing through websites), but why didn't the mailserver have any problem sending those plain text-emails, but does seem to have difficulties when sending HTML emails? Is this still a matter of a network problem / overloaded mailserver ?

Re: it takes very long to send a few mails (15 secs - 3 emails)

Posted: Sun Jun 01, 2008 5:43 pm
by Chris Corbyn
mail() uses sendmail and doesn't use SMTP directly. The long hang would be happening *after* your script was run with mail(). Try using the sendmail connection ;)

Re: it takes very long to send a few mails (15 secs - 3 emails)

Posted: Sun Jun 01, 2008 7:44 pm
by pauloportu
GREAT it works ! ! ! ! :D

thanks a lot Chris, although I must admit to be a little embarassed :oops: that I could not think of this way myself...

I do want to say: SwiftMailer is terrific, thanks a lot for that as well.

Paul