0

I started to use PHPMailer on my website in order to set up a contact form (user -> my own email account). Everything looks fine about the email sending process, but I face a problem when I want to answer my user's email.

It seems that the email address that sends the email from my website is mine, configured via SMTP -> I'm fine with that.

I defined for both setFrom and addReplyTo methods user's email address as parameter, in order to be able to answer to his email from Gmail, and that my answer is sent to the email address provided.

However, when I click on "Reply" on Gmail, both user and I receive my answer.

I guess I really missed something about that, and / or misunderstood the purpose of these methods above.

I tried to place addReplyTo above setFrom as mentioned there, but looks like it doesn't change anything.

I'm kinda stuck about what else I can do, I only use PHP for 4 months now, and some things are still missing, especially about this issue.

//class Mail

public function sendMail ()
{
    //Instantiation and passing "true" enables exceptions
    $mail = new PHPMailer(true);

    $msg = utf8_encode(wordwrap($this->message, 70, "\r\n"));

    try {
        //Server settings
        $mail->SMTPDebug = 0;
        $mail->isSMTP();
        $mail->Host = 'smtp.gmail.com';
        $mail->SMTPAuth = true;
        $mail->Username = 'my_email_address@gmail.com';
        $mail->Password = 'password';
        $mail->SMTPSecure = 'ssl';
        $mail->Port = 465;

        //Recipients
        //Jon Doe <my_email_address@gmail.com(?)>
        $mail->setFrom($this->mail, $this->first_name . ' ' . $this->last_name);
        //User's email address
        $mail->addReplyTo($this->mail);
        $mail->addAddress('my_email_address+ALIAS@gmail.com');


        // Content
        $mail->isHTML(true);
        //contact@domain.com : Jon Doe
        $mail->Subject = 'contact@domain.com : ' . $this->first_name . ' ' . $this->last_name;
        $mail->Body = $msg;
        $mail->AltBody = $msg;
        $mail->send();

    } catch (Exception $e) {
        //False need for my contact.php webpage if email cannot be sent 
        if (!empty($e->getMessage())) {
            $_POST['success'] = false;
       }
    }
}



//contact.php

if (!empty($_POST['last_name']) && !empty($_POST['first_name']) && !empty($_POST['mail']) && !empty($_POST['message']) && isset($_POST['g-recaptcha-response'])) {
    $first_name = htmlentities($_POST['first_name']);
    $last_name = htmlentities($_POST['last_name']);
    $mail = htmlentities($_POST['mail']);
    $message = htmlentities($_POST['message']);

    $contact = new Contact($first_name, $last_name, $mail, $message);  

    //Checking if everything's fine about form submission
    if ($contact->isBool()) {
        $mail = new Mail($contact);
        $mail->sendMail();

        //If something's wrong with the mailing process 
        if (isset($_POST['success'])) {
            //Just some text to display to inform the user
            $error_email['email_sent'] = "Foo";
        } else {
            //Just some text to display to inform the user
            $success = "Bar";
        }   
    } else {
        $errors = $contact->getErrors();   
    }
}

Here's also a screen shot from my Gmail account about mail informations:

mail informations

I'd like to reply to user's email from my Gmail account, and wants him to be the only one to receive my reply. Any suggestion is welcome !

EDIT:

After many tries today, it looks like I'm the only one who receive my answer when I click on 'Reply' button on Gmail. User doesn't receive anything...

iDrums
  • 35
  • 8

3 Answers3

2

Thanks to this post, I've been able to fix my issue.

If the "From" address is either the same as the "To" address, or is configured in GMail Settings as one of the 'Send As...' accounts, Gmail replies to the "To" address instead of the "Reply-To" address. An easy workaround is to specify a non-Gmail "From" address

=> It was totally what I did. Send the email from my Gmail account to my Gmail account was the problem, so I changed this:

//Jon Doe <my_email_address@gmail.com(?)>
$mail->setFrom($this->mail, $this->first_name . ' ' . $this->last_name);

//User's email address
$mail->addReplyTo($this->mail);

$mail->addAddress('my_email_address+ALIAS@gmail.com')

to this:

//user's_email_address@something.com
$mail->addReplyTo($this->mail);

$mail->addAddress('my_email_address@NOT_GMAIL.com');

$mail->setFrom('my_email_address@NOT_GMAIL.com');

then I used another SMTP server provider than Gmail, and made redirection for my_address_email@NOT_GMAIL.com to my_address_email@gmail.com.

Now everything works fine.

Community
  • 1
  • 1
iDrums
  • 35
  • 8
0
$mail->ClearReplyTos();
$mail->addReplyTo(yourreplytomailid, 'Name');
Piyush Shukla
  • 244
  • 1
  • 11
0

The reply to Address should be placed before the Set from address.Please see the example below:

$mail->AddReplyTo('replyto@email.com', 'Reply to name');
$mail->SetFrom('mailbox@email.com', 'Mailbox name');
  • As mentioned in my first post, I tried this but it didn't change anything :( – iDrums Jul 27 '19 at 15:58
  • Have you tried this: $mail->AddReplyTo('replyto@email.com', 'Reply to name'); $mail->AddAddress('user@email.com', 'User name); $mail->SetFrom('mailbox@email.com', 'Mailbox name'); – Nidhi Gupta Jul 27 '19 at 16:03
  • Yes I did, but still the same issue apparently :( – iDrums Jul 27 '19 at 16:10