Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

sending mass email with php 1

Status
Not open for further replies.

lastingforhours

Programmer
Dec 29, 2004
49
0
0
US
im needing to create a mass mailer in php for a newsletter that is going to be sending out to 1000+ mailing list subscribers.

my first attempt to send a mass email blast was to create a form that once submitted, uses a loop that takes all subscriber email addresses from a database and then sends the newsletter to each subscriber. but doing this resulted in the page taking a looooooooong time to reload once the form was submitted.

is there a way around the long, long reload time? or are there any good resources on email blasting that anyone can link me to? thank you!

--
Matt Milburn, Wave Motion Studios
 
Assume that the email addresses are in a table in a MySQL database. That database also has a table for mailout jobs.

I've done this as a thress-script system: a launcher, a monitor, and a mailer.

The launcher does three things: it creates a new job in the jobs table for the current new job; it sets up the mailer script to run as a separate process using the server's AT system (to run a script at a certain time, although that time is immediately) and passes the mailer script the job number on the command line and the message through either the command line or the jobs table in MySQL; and it forwards the browser to the monitor script using a URL like: /monitor.php?jobnum=1234

The monitor does one thing: using a jobnumber passed to it on the URL, it checks the jobs table in the database and reports the status of that job. It runs very small and fast and can be refreshed as often as necessary.

The mailout script does two things as it runs via the server's AT system: it runs through the list of email addresses in the addresses MySQL table, sending an email to each one; it periodically updates the job's entry in the MySQL table (I recommend that it not update the job after sending every email -- for 1000 emails, once every 10th message will do nicely.).


This way, the browser connection that sets the whole process going doesn't have to wait for it to finish, and you have a method of keeping track of how far things have progressed.

It is possible to combine the launcher and monitor scripts, but I generally write separate scripts.



Want the best answers? Ask the best questions! TANSTAAFL!
 
Me personally, I'd do something similar to sleipnir214. I'd create a perl script to perform the mail build and send, and have php do a shell_exec to the perl.

Mark
 
Why would you build this if there are dozens of very competent mailing list programs? ezmlm, majordomo, etc...

D.E.R. Management - IT Project Management Consulting
 
What about doing this :

1) PHP script launched in a new window ( via _blank target )
2) get records number
3) get the database IDs at every 100 records
4) store the IDs in session
5) start the mass-mailing
6) at every stored ID, pause a little bit and redirect via header for the sending of the next chunk.

The advantage is that you can monitor the sending and that it's in a separate window, so you can continue your work in the main window if you want. No need to wait stupidely ;)
Also, not all servers can send so many emails at once.
Even the well known mass-mailers use a pause.

Well, I will need to make my own script soon and it's the strategy I plan to use. I don't pretend it's the holy grail! ... far from it :)
 
I think that having a web-connected script do the mailing is not a good idea. Too many things can kill your script in the middle of mailing out a chunk.



Want the best answers? Ask the best questions! TANSTAAFL!
 
Hi :)

Do you have any practical example in mind?

I'm asking this because most of the newsletter applications that are available nowadays use web-connected scripts.

Also, if in the recipients database, you store a variable that tells whether or not the email was sent to a particular recipient, then, in case of problem, you could re-apply the sending process only to the remaining recipients.

What do you think?
 
That's pretty much what I've done. On a smaller scale.

I wrote a helpdesk app for my work. When a ticket is added/updated, it checks to see if...
1. A new assignment is added
2. Someone is monitoring for the application
3. The status is Critical or another requiring a blast e-mail to the dept.

Once I have my list of users, I build the e-mail, then loop through and mail to each. This is all done through php. Once the e-mail is sent to the new assignment, I update their assignment record with a 1 in the assign_emailed field. This way it won't e-mail them again for the same ticket if another user is added or it gets updated.

Here is the function I use to mail. There are some other functions used, but you'll get the idea.

