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

Pingback: PHPMailer Send Email With Attachments Via SMTP Authentication | 4 Rapid Development