Last active
November 30, 2023 14:55
-
-
Save IftiMahmud/4af8ae39ca3fbac6d0de to your computer and use it in GitHub Desktop.
PHP Script to send mass email. This script will take subscribers emails from MySql database and send them bulk emails. Comes with Unsubscriber script.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//SOURCE: http://thedigilife.com/bulk-email-script-in-php-and-mysql-database/ | |
$con = mysql_connect("localhost","dbuser","dbpass"); // replace dbuser, dbpass with your db user and password | |
mysql_select_db("dbname", $con); // replace dbname with your database name | |
/* | |
To use this script database table must have three fields named sno, email and sub_status | |
*/ | |
$query = "select sno, email from dbtable where sub_status = 'SUBSCRIBED'"; | |
$result = mysql_query($query, $con); | |
$emails = array(); | |
$sno = array(); | |
while($row=mysql_fetch_assoc($result)){ | |
$sno[] = $row['sno']; // this will be used to unsubscribe the user | |
$emails[]=$row['email']; // email id of user | |
} | |
/* you can also get email id data from CSV using below code */ | |
//$file = file_get_contents("travel_database.csv"); | |
//$emails = explode(",",$file); | |
/* count.txt is used to store current email sent number/count */ | |
$count = file_get_contents("count.txt"); | |
for($i=$count;$i<count($emails);$i++) | |
{ | |
$to = $emails[$i]; | |
// subject | |
$subject = 'Set Your Title Here'; | |
// message | |
$message = file_get_contents("sample.html"); // this will get the HTML sample template sample.html | |
$message .= '<p><a href="http://yourdomain.com/path-to-folder/unsubscribe.php?id='.$sno[$i].'&username='.$emails[$i].'">Please click here to unsubscribe.</a></p> | |
</body> | |
</html>'; | |
// 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"; | |
// Additional headers | |
//$headers .= "To: $to" . "\r\n"; | |
$headers .= 'From: Name <[email protected]>' . "\r\n"; | |
//$headers .= 'Cc: [email protected]' . "\r\n"; | |
//$headers .= 'Bcc: [email protected]' . "\r\n"; | |
// Mail it | |
if(mail($to, $subject, $message, $headers)) { | |
$file = fopen("mailsentlist.txt","a+"); // add email id to mailsentlist.txt to track the email sent | |
fwrite($file, $to.",\r\n"); | |
fclose($file); | |
} | |
else | |
{ | |
$file = fopen("notmailsentlist.txt","a+"); // add email to notmailsentlist.txt here which have sending email error | |
fwrite($file, $to.",\r\n"); | |
fclose($file); | |
} | |
if(($i-$count)>=200) // this will send 200 mails from database per execution | |
{ | |
$filec = fopen("count.txt",'w'); // store current count to count.txt | |
fwrite($filec, $i); | |
fclose($filec); | |
break; | |
} | |
}//for end | |
$filec = fopen("count.txt",'w'); // store fine count to count.txt this will be used as a start point of next execution | |
fwrite($filec, $i); | |
fclose($filec); | |
?> | |
<?php | |
//if(($i-$count)>=100) // You can set the cron job on sendmail.php on particular time frame. For example if you hosting provider support only 100 mail per hour than you can set cron job par hour and update the value here. | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$con = mysql_connect("localhost","dbuser","dbpass"); | |
mysql_select_db("dbname", $con); | |
$sno = (integer)$_GET['id']; | |
$email = mysql_real_escape_string($_GET['username']); | |
$query = "update tablename set sub_status = 'UNSUBSCRIBED' where sno = $sno and email = '$email'"; | |
mysql_query($query); | |
echo "You have Successfully unsubscribed. Thank you for using the service."; | |
?> |
your script is picking the last item of the data in the column
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi ifti i need some help related this article can i have your email address?