gpt4 book ai didi

PHPMailer 和 Gmail 对话 View

转载 作者:行者123 更新时间:2023-12-02 06:34:37 27 4
gpt4 key购买 nike

当我们的服务器收到更新(通常与付款相关)时,我正在使用 PHPMailer 发送电子邮件以提供支持。

我正在尝试让相关电子邮件显示为 Gmail 对话,以便支持人员更轻松地关注以前的更新/回复。我最初假设它是基于主题的,但这似乎没有什么不同。

我的邮件程序代码:

$mail               = new PHPMailer;                        // create a new instance
$mail->isSMTP(); // set that we're using stmp
$mail->CharSet = 'UTF-8'; // make sure it's utf-8 encoded
$mail->Host = 'smtp.gmail.com'; // the hostname of the mail server
$mail->Port = 587; // set the smtp port number (587 for authenticated TLS)
$mail->SMTPSecure = 'tls'; // set the encryption to use, ssl (deprecated) or tls
$mail->SMTPAuth = true; // should we use smtp authentication?
$mail->Username = MY_EMAIL_LOGIN; // the user name for the smtp authentication
$mail->Password = MY_EMAIL_PASSWORD; // the password for smtp authentication
$mail->wordWrap = 70; // make sure we've no lines longer than 70 chars
$mail->Subject = "[Payment] - Player {$payment->user->name} ({$payment->user->id}) - Payment ID {$payment->id}";
$mail->Body = $htmlBody; // our html body
$mail->AltBody = $plainBody; // our fallback, plain-text body
$mail->setFrom( SUPPORT_EMAIL, 'Support' ); // who this is from
$mail->addReplyTo( SUPPORT_EMAIL, 'Support' ); // who we can reply to
$mail->addAddress( SUPPORT_EMAIL ); // who we're sending it to
$mail->isHTML( true ); // is this a html formatted email?
if( !$mail->send() )
error_log( "Can't send an email to support about payment {$payment->id} for user {$payment->user->id}" );

如果我收到来自同一用户的 2 封电子邮件,涉及同一笔付款(因此是同一主题),我希望它以以下形式出现:

+-----------------------------------------------------------------------------+
| Support (2) | [Payment] Player foo (123456789) - Payment ID 123456789 |
+-----------------------------------------------------------------------------+

它实际上是什么:

+-----------------------------------------------------------------------------+
| Support | [Payment] Player foo (123456789) - Payment ID 123456789 |
+-----------------------------------------------------------------------------+
| Support | [Payment] Player foo (123456789) - Payment ID 123456789 |
+-----------------------------------------------------------------------------+

我是不是漏掉了一些简单的东西?

最佳答案

对于寻找有效方法的任何人:点击@ErikNedwidek 留下的链接将带您到这篇博文:http://www.sensefulsolutions.com/2010/08/how-does-email-threading-work-in-gmail.html

指定了两条规则:

  • 主题必须相似。
  • 发件人必须是线程的一部分,或者必须使用 in-reply-to。

第一个被覆盖,因为主题相同,第二个的第一部分应该被覆盖,因为发送者与接收者相同。

还有这部分:

One interesting thing to note is that if you send email messages from Gmail they will also be threaded. The rules are exactly the same as when you receive them, except for one minor detail. If you send the same exact message twice with no subject prefix (e.g. subject is test not re: test) it does get threaded on the receiving end, but not on sending end. Conversely, if it does contain a prefix (e.g. re: test) it will be threaded in both cases.

我认为它没有被线程化,因为发送者和接收者地址相同。将接收方地址更改为另一个测试地址意味着消息在收到时已正确串接。保持发送方和接收方地址相同,但添加另一个接收方地址也意味着它们得到了正确的线程化。不过,只有一个与发件人地址匹配的收件人地址是行不通的。

我尝试在主题的开头添加一个“re:”,但这没有任何区别。然而,起作用的是使用以下方法添加“In-Reply-To” header :

$mail->addCustomHeader( 'In-Reply-To', '<' . SUPPORT_EMAIL . '>' );

请注意 <>很重要,因为没有它似乎被忽略了。

总结一下:

  • foo@domain.com发送至 foo@domain.com = 无线程
  • foo@domain.com发送至 bar@domain.com =线程
  • foo@domain.com发送至 foo@domain.combar@domain.com = 都线程
  • foo@domain.com发送至 foo@domain.comre:前置到主题=没有线程
  • foo@domain.com发送至 foo@domain.com带标题 In-Reply-To设置为 foo@domain.com = 无线程
  • foo@domain.com发送至 foo@domain.com带标题 In-Reply-To设置为 <foo@domain.com> =线程

完整的 PHPMailer 代码:

$mail               = new PHPMailer;                                // create a new instance
$mail->isSMTP(); // set that we're using stmp
$mail->CharSet = 'UTF-8'; // make sure it's utf-8 encoded
$mail->Host = 'smtp.gmail.com'; // the hostname of the mail server
$mail->Port = 587; // set the smtp port number (587 for authenticated TLS)
$mail->SMTPSecure = 'tls'; // set the encryption to use, ssl (deprecated) or tls
$mail->SMTPAuth = true; // should we use smtp authentication?
$mail->Username = MY_EMAIL_LOGIN; // the user name for the smtp authentication
$mail->Password = MY_EMAIL_PASSWORD; // the password for smtp authentication
$mail->wordWrap = 70; // make sure we've no lines longer than 70 chars
$mail->Subject = "[Payment] Player {$payment->user->name} ({$payment->user->id}) - Payment ID {$payment->id}";
$mail->Body = $htmlBody; // our html body
$mail->AltBody = $plainBody; // our fallback, plain-text body
$mail->setFrom( SUPPORT_EMAIL, 'Support' ); // who this is from
$mail->addReplyTo( SUPPORT_EMAIL, 'Support' ); // who we can reply to
$mail->addAddress( SUPPORT_EMAIL ); // who we're sending it to
$mail->addCustomHeader( 'In-Reply-To', '<' . SUPPORT_EMAIL . '>' ); // so we get threading on gmail (needed as to and from are the same address)
$mail->isHTML( true ); // is this a html formatted email?
if( !$mail->send() )
error_log( "[paymentsRealtimeUpdates] Can't send an email to support about payment {$payment->id} for user {$payment->user->id}" );

不相关的小点 - 无论你为 setFrom 设置什么地址似乎被忽略了 - Gmail 会使用 MY_EMAIL_LOGIN 后面的任何地址登录。

关于PHPMailer 和 Gmail 对话 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22639839/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com