感谢@Yeeooow 提供有关RFC 2822
标准的信息:The References and In-Reply-To headers must be set in compliance with the RFC 2822 standard.
根据这些信息,我检查了我拥有的其他一些电子邮件线程/对话。所有这些都以相同的方式使用提到的References
和In-Reply-To
标题。有了这些信息,我开始开发以归档相同的结果。
由于我们需要参考旧电子邮件,我们需要一个表格,我们可以在其中存储Message-ID
每封已发送电子邮件的信息。我在我的情况下创建了这个表:
// Table: ticket_message_ids
public function up()
{
Schema::create('ticket_message_ids', function (Blueprint $table) {
$table->increments('id');
$table->integer('ticket_id')->unsigned();
$table->integer('reference_id')->unsigned(); // Optional; You may remove it or make it ->nullable()
$table->string('message_id');
$table->timestamps();
});
}
使用此表,我们能够存储Message-ID
每封已发送电子邮件的信息,并可以参考它属于哪个工单。这也将有助于我们稍后仅获取Message-ID
与此票证相关的关联 s - 否则,我们将在同一个电子邮件线程中混合不同的票证历史记录。
在该reference_id
字段中,您可以选择存储任务的关联 ID:
- 添加了工单文本
- 已执行的工单操作(例如,支持者、优先级、头衔或状态已更改)
在您的可邮寄(例如app\Mail\TicketTextAdded.php
)中,您现在可以将代码部分添加$this->withSwiftMessage() {}
到您的build()
函数中,以捕获Message-ID
这封新电子邮件的当前信息并引用之前的所有其他电子邮件,并存储新的 Message-ID`:
public function build()
{
$email_from_name = "Support - " . config('app.name');
$subject = "[" . $this->ticket->id . "] " . $this->ticket->subject . " - " . config('app.name');
$email = $this->from('support@example.com', $email_from_name)
->subject($subject)
->markdown('emails.customer.ticket.comment_added')
->with([
'nickname' => $this->user->nickname,
'ticket_id' => $this->ticket->id,
'ticket_subject' => $this->ticket->subject,
'ticket_text' => $this->ticket_comments->text,
]);
// Access underlaying Swift message
$this->withSwiftMessage(function ($swiftmessage) {
// Get all Message-IDs associated to this specific ticket
$message_ids = TicketMessageIds::where('ticket_id', '=', $this->ticket->id)->get();
// Build RFC2822 conform 'References' header
// Example: 'References: <4bcf5a806f795b86fb2ba7238c78983c@swift.generated> <ab7898365c4aa91bfe010d4e1e8da377@swift.generated>'
$header_references = "";
foreach($message_ids as $message_id) {
if(empty($header_references)) {
$header_references = $message_id->message_id;
} else {
$header_references = $header_references . " " . $message_id->message_id;
}
}
// Build RFC2822 conform 'In-Reply-To' header
// Example: 'In-Reply-To: <ab7898365c4aa91bfe010d4e1e8da377@swift.generated>'
$header_in_reply_to = TicketMessageIds::where('ticket_id', '=', $this->ticket->id)->orderBy('id', 'DESC')->get(['message_id'])->first()->message_id;
// Add required custom headers with above values
$headers = $swiftmessage->getHeaders();
// 'X-Mailer' header is not required for this purpose
// This header sets only a name for the client, which sent this message (typical values: Outlook 2016, PHPMailer v6.0.5,...)
$headers->addTextHeader('X-Mailer', config('app.name') . ' (' . config('app.url') . ')');
if(!empty($header_references)) {
$headers->addTextHeader('References', $header_references);
}
$headers->addTextHeader('In-Reply-To', $header_in_reply_to);
TicketMessageIds::create([
'ticket_id' => $this->ticket->id,
'message_id' => '<'.$swiftmessage->getId().'>'
]);
});
return $email;
}
仅供参考:您也可以在此处更改Message-ID
我们设置自定义标头的位置,但这需要遵守相关的 RFC 文档:
$msgId = $swiftmessage->getHeaders()->get('Message-ID');
$msgId->setId(time() . '.' . uniqid('thing') . '@example.org');
更多信息:https ://swiftmailer.symfony.com/docs/headers.html#id-headers
希望我可以帮助其他人提供这些信息。:)