简体   繁体   中英

Sending multiple email from same page phpmailer

i am trying to send email to user and to admin at the same time using phpmailer but only one email is sent.. either to user or to admin..

i have tried every possible way with no luck. please help

below is my code.

$html.="This is my message to user";

$bcc = "xxx@gmail.com";
$to= $clientemail;
$subject = "This is my subject;

$host = $SmtpServer;
$username = $SmtpUser;
$password = $SmtpPass;

$mail = new PHPMailer();


$mail->IsSMTP();
$mail->Host = "$host";  // specify main and backup server
$mail->SMTPAuth = true;     // turn on SMTP authentication
$mail->Username = $username;  // SMTP username
$mail->Password = $password; // SMTP password
$mail->Port = 587;         
$mail->SMTPSecure = 'tls';    
try {
$mail->From = $from;
$mail->FromName = $Name;
$mail->AddAddress(''.$to.'');
$mail->AddBCC('xxx@gmail.com');
$mail->WordWrap = 50;
$mail->IsHTML(true);
$mail->Subject = $subject;
$mail->Body    = $html;
$mail->AltBody = $html;
$mail->Send();
    // echo "Message Sent OK<p></p>\n";
    } catch (phpmailerException $e) {
    echo $e->errorMessage(); //Pretty error messages from PHPMailer
    } catch (Exception $e) {
    echo $e->getMessage(); //Boring error messages from anything else!
     }

$message="Thank you for your request.";

$html="this is my second message to admin";

$bcc = "xxx@gmail.com";
$to= $admin;
$subject = "message to admin;

$host = $SmtpServer;
$username = $SmtpUser;
$password = $SmtpPass;

$mail2 = new PHPMailer();


$mail2->IsSMTP();
$mail2->Host = "$host";  // specify main and backup server
$mail2->SMTPAuth = true;     // turn on SMTP authentication
$mail2->Username = $username;  // SMTP username
$mail2->Password = $password; // SMTP password
$mail2->Port = 587;         
$mail2->SMTPSecure = 'tls';    
try {
$mail2->From = $from;
$mail2->FromName = $from;
$mail2->AddAddress(''.$to.'');
$mail2->AddBCC('xxx@gmail.com');
$mail2->WordWrap = 50;
$mail2->IsHTML(true);
$mail2->Subject = $subject;
$mail2->Body    = $html;
$mail2->AltBody = $html;
$mail2->Send();
    //  echo "Message Sent OK<p></p>\n";
    } catch (phpmailerException $e) {
   echo $e->errorMessage(); //Pretty error messages from PHPMailer
   } catch (Exception $e) {
   echo $e->getMessage(); //Boring error messages from anything else!
   }

I'm not sure if you want to send the same email or not to differents persons, but the first improvement to do is to factorize your code.

// here an example of a function to send the SAME email to differents persons
// $emails is an array
function sendMail($emails, $subjects, $msg) {
    $mail = new PHPMailer();
    $mail->IsSMTP();

    try {
        $mail->Host = "$host";  // specify main and backup server
        $mail->SMTPAuth = true;     // turn on SMTP authentication
        $mail->Username = $username;  // SMTP username
        $mail->Password = $password; // SMTP password
        $mail->Port = 587;         
        $mail->SMTPSecure = 'tls';    

        $mail->From = $from;
        $mail->FromName = $Name;

        foreach ($emails as $to) {
            $mail->AddAddress($to);
        }

        $mail->AddBCC('xxx@gmail.com');
        $mail->WordWrap = 50;
        $mail->IsHTML(true);
        $mail->Subject = $subject;
        $mail->Body    = $html;
        $mail->AltBody = $html;

        if(!$mail->Send()) {
            // log if needed
        }
    } catch (phpmailerException $e) {
          echo $e->errorMessage();
    }
}

now you can use this function with differents messages or subjects... whatever you want!

EDIT :

to send different message to users and admins :

//settings for admin
$emails = array('admin@mail.com', 'admin2@mail.com'); // or just array('admin@mail.com');
$subjects = 'my sub';
$msg = 'my msg';

sendMail($emails, $subjects, $msg);

//settings for users
$emails = array('user@mail.com');
$subjects = 'different or not sub';
$msg = 'different or not msg';

sendMail($emails, $subjects, $msg);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM