Hi

I need a solution for sending an email newsletter to a database of clients. Is there a script which will allow me to do this. I have read a lot about servers timing or my browser doing the same... if the script executes for too long. I am looking for a solution which this issue won't conflict with.

Thanks
lworks

Dani AI

Generated

Practical, production-safe approach: avoid trying to send a large newsletter inside a single browser request. The fastest path to solving the timeout/hosting-limit problem is to persist each message/recipient into a queue (database table) and have a small CLI sender run in the background (cron or systemd timer) that pulls a limited batch, sends each recipient individually via authenticated SMTP (PHPMailer/SwiftMailer), updates status, and throttles itself. The off-the-shelf suggestions from and (PHPlist, PoMMo) are fine if a separate list manager is acceptable; if tight integration is required, follow the queue+cron pattern referenced by rather than long web requests or huge Bcc lists shown by /.

Minimal workflow (practical checklist)

  • Queue: insert rows into email_queue (to_address, subject, body, status, attempts, created_at).
  • Sender: run from CLI via cron every few minutes, SELECT a small LIMIT (e.g. tens–low hundreds) WHERE status='queued'.
  • Send: use authenticated SMTP, send per-recipient (not one giant Bcc), update status to sent/failed, increment attempts, move to suppression after N failures.
  • Deliverability: publish SPF/DKIM/DMARC, include a real From/Reply-To and a List-Unsubscribe header, honor unsubscribe requests and process bounces.
  • Throttle & safety: sleep between sends, record logs, use a lock file to avoid overlapping runs, and respect the host/provider sending limits (shared hosts often limit rate).

Example cron + skeleton (concept)

*/5 * * * * /usr/bin/php /path/to/send_queue.php >> /var/log/send_queue.log 2>&1

The CLI send_queue.php should load queue rows, send each with PHPMailer over SMTP, update DB status, and sleep briefly between sends. Final cautions: test with small seed lists (Gmail/Outlook), monitor bounces and blacklists, and use double opt-in to keep list quality high.

Recommended Answers

All 10 Replies

The following is an example based on a php.net example. It just simply uses a coma (,) to add more emails and additional headers for blind carbon copy (bcc) where people can't see what other people received the same email.

<?php
// multiple recipients
$to  = 'aidan@example.com' . ', '; // note the comma
$to .= 'wez@example.com';

// subject
$subject = 'Test subject';

// message
$message = 'This is the message';

// Additional headers
//Blind Carbon Copy emails
$headers = 'Bcc: user@example.com' . "\r\n";

// Mail it
mail($to, $subject, $message, $headers);
?>

So as you can see in the example above, all you need to do in the to field is add a coma to separate the emails. But I am not too sure about the Blind Carbon Copy (Bcc) though. Good luck and hope the extra info helps as I hear sending mass email can slow down delivery time due to spam scanning issues.

Thanks cwarn23, appreciate the info. Would I be able to add a number of email addresses into the $headers?

if you want a script to send to multiple emails:

$sql = "select * from admin_email";
$query = mysql_query($sql);
$num = mysql_num_rows($query);
while($row = mysql_fetch_array($query))
{
	$to = $row['emails'];
	$parts = split(',',$to);
	$headers = 'From: [email]example@site.com[/email];';
	$headers .= 'MIME-Version: 1.0' . "\r\n";
	$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
	$subject = $row['subject'];
	$content = $row['content'];	
}

	$i=0;
	$errors = 0;
	$mail_err = '';
	while ($parts[$i]!='')
	{
		if(mail($parts[$i],$subject,$content,$headers))
		{
			
		}
		else 
		{
			$errors=1;
			$mail_err = $mail_err."<br>".$parts[$i];
		}
		$i++;
	}

as for the timing, you can use an ajax count down script which is easy to make.

Thanks cwarn23, appreciate the info. Would I be able to add a number of email addresses into the $headers?

If you are talking about the Bcc header (Blind Carbon Copy) then since at the moment I don't have access to my localhost server, I will have to say that one of the following two code snippits will do the job. But unsure if both will work.

$headers .= 'Bcc: emailfirst@example.com' . "\r\n";
$headers .= 'Bcc: emailsecond@example.com' . "\r\n";
$headers .= 'Bcc: another@domain.com' . "\r\n";
$headers .= 'Bcc: mine@gmail.com' . "\r\n";

