Page 1 of 1

Limiting number of emails sent per hour

Posted: Sun Apr 15, 2007 2:44 pm
by bruno_m
Hello everyone!

I just discovered swiftmailer a couple of weeks ago, and I have to say its some very nice work! I have already made a little donation to the author :-)

I am able to send my emails without any problems, but on the server that I am currently testing it, I have a limit of sending 100 emails/hour. I don't know if it has anything to do with number of connections or not. Their wiki only says that you are allowed to send 100 emails per hour.

My idea is to use swiftmailer to send emails to my customer base (about 800 emails) currently stored in my database. To achieve this I just need to use the AntiFlood plugin?

When Ii move this into production, to my dedicated server, I guess I won't have this problem.

Thanks,
bruno.

Posted: Sun Apr 15, 2007 2:51 pm
by Chris Corbyn
Thanks for the donation :) Very much appreciated - you're in the 0.2% of users who actually donate so feel good for supporting it ;)

AntiFlood isn't actually time-based. I think what you're looking for is the Throttler plugin which does take time as a factor.

Hope that helps :)

Posted: Sun Apr 15, 2007 3:20 pm
by bruno_m
No problem. Hope you enjoy your time "down under with the aussies !" :-)

I will try out the Throttler plugin. Can you give me your opinion on my sending procedure? This is my first shot at it.
Here is a snippet of the code that does my mailing:

Code: Select all

require_once "../tools/Swift-3.1.2-php5/lib/Swift.php";
require_once "../tools/Swift-3.1.2-php5/lib/Swift/Connection/SMTP.php";
 
//Start Swift
$smtp =& new Swift_Connection_SMTP("mail.myserver.com", 587);
$smtp->setUsername("mylogin");
$smtp->setpassword("mypw");
 
$swift =& new Swift($smtp);

$result = mysql_query("SELECT recipient_email FROM common_recipients WHERE list_id=$list_id");

while ($row = mysql_fetch_array ($result)){

// build email message
       $html_body = "<strong> message here </strong>";
       $text_body = "message here";

//Create the message
	$message =& new Swift_Message(urldecode($message_subject));
	//Add some "parts"
	$message->attach(new Swift_Message_Part($html_body, "text/html"));
	$message->attach(new Swift_Message_Part($text_body, "text/plain"));

	//Now check if Swift actually sends it
	if ($swift->send($message, urldecode($row['recipient_email']), urldecode($message_from_email))) echo "Sent";
	else echo "Failed";

}
Is this a good way of sending 700+ emails or should I structure this differently?

Thanks.

Posted: Sun Apr 15, 2007 3:59 pm
by Chris Corbyn
You'll get a significant performance boost if you create $message before the loop and adjust values in it as needed:

Code: Select all

require_once "../tools/Swift-3.1.2-php5/lib/Swift.php"; 
require_once "../tools/Swift-3.1.2-php5/lib/Swift/Connection/SMTP.php"; 
  
//Start Swift 
$smtp =& new Swift_Connection_SMTP("mail.myserver.com", 587); 
$smtp->setUsername("mylogin"); 
$smtp->setpassword("mypw"); 
  
$swift =& new Swift($smtp); 

$result = mysql_query("SELECT recipient_email FROM common_recipients WHERE list_id=$list_id"); 

// build email message 
$html_body = "<strong> message here </strong>"; 
$text_body = "message here"; 

//Create the message 
$message =& new Swift_Message(urldecode($message_subject)); 
//Add some "parts" 
$message->attach(new Swift_Message_Part($html_body, "text/html")); 
$message->attach(new Swift_Message_Part($text_body, "text/plain"));

while ($row = mysql_fetch_array ($result)){  
        //Now check if Swift actually sends it 
        if ($swift->send($message, urldecode($row['recipient_email']), urldecode($message_from_email))) echo "Sent"; 
        else echo "Failed"; 

}
Remember $message has various accessors and mutators such as setBody(), setSubject() etc. If you create $part as a separate object then attach() that object you can also adjust the values in $part in just the same way.

The Decorator plugin may be of interest if the messages are customized for each recipient :)

Posted: Sun Apr 15, 2007 4:19 pm
by bruno_m
Nice. Thanks for the advice.

That Decorator plugin looks very good. I'll try to use that.
In my messages, the only thing that will change is a link at the bottom to allow the users to unsubscribe. In this link I include the userid which is another database field, so I can use the attach() to just change that line at the bottom of each email. I believe!

Thanks.