PHP NewsLetter Email to All Users – Customize Script Execution Time, Memory Limit and Break Interval

by Hiroshi on January 25, 2013

in Email Systems

This is not a foolproof system but its handy if users/subscribers are few. If you want to send an email (newsletter email) to all users then you can do like this.

Select admin name and admin email from db (not important)
Select users from db
// Start a loop //
Set memory limit and script execution time in loop
call one user and email him
Sleep for one second
Return in loop to get another user to email
// Loop returns //
Loop will continue until all users are emailed

// call variables
// call emailbody
if($txtSubject!='' AND $editor1!='') {
// select admin name and email from database
		$sql2="select * from admin";
		$subject = $txtSubject;
		$mailfrom2="AnyName <>";
/////// starting
$sqlUE="select * from allusers WHERE userban='No' AND userstatus='Verified' AND issubscriber='Yes'";
////////// if started
if($noUE > 0 ) 
////////// loop started
for($i=0; $i<$noUE; $i++) {
// set script execution time limit in seconds 
// set memory limit
$mailto = mysql_result($checkUE,0+$i,"useremail");
$ddName = mysql_result($checkUE,0+$i,"username");
// To send HTML mail, the Content-type header must be set
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= "From: ".$mailfrom2."\r\n";
$headers .= "Reply-To: ".$mailfrom."\r\n";
$headers .= "Return-Path: ".$mailfrom."\r\n";
$headers .= "Signed-by:";
$mmessage = "
Hello, <b>$ddName</b>
<br /><br />
<br /> <br />
You are receiving this email because you are a registered user at websitename and a subscriber to receive our newsletter. To unsubscribe from this newsletter click at the link below:
<br /> <br />
<a href='".$websiteURL."unsubscribe.php?email=$mailto'>Unsubscribe</a>
<br /> <br />
Best Regards <br /><br />
$AdmName<br />
$AdmEmail<br />
SiteName<br />
<a href=''></a>
@mail($mailto, $subject, $mmessage, $headers);
// sleep 1 second after emailing one user and then proceed to another in loop
// loop ends
// if ends
$message="Newsletter has been sent to all customers/users successfully.";
if($message!=='') { echo "$message"; }

HTML Form can be like this:

<form name="" action="<?php echo $PHP_SELF;?>" method="post">
<input type="text" style="width:40%;" class="textbox" value="" id="txtSubject" name="txtSubject">
<textarea class="ckeditor" name="editor1"></textarea>
<input type="submit" name="" value="Send">

Okay so this is a quick setup and small solution for few subscribers of newsletter.

Professionally you can use CRON JOBS. If you cannot use cron jobs use,
PHP List,
Mailing List Manager,
PHP Mailer,
Swift Mailer
PHP Scheduler (Cron Jobs Alternative).

You can alternatively think of a system that takes all user emails and stores them somewhere and then at every page load checks if there are email addresses and select few (10) and send then selected newsletter or something like that.

Related Posts

Previous post:

Next post: