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

<?php
// call variables
$txtSubject=$_REQUEST["txtSubject"];
// call emailbody
$editor1=$_REQUEST["editor1"];
 
if($txtSubject!='' AND $editor1!='') {
// select admin name and email from database
		$sql2="select * from admin";
		mysql_query($sql2);
		$check2=mysql_query($sql2);
		$no2=mysql_num_rows($check2);
 
                $AdmName=mysql_result($check2,0,"adminname");
		$AdmEmail=mysql_result($check2,0,"adminemail");
 
		$subject = $txtSubject;
		$mailfrom=$AdmEmail;
		$mailfrom2="AnyName <info@yourwebsite.com>";
 
/////// starting
$sqlUE="select * from allusers WHERE userban='No' AND userstatus='Verified' AND issubscriber='Yes'";
$checkUE=mysql_query($sqlUE);
$noUE=mysql_num_rows($checkUE);
////////// if started
if($noUE > 0 ) 
{ 
////////// loop started
for($i=0; $i<$noUE; $i++) {
 
// set script execution time limit in seconds 
set_time_limit(30);
// set memory limit
ini_set('memory_limit','90M');
 
$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: yourwebsitename.com";
$mmessage = "
Hello, <b>$ddName</b>
<br /><br />
$editor1
<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='http://www.yourwebsite.com'>http://www.yourwebsite.com</a>
";
@mail($mailto, $subject, $mmessage, $headers);
 
// sleep 1 second after emailing one user and then proceed to another in loop
sleep(1);
}
// 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,
MailMan,
PHP Mailer,
Swift Mailer
or
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: