使用 PHPmailer 回复电子邮件
Replying to an email with PHPmailer
我正在使用 PHPmailer,我想回复电子邮件。我使用 phpimap 获取电子邮件及其 message_id
。我正在使用 PHPmailer 尝试回复电子邮件。我在 addCustomHeader
中使用了 message_id
和 In-Reply-To
。我 运行 代码,当我查看电子邮件时,它显示为新消息而不是回复。我哪里错了?
require 'PHPMailer-master/PHPMailerAutoload.php';
$mail = new PHPMailer;
//$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'mail.domain.co.uk'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'test@domain.co.uk'; // SMTP username
$mail->Password = 'testing'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
$mail->FromName = 'Mailer';
$mail->addAddress('test_2@domain.co.uk'); // Add a recipient
$mail->isHTML(true); // Set email format to HTML
$mail->addCustomHeader('In-Reply-To', $message_id);
$mail->Sender = 'test@domain.co.uk';
$mail->Subject = 'testing';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
让线程中的每条消息都使用不同的主题行是非常合理的,因此线程仅依赖于主题行作为 last-resort 后备,如果您做错了其他所有事情。当客户这样做时,这实际上很烦人,因为您最终会收到不相关的消息,这些消息恰好将相同的主题组合在一起。
线程和回复是使用 References
和 In-Reply-To
headers as defined in RFC2822. Read this guide 实现的,以详细说明如何可靠地执行线程。
简版是这样的,第一次回复留言:
$mail->addCustomHeader('In-Reply-To', $message_id);
$mail->addCustomHeader('References', $message_id);
如果原始消息只是长线程中的最新消息,它会变得更加复杂,但它使用相同的 headers - 阅读规范和指南以获取更多信息。
确保您的消息 ID 格式正确 - 它应该被 <>
包围,例如 <d7751ea969c01cda464ebf2de2fe64e6@example.org>
.
您不需要对主题行做任何事情 - 虽然在前面加上 Re:
是很常见的,但这并不是链接工作所必需的,而且它也因语言而异,所以这不是您想要的可以靠。
我正在使用 PHPmailer,我想回复电子邮件。我使用 phpimap 获取电子邮件及其 message_id
。我正在使用 PHPmailer 尝试回复电子邮件。我在 addCustomHeader
中使用了 message_id
和 In-Reply-To
。我 运行 代码,当我查看电子邮件时,它显示为新消息而不是回复。我哪里错了?
require 'PHPMailer-master/PHPMailerAutoload.php';
$mail = new PHPMailer;
//$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'mail.domain.co.uk'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'test@domain.co.uk'; // SMTP username
$mail->Password = 'testing'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
$mail->FromName = 'Mailer';
$mail->addAddress('test_2@domain.co.uk'); // Add a recipient
$mail->isHTML(true); // Set email format to HTML
$mail->addCustomHeader('In-Reply-To', $message_id);
$mail->Sender = 'test@domain.co.uk';
$mail->Subject = 'testing';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
让线程中的每条消息都使用不同的主题行是非常合理的,因此线程仅依赖于主题行作为 last-resort 后备,如果您做错了其他所有事情。当客户这样做时,这实际上很烦人,因为您最终会收到不相关的消息,这些消息恰好将相同的主题组合在一起。
线程和回复是使用 References
和 In-Reply-To
headers as defined in RFC2822. Read this guide 实现的,以详细说明如何可靠地执行线程。
简版是这样的,第一次回复留言:
$mail->addCustomHeader('In-Reply-To', $message_id);
$mail->addCustomHeader('References', $message_id);
如果原始消息只是长线程中的最新消息,它会变得更加复杂,但它使用相同的 headers - 阅读规范和指南以获取更多信息。
确保您的消息 ID 格式正确 - 它应该被 <>
包围,例如 <d7751ea969c01cda464ebf2de2fe64e6@example.org>
.
您不需要对主题行做任何事情 - 虽然在前面加上 Re:
是很常见的,但这并不是链接工作所必需的,而且它也因语言而异,所以这不是您想要的可以靠。