1

I am using PHPMailer for sending and replying mail. Here i am facing issue is the reply thread mail is not linked with existing group. It will send separate mail. How can i send it with existing group. I am having thread id. Here is my code.

require_once 'plugins/PHPMailer/src/PHPMailer.php';
require_once 'plugins/PHPMailer/src/Exception.php';
require_once 'plugins/PHPMailer/src/SMTP.php';

$mail = new PHPMailer(true);
$mail->Username   = GMAIL_MAIL;
$mail->Password   = GMAIL_PWD;                               // SMTP password
$mail->SMTPSecure = 'tls';                                  
$mail->Port       = 587;                                        $mail->setFrom(MAIL_FROM, '');
$mail->addAddress($request['mail_to'], $request['mail_to']);     
$mail->addReplyTo(MAIL_FROM, MAIL_FROM);
$mail->addCustomHeader( 'In-Reply-To', '<' . MAIL_FROM . '>' );
$mail->addCustomHeader( 'X-GM-THRID', '' . $request['thread_id']. '' );
$mail->addCustomHeader('References', '[' . $request['thread_id'] . ']');
selvan
  • 1,183
  • 3
  • 16
  • 24
  • Check if the info from https://stackoverflow.com/a/32591614/10283047 helps. According to that you need to keep the original subject line (prefixed by `Re: `), if you just want to answer to the thread. If you want to change the subject, it appears you might need to specify Message-ID and/or References as well. – misorude Jul 23 '19 at 11:46
  • `References` is the canonical source for such info. [This is the definitive guide](https://www.karmak.org/archive/2002/08/message-threading.html) for standard-based threading, even though it's very old! Unfortunately there has been a long history of systems doing it wrong, and this has led to clients like gmail trying to reverse-engineer threading via ambiguous values, such as subject lines - you may catch it doing things like joining unrelated emails together just because they have the same subject line, though this should only be used as a last resort. – Synchro Jul 23 '19 at 12:21
  • BTW in `addAddress` and `addReplyTo`, you don't need to provide the name param if you don't have it; it will act the right way if you just drop the second parameter. – Synchro Jul 23 '19 at 12:22

1 Answers1

0

Setting Message-ID is important for mail thread via phpmailer, you will get the thread id via

$this->email->_get_message_id();

append this code to

$this->email->set_header('Message-ID', $this->email->_get_message_id());

and your total code will be like

$this->email->set_header('References', "CABOvPkdN1ed8NTkph0Ep+ogNaAgW_9R0dR4ZPLTpBn=vc7K7QA@mail.gmail.com");
$this->email->set_header('In-Reply-To', "CABOvPkdN1ed8NTkph0Ep+ogNaAgW_9R0dR4ZPLTpBn=vc7K7QA@mail.gmail.com");
$this->email->set_header('Message-ID', $this->email->_get_message_id());

please get in touch if it is not work :)

Justin J
  • 808
  • 8
  • 14