PHP Mailer-用户电子邮件发送给管理员


PHP Mailer - User email getting sent to admin

我正试图使用php-mailer向用户和管理员发送电子邮件。但当我发送邮件时,用户确认邮件也会发送给管理员。收到的电子邮件格式正确。这里的问题是什么?我的代码在这里:

$name = "User Name";
$email = "user@gmail.com";
$mobile = 'User Phone No.';
$body = "<div><div>Name: <b>$name</b></div>  <div>Email:  <b>$email</b></div>  <div>Mobile: <b>$mobile</b></div></div>";
//SMTP needs accurate times, and the PHP time zone MUST be set
//This should be done in your php.ini, but this is how to do it if you don't have access to that
date_default_timezone_set('Etc/UTC');
require 'PHPMailer/PHPMailerAutoload.php';
//Create a new PHPMailer instance
$mail = new PHPMailer();
//Tell PHPMailer to use SMTP
$mail -> isSMTP();

function sendEmail($mail, $from, $password, $to, $body, $subject) {
    //Enable SMTP debugging
    // 0 = off (for production use)
    // 1 = client messages
    // 2 = client and server messages
    $mail -> SMTPDebug = 2;
    //Ask for HTML-friendly debug output
    $mail -> Debugoutput = 'html';
    //Set the hostname of the mail server
    $mail -> Host = 'smtp.gmail.com';
    //Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
    $mail -> Port = 587;
    //Set the encryption system to use - ssl (deprecated) or tls
    $mail -> SMTPSecure = 'tls';
    //Whether to use SMTP authentication
    $mail -> SMTPAuth = true;
    //Username to use for SMTP authentication - use full email address for gmail
    $mail -> Username = $from;
    //Password to use for SMTP authentication
    $mail -> Password = $password;
    //Set who the message is to be sent from
    $mail -> setFrom($from, $from);
    //Set who the message is to be sent to
    $mail -> addAddress($to, $to);
    //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($body);
    //Replace the plain text body with one created manually
    $mail -> AltBody = 'This is a plain-text message body';
    $sucess = $mail -> send();
    // //send the message, check for errors
    // if (!$sucess) {
    // echo "Mailer Error: " . $mail -> ErrorInfo;
    // } else {
    // echo "Message sent!";
    // }
}
$from = "noreplyadmin@gmail.com";
$maiAdmin = "admin@gmail.com";
$password = "password";
$subject = "Enquiry";
// Send Mail to admin
sendEmail($mail, $from, $password, $maiAdmin, $body, $subject);

// Send Mail to User
$userMessage = "We Received your request. Our representative will contact you soon.";
    sendEmail($mail, $from, $password, $email, $userMessage, 'Acknowlwdgement');

您使用的是同一个实例,并且尚未从PHPMailer $mail实例中清除前一个实例。您可以从PHPMailer实例中清除旧数据,也可以这样做:

$adminmail = new PHPMailer();
$adminmail -> isSMTP();
$usermail = new PHPMailer();
$usermail -> isSMTP();
sendEmail($adminmail, $from, $password, $maiAdmin, $body, $subject);
$userMessage = "We Received your request. Our representative will contact you soon.";
sendEmail($usermail, $from, $password, $email, $userMessage, 'Acknowlwdgement');

我想是什么让你的船漂浮起来。

相关文章: