0
votes

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 = '[email protected]';
        $mail->Password = 'password';
        $mail->SMTPSecure = 'ssl';
        $mail->Port = 465;

        //Recipients
        //Jon Doe <[email protected](?)>
        $mail->setFrom($this->mail, $this->first_name . ' ' . $this->last_name);
        //User's email address
        $mail->addReplyTo($this->mail);
        $mail->addAddress('[email protected]');


        // Content
        $mail->isHTML(true);
        //[email protected] : Jon Doe
        $mail->Subject = '[email protected] : ' . $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...

3
Can you post the raw email headers in a gist/pastebin so we can see what the issue is?atymic
Sure, here it is : pastebin.com/raw/R9EEvchSiDrums

3 Answers

1
votes

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 <[email protected](?)>
$mail->setFrom($this->mail, $this->first_name . ' ' . $this->last_name);

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

$mail->addAddress('[email protected]')

to this:

//user'[email protected]
$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 [email protected].

Now everything works fine.

0
votes
$mail->ClearReplyTos();
$mail->addReplyTo(yourreplytomailid, 'Name');
0
votes

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

$mail->AddReplyTo('[email protected]', 'Reply to name');
$mail->SetFrom('[email protected]', 'Mailbox name');