how to send 2300 emails without errors?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

alexus
Forum Contributor
Posts: 159
Joined: Fri Jul 04, 2003 10:49 pm

how to send 2300 emails without errors?

Post by alexus »

Hi,,
I need advice..
I have 2300 emails in DB and I want to create Newsletter generator.... so the qiestion is this :

1. wil mail() function will be able to hanmdle this many emails in CC field or should I do LIMIT 0, 100 etc
2. Should I send the emails row by row? how can I work around with max exec time then?

Any advice? Thnx

And yea, I cant really play around because ppl might get angry of getting test emails he he
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

1) Read my sig.
2) Download SwiftMailer.
3) Install it.
4) Use it.
5) Celebrate over beers with d11wtq.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

... and here's a tip for once you've learned the basics:

http://www.swiftmailer.org/documentation/66#view
alexus
Forum Contributor
Posts: 159
Joined: Fri Jul 04, 2003 10:49 pm

Post by alexus »

Ok i just tried to use SWIFT for batch mailing with AntiFlood but its aint working :-(
(Works without antiflood though)

Can some one help me on tweaking the antiflood?

Here is what i did:

Code: Select all

<?php
require('../../Swift.php');
require('../../Swift/Connection/SMTP.php');
require('../../Swift/Plugin/AntiFlood.php');

$recipients = array(
	'myemail@example.com',
	'myemail@example.com'
);

$mailer = new Swift(new Swift_Connection_SMTP('mail.example.com'));
//If anything goes wrong you can see what happened in the logs 
$swift = new Swift($connectionObject);
//100 mails per batch with a 30 second pause between batches
$swift->loadPlugin(new Swift_Plugin__AntiFlood(100, 30));

if ($mailer->isConnected())
{
	//Add as many parts as you need here
	$mailer->addPart('Some plain text part');
	$mailer->addPart('Some HTML <strong>part with bold</strong> text', 'text/html');
	
	$mailer->AddBcc($recipients);
    
    //Make the script run until it's finished in the background
	set_time_limit(0); ignore_user_abort();
	echo "Close the browser window now...";
	flush(); ob_flush();

	
	//You can call authenticate() anywhere before calling send()
	if ($mailer->authenticate('username', 'password'))	{
    	//Leaving the body out of send() makes the mailer send a multipart message
		$mailer->send(
			'"Ultra Newsletter" <newsletter@example.com>',
			'"Ultra Newsletter" <newsletter@example.com>',
			'Some Subject'
		);
    }else echo "Didn't authenticate to server";
	
	$mailer->close();
}
else echo "The mailer failed to connect. Errors: <pre>".print_r($mailer->errors, 1)."</pre><br />
	Log: <pre>".print_r($mailer->transactions, 1)."</pre>";

?>
Im getting this error:
Fatal error: Call to a member function on a non-object in /home/elite/public_html/ultra/inc/Swift/Swift.php on line 297
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Are you using the right version of Swift for your PHP version?
alexus
Forum Contributor
Posts: 159
Joined: Fri Jul 04, 2003 10:49 pm

Post by alexus »

yap, i downloaded the PHP5 version first but then is didnt work even with basic mailing so I figured that "interface" class is not supportrd in v4 so now im using riht one... all works with authentication and batch mailing, but now I want to add anti-flood in the script and it throws me all kinds of errroes
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

You've instantiated swift twice and are using variables from pseudo code that don't even exist. You also have an extra underscore on the plugin name.

I don't know why you're sending to the all as Bcc recipients.... they just go in the "To:" list. Sending as Bcc with no To: address will likely be blocked as spam.

Remember to change the username and password and to also change the smtp server.

Code: Select all

<?php
require('../../Swift.php');
require('../../Swift/Connection/SMTP.php');
require('../../Swift/Plugin/AntiFlood.php');

$recipients = array(
        'myemail@example.com',
        'myemail@example.com'
);

$swift = new Swift(new Swift_Connection_SMTP('mail.example.com'));
//If anything goes wrong you can see what happened in the logs

//100 mails per batch with a 30 second pause between batches
$swift->loadPlugin(new Swift_Plugin_AntiFlood(100, 30));

if ($swift->isConnected())
{
        //Add as many parts as you need here
        $swift->addPart('Some plain text part');
        $swift->addPart('Some HTML <strong>part with bold</strong> text', 'text/html');
   
    //Make the script run until it's finished in the background
        set_time_limit(0); ignore_user_abort();
        echo "Close the browser window now...";
        flush(); ob_flush();

       
        //You can call authenticate() anywhere before calling send()
        if ($swift->authenticate('username', 'password'))      {
        //Leaving the body out of send() makes the mailer send a multipart message
                $swift->send(
						$recipients,
                        '"Ultra Newsletter" <newsletter@example.com>',
                        'Some Subject'
                );
    }else echo "Didn't authenticate to server";
       
        $swift->close();
}
else echo "The mailer failed to connect. Errors: <pre>".print_r($swift->errors, 1)."</pre><br />
        Log: <pre>".print_r($swift->transactions, 1)."</pre>";

?>
alexus
Forum Contributor
Posts: 159
Joined: Fri Jul 04, 2003 10:49 pm

Post by alexus »

Ok that works! thanks!

Also in documentation it says that it will run as the background proces? does it mean that if youser opens new php page in same window the process wouldnt be interapted?

Can I output some debug info?
like say: Sending mail now... olease wait... mail sent to 1100 users ... 3 adresses do not exist?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

What was that green reptilian flash! I think it was Swift Mailer Man! :D
(#10850)
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

alexus wrote:Ok that works! thanks!

Also in documentation it says that it will run as the background proces? does it mean that if youser opens new php page in same window the process wouldnt be interapted?

Can I output some debug info?
like say: Sending mail now... olease wait... mail sent to 1100 users ... 3 adresses do not exist?
RE backgrounding....

Yes the user can stop the page loading and navigate away without breaking the script. I wouldn't have a user executing a send to 2300 addresses though 8O

I've not tested it but apprently this works if you don't want the user to have to kill the page loading:

Code: Select all

<?php
require('../../Swift.php');
require('../../Swift/Connection/SMTP.php');
require('../../Swift/Plugin/AntiFlood.php');

$recipients = array(
        'myemail@example.com',
        'myemail@example.com'
);

$swift = new Swift(new Swift_Connection_SMTP('mail.example.com'));
//If anything goes wrong you can see what happened in the logs

//100 mails per batch with a 30 second pause between batches
$swift->loadPlugin(new Swift_Plugin_AntiFlood(100, 30));

if ($swift->isConnected())
{
        //Add as many parts as you need here
        $swift->addPart('Some plain text part');
        $swift->addPart('Some HTML <strong>part with bold</strong> text', 'text/html');
   
    //Make the script run until it's finished in the background
        set_time_limit(0); ignore_user_abort();
        header('Location: somewhere_else.php'); //Redirect from page

       
        //You can call authenticate() anywhere before calling send()
        if ($swift->authenticate('username', 'password'))      {
        //Leaving the body out of send() makes the mailer send a multipart message
                $swift->send(
                                                $recipients,
                        '"Ultra Newsletter" <newsletter@example.com>',
                        'Some Subject'
                );
    }else echo "Didn't authenticate to server";
       
        $swift->close();
}
else echo "The mailer failed to connect. Errors: <pre>".print_r($swift->errors, 1)."</pre><br />
        Log: <pre>".print_r($swift->transactions, 1)."</pre>";

?>
RE: Debugging

If you have swift mailer 2 then there's a method called getFailedRecipients() which you can run at the end of sending. This will provide you with an array of addresses which was rejected by the SMTP server for whatever reason (badly formatted, non-existent domain, no MX record etc etc...).
alexus
Forum Contributor
Posts: 159
Joined: Fri Jul 04, 2003 10:49 pm

Post by alexus »

if im not mistakken what you changed in the script is:
" header('Location: somewhere_else.php'); //Redirect from page "

which will just do the same as if the user just leave the page,,, but that would prevent them to cancel the page execution :-)

Ok I will play around and probably will get back in here!

Thanks for help!

PS: email will be send to 2300 users by admin he he... thats mass mailing to all our site members, hope we not going to spam list
alexus
Forum Contributor
Posts: 159
Joined: Fri Jul 04, 2003 10:49 pm

Post by alexus »

getting back about


getFailedRecipients ( )


how do I use that function? I tried just to do the output but got

Code: Select all

Fatal error: Call to undefined function: getfailedrecipients()

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

Post by Chris Corbyn »

alexus wrote:getting back about


getFailedRecipients ( )


how do I use that function? I tried just to do the output but got

Code: Select all

Fatal error: Call to undefined function: getfailedrecipients()

Please help!

Code: Select all

print_r($swift->getFailedRecipients());
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

/me would like to note he's used swift sending to 7000 emails =]
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
alexus
Forum Contributor
Posts: 159
Joined: Fri Jul 04, 2003 10:49 pm

Post by alexus »

ok, when I outputed the failed addreeses it jyst gave me number "1" which is I assume 1 bad address or error, and it is probably my hotmail account that for soeme rasont doesnt get the maill I send from SWIFT, but works just fine with the same SMPT account. a)Why that happence and gow can I debug? I dont want to loose all hotmail accounts thats about 500 ppl in my database

Thanks!
Post Reply