How to do email form with multiple recipients and

2020-03-08 06:13发布

i have one contact form, when user submit all value will send(email) to admin.But now i want to do when user submit admin will receive the email and user also will receive an email but with different body.

here my previous code :

<?php
if(md5($verif_box).'a4xn' == $_COOKIE['tntcon']){

$name= $_POST["name"];
$email= $_POST["email"];
$phone= $_POST["phone"];
$company= $_POST["company"];
$message= $_POST["message"];

require_once('lib/class.phpmailer.php');

$mail             = new PHPMailer(); // defaults to using php "mail()"

$mail->AddReplyTo("admin@gmail.com","I Concept");

$mail->SetFrom('admin@gmail.com', 'I Concept');

$mail->AddReplyTo("admin@gmail.com","I Concept");

$address = "admin@gmail.com";
$mail->AddAddress($address, "I Concept");

$mail->Subject    = "MY - Request a Quote";

$mail->AltBody    = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test

$mail->Body = "<strong>Request a Quote from I Concept Malaysia Website</strong><br><br> 

Name : $name<br>
Email : $email<br> 
Phone : $phone<br> 
Company : $company<br> 
Enquiry : $message<br> <br> 

Thank You!<br>

";

if(!$mail->Send()) {
  echo "Mailer Error: " . $mail->ErrorInfo;
} else {
  echo "Message sent!<br>";
}


}
?>

标签: php forms email
3条回答
欢心
2楼-- · 2020-03-08 06:49

I'm sure you can't send different bodies in one SMTP call. However you can just send the first email and initiate a new PHPMailer.

查看更多
Emotional °昔
3楼-- · 2020-03-08 06:51

Cloning the PHPmailer object is not necessary. Just use the ClearAllRecipients method that is built into PHPmailer before changing the body and sending the second email.

查看更多
疯言疯语
4楼-- · 2020-03-08 06:57

Try the following. Didn't test but you basically need to get another PHPMailer object going and set the body and to information separately.

$address = "admin@gmail.com";
$mail->Subject    = "MY - Request a Quote";

// keeps the current $mail settings and creates new object
$mail2 = clone $mail;

// mail to admin
$mail->AddAddress($address, "I Concept");
$mail->AltBody    = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->Body = "<strong>Request a Quote from I Concept Malaysia Website</strong><br><br> 

    Name : $name<br>
    Email : $email<br> 
    Phone : $phone<br> 
    Company : $company<br> 
    Enquiry : $message<br> <br> 

    Thank You!<br>";

if(!$mail->Send()) {
    echo "Mailer Error: " . $mail->ErrorInfo;
} else {
    echo "Message sent!<br>";
}

// now send to user.
$mail2->AddAddress($email, $name);
$mail2->AltBody    = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail2->Body = "Separate email body for user filling form out.";

if(!$mail2->Send()) {
    echo "Mailer Error: " . $mail2->ErrorInfo;
} else {
    echo "Message sent!<br>";
}
查看更多
登录 后发表回答