Search code examples
phpemailsmtpgmailphpmailer

PHPMailer - gmail smtp not working properly


I use gmail smtp for contact form in my site.(PHPMailer script https://github.com/PHPMailer/PHPMailer‎)
my code is:

<?php
include "classes/class.phpmailer.php"; // include the class name
$mail = new PHPMailer(); // create a new object
$mail->IsSMTP(); // enable SMTP
$mail->SMTPDebug = 1; // debugging: 1 = errors and messages, 2 = messages only
$mail->SMTPAuth = true; // authentication enabled
$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for GMail
$mail->Host = "smtp.gmail.com";
$mail->Port = 465; // or 587
$mail->IsHTML(true);
$mail->Username = "[email protected]";
$mail->Password = "xxxxxxxxxx";
$mail->SetFrom("[email protected]");
$mail->addReplyTo("[email protected]");
$mail->Subject = "Your Gmail SMTP Mail";
$mail->Body = "Hi, your first SMTP mail via gmail server has been received.";
$mail->AddAddress("[email protected]");
 if(!$mail->Send()){
  echo "Mailer Error: " . $mail->ErrorInfo;
}
else{
    echo "Message has been sent";
}
?>

It works but i have two problems:

  1. I set $mail->SetFrom("[email protected]");
    but in my gmail show from: [email protected]

  2. I set $mail->addReplyTo("[email protected]");
    but in my gmail when i click replay button email replayed to [email protected]
    my code is


Solution

  • I found my answer. in your Gmail go to

    setting ->accounts ->Send mail as
    

    click Add another email address you own in new window enter new email address (example if your gmail is [email protected] you must enter [email protected])or(if your gmail address have dot you must change position of dot. example if your gmail is [email protected] you must enter [email protected])
    don't forget uncheck Treat as an alias.
    click next step.
    enter image description here

    go back to setting ->accounts ->Send mail as
    make a new email as defult
    check Reply from the same address the message was sent to
    all done!
    i change code use new codes.
    enter image description here
    now show from my site

    enter image description here
    now when you click replay botton show replay to user email
    enter image description here

    <?php
    include "classes/class.phpmailer.php"; // include the class name
    $mail = new PHPMailer(); // create a new object
    $mail->IsSMTP(); // enable SMTP
    $mail->SMTPDebug = 1; // debugging: 1 = errors and messages, 2 = messages only
    $mail->SMTPAuth = true; // authentication enabled
    $mail->SMTPSecure = 'tls'; // secure transfer enabled REQUIRED for GMail
    $mail->Host = "smtp.gmail.com";
    $mail->Port = 587; // or 587
    $mail->IsHTML(true);
    $mail->Username = "[email protected]"; 
    $mail->Password = "xxxxxxxxx";
    $mail->addReplyTo("[email protected]","user");
    $mail->SetFrom("[email protected]","My Site");
    $mail->Subject = "Your Gmail SMTP Mail";
    $mail->Body = "Hi, your first SMTP mail via gmail server has been received.";
    $mail->AddAddress("[email protected]");
     if(!$mail->Send()){
      echo "Mailer Error: " . $mail->ErrorInfo;
    }
    else{
        echo "Message has been sent";
    }
    ?>