PHP Mailer - User email getting sent to admin

点点圈 提交于 2019-12-13 09:28:53

问题


I am trying to send an email to user and admin using php-mailer.But when I send the mail, the user Acknowledgement mail is also getting sent to the admin. The e mails getting sent are in proper format. What is the issue here? My Code is Here:

$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:


You are using the same instance and have not cleared the previous out of the PHPMailer $mail instance. You can either clear the old data out of the PHPMailer Instance or do this:

$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');

Whatever floats your boat I suppose.



来源:https://stackoverflow.com/questions/34428438/php-mailer-user-email-getting-sent-to-admin

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!