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 = "[email protected]"; // SMTP password $Password = "your-smtp-password"; // SMTP username $From = "[email protected]"; $FromName = "From Name"; $Tos = array( "To Name 1" => "[email protected]iddev.com", "To Name 2" => "[email protected]" ); $Ccs = array( "CC Name 1" => "[email protected]", "CC Name 2" => "[email protected]" ); $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