1

I am working on ticketing system in PHP. I convert mails to tickets. When a user replies to the mail from the Ticketing system, it is sending the mail to the customer as a new mail. No message/mail threading.

I think, my problem is related to the added ticket id at the end of the subject. (e.g. Subject: Installation Problem [#EMSY45])

I have passed the Message ID and References in the Header

I am using PHPMailer to send the mail.

Here is my code:

$mail = new PHPMailer();
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = str_replace('/', '', $host); // Specify main and backup SMTP servers
if($outgoing_server_details['smtp_auth'] == 1)
        $mail->SMTPAuth = true; // Enable SMTP authentication

$mail->Username = $outgoing_server_details['server_username']; // SMTP username
$mail->Password = $outgoing_server_details['server_password']; // SMTP password
$mail->SMTPSecure = $protocol; // Enable TLS encryption, `ssl` also accepted
$mail->Port = $port;

$mail->setFrom($outgoing_server_details['from_email_field'], $from_name);
$mail->addAddress($data['_from'], $to_name); // Add a recipient
$mail->addReplyTo($outgoing_server_details['from_email_field'], $reply_to_name);
$message_id = $data['message_id'];
$mail->AddCustomHeader('In-Reply-To', $message_id);
$mail->AddCustomHeader('References', $message_id);

//$mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
//$mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name
$mail->isHTML(true);                                  // Set email format to HTML

$mail->Subject = $data['subject'];
$mail->Body    = $mail_content;
$mail->send();
trincot
  • 317,000
  • 35
  • 244
  • 286
Rajkumar M
  • 11
  • 1
  • You're doing the right thing by setting those headers, but you really need to inspect the headers in the received messages - are you sure that the message ID is being populated correctly, and matches a previous message ID exactly? – Synchro Jul 12 '16 at 13:26
  • @Synchro I am sure, I am adding the message id correctly. When I fetch the mail using IMAP. I get the message id as well. I am adding the same message id for References header too. – Rajkumar M Jul 12 '16 at 18:05
  • Some clients are better than others at paying attention to these headers - some are a bit useless and rely on heuristics matching subject lines instead- or even both at once. Are you seeing this behaviour in all client apps? – Synchro Jul 12 '16 at 19:04
  • @Synchro I am using Mac Default mail client now. Some times, it showing the threading correctly there. But in Gmail, I am not seeing that threading at first. When subject updated, it started its own threading. Is this mandatory to send the previous reply with the reply ? – Rajkumar M Jul 13 '16 at 18:05
  • I don't know for sure, but gmail is generally pretty badly behaved as a mail client. It also gets reply-to handling completely wrong, in addition to its well-known HTML-wrecking abilities. To do threading the right way, I recommend reading [this](http://cr.yp.to/immhf/thread.html), and [this](https://www.jwz.org/doc/threading.html). More on gmail's broken approach is in [this question](http://stackoverflow.com/questions/23007197/how-to-cause-sent-emails-to-appear-threaded-in-gmail-recipients-view-with-messa). – Synchro Jul 13 '16 at 22:52

1 Answers1

-1

If you need to add ticket ID in the end Subject of the E-Mail then try this.

$mail->Subject = "Installation Problem [".$message_id."]";

OR

$mail->Subject = $data['subject'].$message_id;

And one more Correction

$mail->isHTML(true);//should always come after the Body is set.

i.e.,

$mail->Subject = $data['subject'].$message_id;
$mail->Body    = $mail_content;
$mail->isHTML(true);
$mail->send();
Avinash
  • 812
  • 1
  • 8
  • 22
  • This is a cosmetic fix, not the solution. It doesn't matter when you call `isHTML` - read the code. – Synchro Jul 12 '16 at 13:21
  • @avinash I can able to add Ticket Id at the end of the subject. My problem is Mail threading. When I add Ticket Id at the end, Mail threading is not working. – Rajkumar M Jul 12 '16 at 18:08