Code:
function mailTrackerItems( $id ) {
GLOBAL $AppUI, $locale_char_set;
$isv = dPgetSysVal( 'HelpDeskSeverity' );
$ist = dPgetSysVal( 'HelpDeskStatus' );

$sql = "SELECT * from helpdesk_items WHERE item_id = $id";
db_loadHash( $sql, $items );

$sql = "SELECT assign_to,CONCAT_WS(',',assign_status,assign_to,assign_emailed) from helpdesk_assign WHERE assign_id = $id";
$assigned = db_loadHashList( $sql );

$sql = "SELECT * from helpdesk_detail WHERE detail_id = $id";
$detail = db_loadList( $sql );

$sql = "SELECT user_id,user_email from users";
$email = db_loadHashList( $sql );

$sql = "SELECT user_id,CONCAT_WS(' ',user_first_name,user_last_name) from users";
$username = db_loadHashList( $sql );

$sql = "SELECT app_id,app_desc from applications";
$apps = db_loadHashList( $sql );

$sel_app = $items["item_application"];
$sql = "SELECT monitor_id,CONCAT_WS(',',monitor_id,monitor_app) mon_app from helpdesk_app_monitor WHERE monitor_app=$sel_app";
$mon_app = db_loadHashList( $sql );

$mail = new Mail;
		$mail->Subject( "Workorder# " . $id);
		$body = "Requestor: " . $items["item_requestor"];
		if ($items["item_requestor_email"] != "") {
		$body .= "\n"."Email: " . $items["item_requestor_email"];
		}
		if ($items["item_phone"] != "") {
		$body .= "\n"."Phone/Ext: " . $items["item_phone"];
		}
		$body .= "\n";
		$body .= "\n"."Assigned To/Status: ";
		foreach ($assigned as $k => $v){
		$ass_explode = explode(',', $assigned[$k]);
		$body .= "\n". "    " . $username[$ass_explode[1]] . ' - ' .$ist[$ass_explode[0]];
		}
		$body .= "\n";
		$body .= "\n"."Severity: " . $isv[$items["item_severity"]];
		if ($assigned["item_needed_by_date"] != "") {
		$body .= "\n"."Needed by: " . $items["item_needed_by_date"] . " " . $items["item_needed_by_time"];
		}
		$body .= "\n"."Problem summary: ";
		$body .= "\n".$items["item_summary"];
		$body .= "\n";
		$body .= "\n"."Resolution: ";
		foreach ($detail as $k => $v){
		$body .= "\n". '[' . $username[$detail[$k]["detail_assign_to"]] . ' - ' . $detail[$k]["detail_datetime"] . ']';
		$body .= "\n".$detail[$k]["detail_resolution"];
		}
		$mail->Body( $body );
		
		//e-mail to assigned users and mark as e-mailed
		foreach ($assigned as $k => $v){
		$ass_explode = explode(',', $assigned[$k]);
		$e_status = $ass_explode[0];
		$e_userid = $ass_explode[1];
		$e_emailed = $ass_explode[2];
		
		if ($e_emailed != 1){
		$mail->From ( 'HelpDesk@whapache.org' );
		$mail->To( $email[$e_userid], true );
		$mail->Send();
		
		$sql = "UPDATE helpdesk_assign 
			SET assign_emailed=1
			WHERE assign_id=$id AND assign_to=$e_userid";
		db_exec($sql);
		}
		}
		
		//e-mail every time to users monitoring apps
		foreach ($mon_app as $k => $v){
		$mon = explode(',', $mon_app[$k]);
		//echo '<pre>';print_r($mon);echo "</pre>";die;
		$m_userid = $mon[0];
		$m_app = $mon[1];
		
		$mail->From ( $apps[$m_app] . '@whapache.org' );
		$mail->To( $email[$m_userid], true );
		$mail->Send();
		}
		
		//e-mail is-department@wheelinghospital.com every time for Critical workorders
		if ($items['item_severity'] == 5){
		$mail->From ( 'Critical_Workorder@whapache.org' );
		$mail->To( 'is-department@wheelinghospital.com', true );
		$mail->Send();
		}
		
		//e-mail is-managers@wheelinghospital.com every time for Administration workorders
		if ($items['item_severity'] == 6){
		$mail->From ( 'Administration_Workorder@whapache.org' );
		$mail->To( 'is-managers@wheelinghospital.com', true );
		$mail->Send();
		}
		
		
}

It usually takes less than 1 second for the e-mail to hit my inbox after the Submit button is hit.

Mark
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top