running swiftmailer in the background (general help)

Swift Mailer is a fantastic library for sending email with php. Discuss this library or ask any questions about it here.

Moderators: Chris Corbyn, General Moderators

Post Reply
ToastRB
Forum Newbie
Posts: 2
Joined: Sun Apr 29, 2007 1:40 pm

running swiftmailer in the background (general help)

Post by ToastRB »

Jcart | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hi,

I have set up Swiftmailer to run as a function in an include file called from a webpage.  It is the basic Swiftmailer setup with an AntiFlood plugin throwing mail out every 60 seconds.  My desired result is to call the mailer function and then immediately redirect the page so that the call runs in the background.  I have tried a couple of different configurations and none of them seem to work (the page doesn't redirect until after 60 seconds when sending only 1 email):

Code: Select all

if($_POST['action'] == "mass_mail") {
	
	$someArgs = "blah blah blah";
	
	ignore_user_abort(TRUE);
	header('Location: index.php');

	massMailThroughSwift($someArgs);

}

Code: Select all

if($_POST['action'] == "mass_mail") {
	
	$someArgs = "blah blah blah";
	
	ignore_user_abort(TRUE);
	header('Location: index.php');

	echo "Trying to force server to output...";
	flush();
	ob_flush();

	massMailThroughSwift($someArgs);

}

Code: Select all

if($_POST['action'] == "mass_mail") {
	
	$someArgs = "blah blah blah";
	
	ignore_user_abort(TRUE);
	header('Location: index.php');

	register_shutdown_function("massMailThroughSwift",$someArgs);
	exit();

}
1.) Does know what's wrong with my php in general?
2.) What's a configuration I can use to get the page to redirect first and then run the call in the background?

My dev setup is MAMP, PHP5 and Swiftmailer 3.1.3.

Thanks.


Jcart | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

This is typically done by using a cron job. Essentially, you will create some form of "flag" in your database to determine that emails need to be sent, and having a cron job every 5 min or so to check if there are any emails pending that need to be sent.

It's impossible, as far as I'm aware, to fork processes into the background and continue on. After all, php is not multi threaded.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Jcart wrote:This is typically done by using a cron job. Essentially, you will create some form of "flag" in your database to determine that emails need to be sent, and having a cron job every 5 min or so to check if there are any emails pending that need to be sent.

It's impossible, as far as I'm aware, to fork processes into the background and continue on. After all, php is not multi threaded.
Well, terminology aside, you can "fork" processes, but you cannot multithread within one process :)

One approach you can use is the fsockopen() a script on your own server over HTTP. This script should contain set_time_limit(0) and ignore_user_abort(). Once you have made the request with fsockopen(), just foregt about it and end your script... the process will keep running in the background :)

CRON is a better solution though.
ToastRB
Forum Newbie
Posts: 2
Joined: Sun Apr 29, 2007 1:40 pm

Post by ToastRB »

Could you perhaps give me an example of how I would use fsockopen? I can't seem to figure out how to use it properly; nothing is happening and no errors are being logged.

I also tried exec() but that didn't seem to work either.

Thanks.
Post Reply