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

时间:2015-12-23 04:00:13

标签: php phpmailer

我正在尝试使用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');

1 个答案:

答案 0 :(得分:0)

您正在使用相同的实例,并且尚未清除以前的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');

无论你认为是什么漂浮在你的船上。