(Swift) PHP Mailer class with plugin support

Small, short code snippets that other people may find useful. Do you have a good regex that you would like to share? Share it! Even better, the code can be commented on, and improved.

Moderator: General Moderators

User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Ok guys, for those with the questions about sending mail to thousandsof recipients and don't want to use cron... here you go:

The anti-flood plugin two posts above this one has been updated. The second constructor parameter is an optional pause in seconds between re-connection.

Lets say you want to send say 10,000 emails in batches of 100 at a time with a 10 second pause between each 100 to give the poor SMTP server a rest.

A combination of set_time_slimit() and ignore_user_abort() with the anti-flood plugin makes this possible.

You'd do something like this and then open it in your browser. As soon as the script starts to run just kill your browser window and the mails will be sent in the background with a 10 second pause between each 100. This will take (obviously) a minimum of 990 seconds + the network overhead for the data to be sent but you don't need to sit and wait. I tested it by sending 1000 emails to my won address with a disconnect after each 10 and a 2 second pause. It worked well even when I closed the browser.

Code: Select all

set_time_limit(0);
ignore_user_abort(); //Vital maybe

$swift = new Swift($connection);

//100 mails per connection, 10 second pause between connections
$swift->loadPlugin(new Swift_Anti_Flood_Plugin(100, 10));

$recipients = array(); //Fill this with the 10,000 recipients you want

$swift->send($recipients, $your_address, $subject, $body);

$swift->close();
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

I finally got Xdebug working from Gentoo portage and I'm profiling the latest version (1.3.0 due for release tonight after this testing/optimizing) and before I even examine areas to be optimized most importantly Swift is performing at ~44% LESS peak memory than PHPMailer (1.73).

I'm going to benchmark a handful of different scenarios at low level usage to full-out trashing it and post the results on both the website and here (along with the code used). I'm also going to do a set of benchmarks focused on speed rather than resources.
Roja
Tutorials Group
Posts: 2692
Joined: Sun Jan 04, 2004 10:30 pm

Post by Roja »

d11wtq wrote:I finally got Xdebug working from Gentoo portage and I'm profiling the latest version (1.3.0 due for release tonight after this testing/optimizing) and before I even examine areas to be optimized most importantly Swift is performing at ~44% LESS peak memory than PHPMailer (1.73).
You now have my attention. :)
d11wtq wrote:I'm going to benchmark a handful of different scenarios at low level usage to full-out trashing it and post the results on both the website and here (along with the code used). I'm also going to do a set of benchmarks focused on speed rather than resources.
Seriously looking forward to it.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

44% less memory.. nice.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

