【问题标题】:PHPMailer : Is there a way to reply to the email address provided by the user?PHPMailer : 有没有办法回复用户提供的电子邮件地址?
【发布时间】:2019-07-27 00:17:45
【问题描述】:

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

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

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

但是,当我点击 Gmail 上的“回复”时,我和用户都会收到我的回复

我想我真的错过了一些东西,和/或误解了上述这些方法的目的。

我尝试将 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 帐户关于邮件信息的屏幕截图:

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

编辑:

今天经过多次尝试,当我点击 Gmail 上的“回复”按钮时,似乎只有我一个人收到了我的回复。用户没有收到任何东西...

【问题讨论】:

标签: php phpmailer reply


【解决方案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强>.

现在一切正常。

【讨论】:

    【解决方案2】:
    $mail->ClearReplyTos();
    $mail->addReplyTo(yourreplytomailid, 'Name');
    

    【讨论】:

    • 看起来无法解决问题
    • 回复地址需加在发件人地址前:
    【解决方案3】:

    地址的回复应该放在Set from address之前。请看下面的例子:

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

    【讨论】:

    • 正如我在第一篇文章中提到的,我尝试了这个,但它没有改变任何东西:(
    • 你试过这个:$mail->AddReplyTo('replyto@email.com', 'Reply to name'); $mail->AddAddress('user@email.com', '用户名); $mail->SetFrom('mailbox@email.com', '邮箱名称');
    • 是的,我做到了,但显然还是同样的问题:(
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-10-21
    • 1970-01-01
    • 1970-01-01
    • 2021-05-16
    • 2020-01-12
    • 1970-01-01
    • 2014-03-26
    相关资源
    最近更新 更多