user3496831 user3496831 - 1 year ago 85
PHP Question

How To make Bulk email Script using php

I am making a bulk email script for 1k user and I want to send email but my script gives me error, timeout.

How to handle page timeout. any suggestion for backend run this script.

If user clicks send button then run this script backend if user close this page but script working.

$queryUser=mysql_query("SELECT `email` From `users`",$conn);
$to = $User["email"];
$from = '';
$from_name = 'Test bulk email';
$subject = $_POST["subject"];
$html_content = $_POST["message"];
$from = $senderName." <".$senderMail.">";
$headers = "From: $from";
$headers = "CC:";
$headers = "BCC:";

$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";

$headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\"";

$message = "--{$mime_boundary}\n" . "Content-Type: text/html; charset=\"UTF-8\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n";

$query=mysql_query("SELECT * From `bulk_email_attchment` where `unique_id`='".$_SESSION["uniqid"]."' ",$conn);
$files = "Attchment/".$row["name"];
$message .= "--{$mime_boundary}\n";
$fp = @fopen($files,"rb");
$data = @fread($fp,filesize($files));
$data = chunk_split(base64_encode($data));
$message .= "Content-Type: application/octet-stream; name=\"".basename($files)."\"\n" .
"Content-Description: ".basename($files)."\n" .
"Content-Disposition: attachment;\n" . " filename=\"".basename($files)."\"; size=".filesize($files).";\n" .
"Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";

$message .= "--{$mime_boundary}--";
$returnpath = "-f" . $senderMail;

//send email
$mail = mail($to, $subject, $message, $headers, $returnpath);


Answer Source

The Right Way: Create a php Queue, with a Named Pipe, that connects to another php script that would send the emails each time it is invoked. If you are just staring php, it'll take a while before all this is coded properly.

The Moderate but not ideal way: Set up a Cron Job to invoke your script (that would send 20 or so emails each time invoked. If your using a service like Godaddy, just google: "CronJob godaddy setup"

The Hacky Way: Have the script in a page that redirects to itself with a query string updating it on how many emails were sent:

After 20 emails for instance are sent, use the code:

$count = 20;

I don't recommend using The Hacky Way because as a programmer, we should try to follow best practices, but I personally can't hold you back :)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download