or if the above doesn't work.

$headers .= 'Bcc: emailfirst@example.com, emailsecond@example.com, another@domain.com, mine@gmail.com';

Hope that answers your question as I am unsure if the above code works.

Hi this is nathen,

Just see ithink its usefull for you

$arr=$_POST['allusrs'];
			      foreach($arr as $tt)
				   {
				  	$query3="select email from cfair_adminusers where admin_id=".$tt;
					$var3=mysql_query($query3);
					$bh=mysql_num_rows($var3);
					$arrayemail=mysql_fetch_array($var3);
		            $emailid=$arrayemail['email'];
				    $to = $emailid;
					$subject = $subject ;
					$message=$message;
				  
				   if($_FILES['filename']!='')
				   {
					   $tmp_name = $_FILES['filename']['tmp_name'];
					  
					   $type = $_FILES['filename']['type'];
					   $name = $_FILES['filename']['name'];
					   $size = $_FILES['filename']['size'];
					   if (file_exists($tmp_name))
					   {
						  if(is_uploaded_file($tmp_name)){
						  $file = fopen($tmp_name,'rb');
				          $data = fread($file,filesize($tmp_name));
						 fclose($file);
						 $data = chunk_split(base64_encode($data));
						 }
					} 
					 $headers .= 'From:superadmin@couponsfair.com' . "\r\n" .
						"MIME-Version: 1.0\r\n" .
						 "Content-Type: multipart/mixed;\r\n" .
						 " boundary=\"{$mime_boundary}\"";
				 	$headers .= 'Bcc: [email]admin@couponsfair.com[/email]' . "\r\n";
								'Reply-To: [email]seo@.spwebsite.com[/email]' . "\r\n" .
								'X-Mailer: PHP/' . phpversion();
		   
					 $message .= "This is a multi-part message in MIME format.\n\n" .
					 "--{$mime_boundary}\n" .
					 "Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
					 "Content-Transfer-Encoding: 7bit\n\n" .
					 $message . "\n\n";
					 $message .= "--{$mime_boundary}\n" .
					 "Content-Type: {$type};\n" .
					 " name=\"{$name}\"\n" .
					 "Content-Disposition: attachment;\n" .
					
					 "Content-Transfer-Encoding: base64\n\n" .
					 $data . "\n\n" .
					 "--{$mime_boundary}--\n";
	
					if (@mail($to, $subject, $message, $headers))
					{
						 echo "Message Sent";
						 header("location:send_newsletter.php");
					 }
					else
					{
						 echo "Failed to send";
					}
	   			}
			 
                                             }

How much is alot of people? Most reseller packages would crash after 100 iterations of email sending and many would breach the terms of service so the quantity is imperative. I actually am battling with the same question. I know I can mass mail using cron jobs, sending so many out every fifteen minutes, but that just seems like a difficult way to go about it. I had looked over phpMailer, but I would rather this sending script to be a small app that could be integrated into another system I had developed. Any suggestions?

I'd say a lot of people is very open to interpretation. In my experiences with this it is very easy to get an ip or domain blacklisted and difficult to get it unlisted.

If you're looking for mass mailing capabilities and it needs to be built in php than using SMTP and a queue of sorts to send n emails every x mins is probably the best way to handle this. CRON would be unavoidable if you want something that is actually done in measured increments.

Perhaps look into a variety of mass mailing services which will obviously handle this for a cost, pay by email, pay by subscriber etc. In my experiences with these types of services, their deliverability rates are exceptional and they provide so much more beyond just sending emails. Most of these services have API's and will offer you a developer account to integrate their api into your system. From within your system, have a common mailing interface and provide your users with various adapters that they can choose from with SMTP being a fallback if the user does not wish to pay for a service.

iContact, MailChimp and StreamSend all come to mind but there are probably hundreds if not more, services like these.

Wow... this thread is old...

Since posting I think I settled on PHPlist, as suggested above, I wasn't able to integrate it with my system, although I didn't even try - I was quite a noob back then (some might say I still am). But PHPlist is great, totally recommendable.

lowrks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.