PHPMailer:是否可以回复用户提供的电子邮件地址?

时间:2019-07-27 00:17:45

标签: php phpmailer reply

我开始在我的网站上使用PHPMailer来设置联系表单(用户->我自己的电子邮件帐户)。电子邮件发送过程看起来一切正常,但是当我想回复用户的电子邮件时遇到了问题。

从我的网站发送电子邮件的电子邮件地址似乎是我的,通过SMTP配置-> 我同意

我为 setFrom addReplyTo 方法定义了用户的电子邮件地址作为参数,以便能够从Gmail答复其电子邮件,并发送了我的答案到提供的电子邮件地址。

但是,当我在Gmail上单击“答复”时,两个用户都收到了我的答案

我想我真的错过了一些事情,并且/或者误解了上面这些方法的目的。

我曾尝试将{strong> addReplyTo 放在 setFrom 上方,如there所述,但看起来它并没有任何改变。

我对其他无能为力感到困惑,我只使用PHP 4个月了,仍然缺少一些东西,尤其是关于此问题。

//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();   
    }
}

这也是我的Gmail帐户中有关邮件信息的屏幕截图:

mail informations

我想通过我的Gmail帐户回复用户的电子邮件,并希望他成为接收我回复的唯一人。任何建议都欢迎!

编辑:

今天尝试了许多之后,当我单击Gmail上的“答复”按钮时,我似乎是唯一收到我的答复的人。用户什么都没有收到...

3 个答案:

答案 0 :(得分:1)

感谢this post,我能够解决我的问题。

如果“发件人”地址与“收件人”地址 相同,或在GMail设置中配置为“发送为”之一...”帐户, Gmail会回复“收件人”地址,而不是“回复”地址。一个简单的解决方法是指定非Gmail“发件人”地址

=>这完全是我所做的。将电子邮件从我的Gmail帐户发送到我的Gmail帐户是问题所在,所以我对此进行了更改:

//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')

对此:

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

然后,我使用Gmail以外的其他SMTP服务器提供商,并将 my_address_email@NOT_GMAIL.com 重定向到 my_address_email@gmail.com

现在一切正常。

答案 1 :(得分:0)

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

答案 2 :(得分:0)

对地址的答复应放置在Set from地址之前。请参见以下示例:

$mail->AddReplyTo('replyto@email.com', 'Reply to name');
$mail->SetFrom('mailbox@email.com', 'Mailbox name');
相关问题