How to send mail to multiple users?

I have a code where in I am raising a ticket based on the location that I choose. On selection of the location I have multiple recipients to whom the mail must be submitted but it gives me an error saying “You must select at least one mail recipient”.

$result6 = mysqli_query($conn, "select seat_no, desktop_id from it_support_hw_allocate where emp_id='".$emp_id."'");

                                list ($seat_no, $sys_no) = mysqli_fetch_array($result6);

//        send mail to the support team and to the employee

                                if ((preg_match('/(ERP\-M3|HRIS)/i', $subject)))
                                {
                                        $to = "[email protected], [email protected]";
                               }
							   if ((preg_match('/(SAP Support)/i', $category)))
                                {
                                        $to = "[email protected], [email protected]";
                                }
                                else
                                {
                                    if (($location == 'Chennai') || ($location == 'Bengaluru'))
                                    {
                                        $to = "[email protected],[email protected]";
                                    }
                                  
									if ($location == 'Hyderabad') {
									    $to = "[email protected], [email protected]";
									}
									if ($location == 'Bengaluru') {
									    $to = "[email protected], [email protected], [email protected]";
									}else
										{
										$to = "[email protected], [email protected]";
										}
                                    }
                                    echo "\n\n\n";
                                $headers[] = "From: IT Team";
                                $headers[] = $to;
                                $subject = "New Ticket raised ($nfname - $tgi - $uid)";
                                $msg = "
        
                                    Hello IT Team,

                                    $nfname $nlname has submitted a new support ticket \"$nsubj\".

                                    Ticket ID: $nuid
                                    Seat No: $seat_no
                                    System No: $sys_no
                                    Description: \"$comments\"------------- This is an auto-generated mail, please do not reply ---------------";
                                echo "Sending Mail 1";
                                    
                                sendMail($to, $subject, $msg, $headers, 'ITTS');

Below is the Mail function

function sendMail($to, $subject, $body, $headers, $toolName){
    require_once 'PHPMailer.php';
    require_once 'Exception.php';
    require_once 'SMTP.php';
    $err = array();
    $mail = new PHPMailer\PHPMailer\PHPMailer();
    // include "emails/PHPMailer/PHPMailerAutoload.php"; 
    $mail = explode(";", $to);
    //Create a new PHPMailer instance
    $email = $mail[0];
    $mail = new \PHPMailer\PHPMailer\PHPMailer();
    $mail->IsSMTP();
   
    // $mail->SMTPDebug = 1; 
    $mail->SMTPAuth = true; 
    $mail->SMTPSecure = 'tls'; 
    $mail->Host = "10.33.233.7";
    $mail->SMTPSecure = 'tls';
    $mail->Port = 25; 
    $mail->IsHTML(true);
    //Username to use for SMTP authentication
    $mail->Username = "Shruti U";
    $mail->Password = "Fh6e38kHPL";
    //Set who the message is to be sent from
    $mail->setFrom("Shruti", 'Tools');
    //Set an alternative reply-to address
    // $mail->addReplyTo('[email protected]', 'Software Developer');
    //Set who the message is to be sent to
    $mail->AddAddress($email, "Shruti Upari");
    $mail->AddBCC($email, "test");

    $mail->WordWrap = 50;  // set word wrap to 50 characters
    $mail->IsHTML(true); // set email format to HTML
    //Set the subject line
    $mail->Subject = $subject;
    //Read an HTML message body from an external file, convert referenced images to embedded,
    //convert HTML into a basic plain-text alternative body
    // $mail->msgHTML("convert HTML into a basic plain-text alternative body");
    //Replace the plain text body with one created manually
    $mail->Body = $body;


    //send the message, check for errors
    if (!$mail->send()) {
        echo "Mailer Error: " . $mail->ErrorInfo;
    } else {
        echo "Message sent!";
    }
}

PHPMailer stores primary addresses and CC addresses in the same array internally; since you’re adding $email as “to” and as “BCC”, this might be your problem.

Sponsor our Newsletter | Privacy Policy | Terms of Service