Send multiple emails at once using PHPMailer



In this tutorial I will show you how to send multiple emails at once using PHPMailer.

Example Code:

In following example we have an array of email and content to be sent. So we have created an object on PHPMailer and sending email in for loop.

require('PHPMailer/class.phpmailer.php');

$emailArray = array("email1@gmail.com","email2@gmail.com","email3@gmail.com");
$bodyArray = array("mail content for email1@gmail.com","mail content for email2@gmail.com", "mail content for email3@gmail.com");

$mail = new PHPMailer();
$mail->IsMail();
$mail->Timeout  = 360;

$totalEmails = count($emailArray);
for($i=0; $i<$totalEmails; $i++) {

	$mail->From = "username@gmail.com";
	$from = "User Name";
	$mail->FromName = $from;	  
	
	$Email = $emailArray[$i];		
	$mail->AddReplyTo($Email, $from);		
	
	$to = $Email;
	$mail->AddAddress($to, "");		
	
	$mail->IsHTML(true);
	$mail->Subject =  'multiple email'; 		
	
	$mail->Body  = $bodyArray[$i];
	$mail->Send();
	
	$mail->ClearAddresses();
}

Now most important part of this example is at the end. If you not mention “$mail->ClearAddresses();” , Then all email id except the last email will get multiple emails instead of 1 email.

$mail->ClearAddresses() is used to Clear all To recipients. If $mail->ClearAddresses() is not working, you can use $mail->ClearAllRecipients(); to Clear all recipient types.

Hope this tutorial will help you to send multiple email at once using PHPMailer.