Send email to selected users using PHP

Hello friends, today I come back with another small application, send mail to selected user using PHP. Suppose you have a newsletter subscription form in your website. Guest user or register user will subscribe to your newsletter. Now you want to send your content to your subscribe user.When they subscribe to your newsletter, they put their email address and this email address will store your database table. Using this email address you can promote your site link to subscribe user. Using this application you can send email to multiple user at a time.

Send email to selected users process below

Step 1: Fetch all email address from database and check, uncheck email address using javascript



<!DOCTYPE HTML>
<html lang="en-US">
<head>
	<meta charset="UTF-8">
	<title>Send Mail To Selected User Using PHP</title>
	<script type="text/javascript" language="javascript">// <![CDATA[
	function checkAll(ele) {
		var checkboxes = document.getElementsByTagName('input');
		if (ele.checked) {
			 for (var i = 0; i < checkboxes.length; i++) {
				 if (checkboxes[i].type == 'checkbox') {
					 checkboxes[i].checked = true;
				 }
			 }
		} else {
			 for (var i = 0; i < checkboxes.length; i++) {
				 console.log(i)
				 if (checkboxes[i].type == 'checkbox') {
					 checkboxes[i].checked = false;
				 }
			 }
		}
	}
	</script>
</head>
<body>
	<form name="form1" method="post" action="send.php">
	<?php

	mysql_connect('localhost','root','');
	mysql_select_db('db_newslettrer');
	// create table if needed
	$query = "CREATE TABLE IF NOT EXISTS emailList (				
	`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,				
	`email` VARCHAR( 100 ) NOT NULL,				
	PRIMARY KEY ( `id` ) ,				
	UNIQUE ( `email` )				
	)";
	$result = mysql_query($query);
	if (!$result) die('mySQL error: ' . mysql_error());
	// fetch email
	$query = "SELECT * FROM emailList";
	$result = mysql_query($query);
	if (!$result) die('mySQL error: ' . mysql_error());
	echo "<style>	
	table { border:1px solid #ccc; border-collapse:collapse; padding:5px; }	
	th { background:#eff5fc; padding:10px; text-align:center; }	
	td { padding:10px; }	
	</style>";
	echo "<table>";
	echo "<th><input type='checkbox' onchange='checkAll(this)' name='chk'/></th><th>ID</th><th>Email</th>";
		
	if (mysql_num_rows($result) == 0) {    
	echo "<tr><td colspan='3'>No emails in database.</td></tr>";    
	} 

	while ($row = mysql_fetch_assoc($result)) {     
		echo "<tr><td><input value='".$row['email']."' type='checkbox' name='check[]'/></td>";	
		echo "<td>".$row['id']."</td>";
		echo "<td>".$row['email']."</td></tr>";
	} 
	echo "</table>";
	?>
	<p>Subject:<input type="text" name="subject" value="" /></p>
	<p>Message:<textarea name="message" cols="40" rows="6"></textarea></p>
	<input type='submit' name='submit' value='SUBMIT'/>
	<?php
	mysql_close(); 
	?>
	</form>
</body>
</html>

Step 2: Mail sends code: Here I just catch all the data using POST method and Send mail to checked email address using loop

<?php
if(isset($_POST['check']) == true)
{
	//print_r($_POST);
	$subject = trim($_POST['subject']);
	$message = trim($_POST['message']);
	foreach($_POST['check'] as $k => $v)
	{
		//This will open to send mail
 		if(@mail($v,$subject,$message,'Form:abhijit@technokrats.in'))
		{
			echo 'Mail send to '.$v.'</br>';
		} 
	}
}
else
{
	echo 'Please select email address';
}
?>

Demo
Download

Spread the love
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •