I have a simple PHP endpoint that takes a POST request and uses PHPMailer to send me an email. The whole thing works fine but I notice that in my email client (Gmail on web), the emails I receive from it seem to be grouped as part of the same thread/conversation.
Is there a param I can set in the email header to inform email clients that each message should be treated as its own separate conversation? In other words, don't chain the emails (as shown in the screenshot).
I assume this behavior can be achieved since you can effectively do this if you were sending separate emails to someone manually (versus replying to an email chain you already sent).
Here is how I send an email (simplified and redacted):
<?php
require '../utils/phpmailer/vendor/autoload.php';
$email = $_POST['email'];
$message = $_POST['message'];
$mailer = new PHPMailer;
$mailer->isSMTP();
$mailer->Host = 'smtp.mailgun.org';
$mailer->SMTPAuth = true;
$mailer->Username = getenv('MAILGUN_SMTP_USERNAME');
$mailer->Password = getenv('MAILGUN_SMTP_PASSWORD');
$mailer->SMTPSecure = 'tls';
$mailer->Port = 587;
$mailer->From = 'noreply@mydomain.com';
$mailer->addAddress('myemail@gmail.com', 'Me');
$mailer->isHTML(true);
$mailer->Subject = 'Message from Endpoint';
$mailer->Body = 'Received a message from '.$email.'. Message: '.$message;
if (!$mailer->send()) {
die('failed');
} else {
echo 'success';
}
?>
Screenshot from my email client showing messages being chained together as the same "conversation":
No, there is not.
There is a whole chunk of email RFCs relating to connecting messages together in threads: the Message-ID
, In-Reply-To
, and References
headers. Unfortunately widespread incompetence and broken implementations have made a bit of a mess of them in practice, and so mail clients (such as gmail) sometimes resort to linking messages together by home-grown heuristics, e.g. if they are from the same sender, or happen have the same subject, even if those messages are completely unrelated in any sensible way.
What's worse, some have taken up ignoring correctly set headers, which doesn't help anyone.
Client-specific heuristics are by definition outside of the email spec, and are thus uncontrollable from the sender's point of view. Gmail is particularly poor at this, and randomly links together all kinds of unrelated messages.
While I'm here, this looks very suspicious:
require '../utils/phpmailer/vendor/autoload.php';
Your vendor
folder should belong to your project, not any library used by it. Using an autoloader generated from PHPMailer's own composer.json
(which is what this line looks like) is likely to include dev dependencies, which should not be present in production.