,
Share this with your friends 
 

Code to send email messages to multiple persons using PHP

10 ratings Views 277 
Author: Mallika_51b07ae0 (Mallika M)  View Profile |  View other solutions by this author

Question / Problem


When we need to automatically send email messages to multiple persons using same template but different details within PHP without using mail merge we can use this solution .

Solution



******email.php***********


<?php include 'conn.php';?>
<?php
$date=$_POST['date'];
$month=$_POST['month'];
$year=$_POST['year'];
$date1= sprintf('%s-%s-%s', $date, $month, $year);
echo $date;
require 'PHPMailer/PHPMailerAutoload.php';
$array = array();
$array =$_POST['checkboxvar'];
// Include the PHPMailer class
include('PHPMailer/class.phpmailer.php');
// Retrieve the email template required
$message = file_get_contents('email_templates/register.html');
// Replace the % with the actual information
$message = str_replace('%date%', $date1, $message);
$mail= new PHPMailer();
$mail->IsSMTP();
$mail->Host = 'ssl://smtp.gmail.com:465';
$mail->SMTPAuth = TRUE;
$mail->Username = 'm********gmail.com';
$mail->Password = 'sre****ab'; // SMTP password
// Set who the email is coming from
$mail->SetFrom('ma********@gmail.com', 'Website Admin');
$a = sizeof($array);
for($i=0;$i<$a;$i++)
{
$id=$array[$i];
$sql = "SELECT DISTINCT empemail from timetable WHERE empid= '$id';";
$result = mysqli_query($conn,$sql);
if (mysqli_num_rows($result) > 0)
{
while($row = mysqli_fetch_assoc($result)) {

$email=$row['empemail'];
$mail->ClearAddresses();
$mail->AddAddress($email);
print ($mail->Send()) ? "Message sent to: " : "Message <b>sent</b> to: ";
print $email."<br />\n";

}
}

// Set the subject
$mail->Subject = 'Your account information';

//Set the message
$mail->MsgHTML($message);
//$mail->AltBody(strip_tags($message));

// Send the email
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
}

*****register.html*****
<html>
<body>
<p style="text-align: center;"><span style="font-size: 14pt;"><strong>&nbsp;&nbsp; CMS COLLEGE, KOTTAYAM</strong></span></p>
<p style="text-align: center;"><span style="font-size: 14pt;"><strong>(<em>AUTONOMOUS</em>)</strong></span></p>
<p>&nbsp;</p>
<p style="text-align: center;"><strong>First and Third Semester M.A/ M.Sc/ M.Com degree Examination &ndash; January 2017</strong></p>
<p>&nbsp;</p>
<p style="text-align: justify;"> You are appointed as Invigilator for the conduct of first and third Semester M.A/ M.Sc/ M.Com degree Examination January 2017. You are requested to take up the duty on the following date.</p>
<p style="text-align: justify;"><strong>%date%</strong></p>
<p><strong>Instructions</strong></p>
<ol>
<li>The examinations will commence at 9.30 a.m. in the forenoon (FN) and <span data-term="goog_324073013">1.30 p.m.</span> in the afternoon (AN). On Fridays the AN session will commence at <span data-term="goog_324073014">2.00 p.m.</span></li>
<li>The Invigilators shall report at the Office of the Chief Superintendent of the Examinations, atleast 30 minutes prior to the commencement of the Examination.</li>
<li>The Invigilators shall be present in the Examination Hall 15 minutes prior to the commencement of the Examination.</li>
<li><strong>The Invigilator shall open the Question Paper packet only at <span data-term="goog_324073015">9.20 a.m.</span> in the FN session and <span data-term="goog_324073016">1.20 p.m.</span> in the in the AN session (on Fridays AN session <span data-term="goog_324073017">1.50 p.m</span>)</strong>.<strong> &nbsp;&nbsp;</strong>Two students shall sign on the Question Paper packet as witness.</li>
</ol>
<p>Principal/ Chief Superintendent</strong></p>
</body>
</html>
}
?>


Applies to

PHP

Rank It

Login to rank it

Report


Advertisement