The following data was obtained using Xdebug on PHP4 (I can't get Xdebug working on PHP5!) for PHPMailer 1.73 and Swift 1.3.0.

OK all tests were run 10 ten times each, the figures shown are the averages but I should point out that Xdebug appears to be pretty close to a set figure since most often, anything beyond the second run returns *exactly* the same memory amount to the byte.

Sending a (unique) plain text mail over SMTP N times without stopping for a rest

Image

Indicates a consistent 44% memory saving at 1, 10, 30, and 100 emails.

------------------------------------------------

Sending an attachment with a size of N Kilobytes (generated using linux dd command on /dev/zero)

Image

This seems to indicate that Swift won't go beyond a 20% saving on memory (it hits a tangent) in comparison to PHPMailer but at small attachment sizes it can be as high as 42%. A significant saving all the same.

------------------------------------------------

Speed tests. I don't trust these too much with SMTP since network traffic varies but here it is in any case - confusing the hell out of me. Most of the time used should be SMTP traffic but I have no clue what goes on in PHPMailer :? I used SMTPKeepAlive too but still a big gap.

Image

You can read the overall comparison here: http://www.swiftmailer.org/phpmailer/

EDIT | Interestingly I just received an email from a developer who's done some speed tests of his own with an older version. His results are here: http://www.swiftmailer.org/contrib/phpm ... mailer.doc

They seem to correlate with what I'm seeing too.... very odd indeed.
(I'll be adding that (minor) fix that he gave for the bounce detection too).
Last edited by Chris Corbyn on Sun Jun 25, 2006 8:31 am, edited 6 times in total.
printf
Forum Contributor
Posts: 173
Joined: Wed Jan 12, 2005 5:24 pm

Post by printf »

10,000 email through a PHP script, not very good idea. ALL MTA or SMTP servers have better ways to deal with this, (envelopes). It just seems like hacking to me. This is a nice class, but for mass mailing, I don't think so! It would be better to send (1) message then envelope the RCPT To, sending that one message to all 10,000 recipients! By using a envelope, you save the outbound server and the receiving server loads a bandwidth because domains are grouped in the sending of the message! Say your 10,000 user mailing list has 1,150 google recipients, then SMTP/MTA will send one message to the google SMTP/MTA with the envelope and then the google SMTP/MTA will distribute the message to all recipients using the envelope!

pif!
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

printf wrote:10,000 email through a PHP script, not very good idea. ALL MTA or SMTP servers have better ways to deal with this, (envelopes). It just seems like hacking to me. This is a nice class, but for mass mailing, I don't think so! It would be better to send (1) message then envelope the RCPT To, sending that one message to all 10,000 recipients! By using a envelope, you save the outbound server and the receiving server loads a bandwidth because domains are grouped in the sending of the message! Say your 10,000 user mailing list has 1,150 google recipients, then SMTP/MTA will send one message to the google SMTP/MTA with the envelope and then the google SMTP/MTA will distribute the message to all recipients using the envelope!

pif!
OK, no need to come across so strongly printf. I don't believe I'm supposed to have offended anybody. You're right in what you're saying but your assumption is that all these recipients are getting the same message... there aren't many companies that want to send exact copies of messages to all recipients. If you wanted to do what you're saying then you can set useExactCopy() to true and just throw in all RCPT addresses for a single email without sending the email multiple times.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Benchmarks sorted out - graphs used to display data.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

For those people who are relaying mail via Gmail SMTP servers and currently putting up with the re-written From: header (the reply-to still works) have a read of this:

http://www.mobileread.com/forums/showth ... eadid=4857
Roja
Tutorials Group
Posts: 2692
Joined: Sun Jan 04, 2004 10:30 pm

Post by Roja »

I meant to mention, as I commented on my blog, I'll be looking at switching BNT to use Swift instead of Phpmailer in the near-term future. I'll be in touch when I run into problems. (Notice, not IF, when). :)

[[EDIT: I meant, sarcastically, that my ability to follow even simple directions is non-ideal. Not that there was anything wrong with Swift. :P ]]
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Roja wrote:I meant to mention, as I commented on my blog, I'll be looking at switching BNT to use Swift instead of Phpmailer in the near-term future. I'll be in touch when I run into problems. (Notice, not IF, when). :)
Glad to hear it! Let me know how you get on ;)
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Awesome :) That's what I was hoping would happen :P

http://search.msn.co.uk/results.aspx?q= ... &FORM=QBHP

(Google still hasn't indexed it yet).
akreider
Forum Commoner
Posts: 46
Joined: Mon May 22, 2006 3:54 pm
Location: USA

utf-8 support?

Post by akreider »

Does this have utf-8 support? I'm getting my software to work in unicode so that it will work in 99% of the world's languages.

I'm not sure if you'd want to build-in utf-8 support, or if there is something that programmers should be doing before they get the email text into your script?

I've read something about needing to convert the email text to base64... I guess I'd want to be able to have the email text, subject header, and to: header in unicode.
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Post by jayshields »

I've never sent an email with an attachment via PHP. It's becoming confusing for me. I decided to use Swift, but I can't find any attachment examples where it sends an email with a user uploaded file; they are always server side files.

Does that mean I have to upload the file, add it as attachment, send the email, then delete the uploaded file? I'm not 100% sure how attachments work anyways - would I have to wait until the user has recieved the email and grabbed the attachment before deleting the uploaded file?

Thanks.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

jayshields wrote:I've never sent an email with an attachment via PHP. It's becoming confusing for me. I decided to use Swift, but I can't find any attachment examples where it sends an email with a user uploaded file; they are always server side files.

Does that mean I have to upload the file, add it as attachment, send the email, then delete the uploaded file? I'm not 100% sure how attachments work anyways - would I have to wait until the user has recieved the email and grabbed the attachment before deleting the uploaded file?

Thanks.
Uploading a file is nearly identical to having a "server side file" because they are both on the server. Also, the attached file is sent along with the email so you do not need to keep a copy of the file and await the user to download it.
Post Reply