Prasad
Prasad

Reputation: 103

how to send mutiple emails in my code?

I need to send the email to the multiple recipients but I am getting error in my code. I need to send the email to multiple recipients.

<?php    
require 'phpmailer/PHPMailerAutoload.php';

if(isset($_POST['send']))
{
  $email = $_POST['email'];                    
  $password = $_POST['password'];
  $to_id = $_POST['toid'];
  $message = $_POST['message'];
  $subject = $_POST['subject'];

  $mail = new PHPMailer;

  $mail->isSMTP();

  $mail->Host = 'mail.domain.com';

  $mail->Port = 587;

  $mail->SMTPSecure = 'tls';

  $mail->SMTPAuth = true;

  $mail->Username = $email;

  $mail->Password = $password;

  $mail->setFrom('[email protected]', 'name');

  $mail->addReplyTo('[email protected]', 'name');

  $mail->addAddress($to_id);

  $mail->Subject = $subject;

  $mail->msgHTML($message);

  if(!$mail->send()) {
    $error = "Mailer Error: " . $mail->ErrorInfo;
  ?>
    <script>alert('<?php echo $error ?>');</script>
  <?php
  } 
  else {
    echo "Message Sent Successfully";
  }
}
?>

Upvotes: 0

Views: 55

Answers (3)

Rajesh Kumar
Rajesh Kumar

Reputation: 898

Try this code.

require 'PHPMailer/PHPMailerAutoload.php';
function SendPHPMail($to, $from, $subject, $htmlContent, $attachments = array())
    {
        $mail = new PHPMailer;
        $mail->isSMTP();
        $mail->Host = 'smtp.gmail.com';
        $mail->SMTPAuth = true;
        $mail->Username = '[email protected]'; 
        $mail->Password = 'password';
        $mail->SMTPSecure = 'tls';
        $mail->Port = 25;
        $mail->SMTPOptions = array(
                'ssl' => array(
                        'verify_peer' => false,
                        'verify_peer_name' => true,
                        'allow_self_signed' => true
                )
        );

        $mail->From = '[email protected]'; //sender emailAddress
        $mail->FromName = 'name'; //sender name

        //Here $to has multiple emailAddress
        //$to = array('[email protected]','[email protected]','[email protected]');
        if(!empty($to)){
            foreach($to as $emailAddress){
                $mail->addAddress($emailAddress);
            }
        } else{
            throw new \Exception('No emails found!');
        }

        if(!empty($attachments)){
            foreach($attachments as $attachment){
                $mail->addAttachment($attachment);
            }
        } 

        //$mail->addCC();

        $mail->WordWrap = 50;
        $mail->isHTML(true);

        $mail->Subject = $subject;
        $mail->Body    = $htmlContent;

         if(!$mail->send()) {
            throw new \Exception($mail->ErrorInfo);
        } 
    }

Upvotes: 2

devpro
devpro

Reputation: 16117

You can use just like that:

$mail->AddAddress('[email protected]', 'First Email');
$mail->AddAddress('[email protected]', 'Second Email');

Or if you have emails in an array $_POST['toid'], than you can use AddAddress() in a loop.

Upvotes: 0

Blueblazer172
Blueblazer172

Reputation: 600

instead of $mail->addAddress($to_id); use something like this:

$mail->addAddress("[email protected] , [email protected] , [email protected]", "...");

Upvotes: 0

Related Questions