PHP Send Email To Multiple Recipients And CC To Multiple Recipients With PHP Mailer
Posted by in PHP July 21, 2011 3 Comments

In almost web application, sending email is one of most important function which helps to communicate with your customers/visitors.

This tutorial will show How to send the same message TO multiple recipients and CC to multiple recipients in one email sending process. It reduces your server resource consumption and avoid repeat the same function again and again.

Source code below uses PHPMailer as a email transport class via an Authenticated SMTP Server.

Send email and CC to multiple recipients with SMTP authentication by PHP

<?php
	include "class.smtp.php";
	include "class.phpmailer.php";
 
	$Host = "mail.yourdomain.com";						// SMTP servers
	$Username = "your-smtp-username@yourdomain.com";	// SMTP password
	$Password = "your-smtp-password";					// SMTP username
 
	$From = "from-email@yourdomain.com";
	$FromName = "From Name";
 
	$Tos = array(
		"To Name 1" => "to-email-1@4rapiddev.com",
		"To Name 2" => "to-email-2@gmail.com"
	);	
	$Ccs = array(
		"CC Name 1" => "cc-email-1@yahoo.com",
		"CC Name 2" => "cc-email-2@gmail.com"
	);
 
	$Subject = "Hello there";
	$Body = "This is a test email which will send to multiple recipients";
 
	$mail = new PHPMailer();
 
    $mail->IsSMTP();                 	// send via SMTP
    $mail->Host     = $Host; 
    $mail->SMTPAuth = true;     		// turn on SMTP authentication
    $mail->Username = $Username;  
    $mail->Password = $Password; 
 
    $mail->From     = $From;
    $mail->FromName = $FromName;
	foreach($Tos as $key => $val){
		$mail->AddAddress($val , $key); 
	}
 
	foreach($Ccs as $key => $val){
		$mail->AddCC($val , $key); 
	}
 
    $mail->WordWrap = 50;				// set word wrap
	$mail->Priority = 1; 
    $mail->IsHTML(true);  
    $mail->Subject  =  $Subject;
    $mail->Body     =  $Body;
    if(!$mail->Send())
    {
        echo "Mailer Error: " . $mail->ErrorInfo;
    }
    else
    {
        echo 'Message has been sent.';
    }
?>

Download the source code above which includes PHPMailer files

Hoan Huynh is the founder and head of 4rapiddev.com. Reach him at hoan@4rapiddev.com
  • Pingback: PHPMailer Send Email With Attachments Via SMTP Authentication | 4 Rapid Development

  • antony

    this works very very well, took me two days to locate such helpful information :-(
    thanks a lot.

  • http://www.medicareplanstoday.com Medicare Plans

    Quick question.. Do you know how I can send a different message and different subject to different people at once?

    I’m working for an insurance company, and we need to send 1 email to the new lead, and 1 email to the lead manager w/ that lead’s information…

    What’s the best way of doing this?