PHPMailer& Gmail对话视图

时间:2014-03-25 15:55:51

标签: php email phpmailer

我正在使用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 |
+-----------------------------------------------------------------------------+

我错过了一些简单的东西吗?

3 个答案:

答案 0 :(得分:8)

对于任何寻找有用之处的人:按照@ErikNedwidek留下的链接将引导您访问此博文:http://www.sensefulsolutions.com/2010/08/how-does-email-threading-work-in-gmail.html

指定了两个规则:

  • 主题必须类似。
  • 发件人必须是主题的一部分或必须使用in-reply-。

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

还有这一部分:

  

有一点需要注意的是,如果您从Gmail发送电子邮件,那么它们也会被线程化。除了一个小细节之外,规则与收到它们时完全相同。如果您发送相同的确切消息两次没有主题前缀(例如,主题是测试而不是re:test)它会在接收端进行线程化,但不会在发送端进行线程化。相反,如果它确实包含前缀(例如re:test),则在两种情况下都会进行线程化。

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

我尝试在主题的开头添加're:',但这没有任何区别。然而,有效的是使用以下方法添加'In-Reply-To'标题:

$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.com re:预先主题为主题=无线索
  • foo@domain.com发送至foo@domain.com,标头In-Reply-To设置为foo@domain.com =无线程
  • foo@domain.com发送至foo@domain.com,标头In-Reply-To设置为<foo@domain.com> = threading

完整的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登录后面的任何地址。

答案 1 :(得分:1)

在这里查看接受的答案:

https://webapps.stackexchange.com/questions/965/how-does-gmail-decide-to-thread-email-messages

作者在博客文章中也有更多信息。它已经3岁了,但希望信息仍然存在。

答案 2 :(得分:0)

嗯,这与PHP没有任何关系。这与Gmail在“对话”中将电子邮件嵌套在一起的方式有关

您可能想查看https://webapps.stackexchange.com/questions/16651/gmail-not-grouping-messages-with-the-same-subject-from-google-groups

等问题
相关问题