listmonk icon indicating copy to clipboard operation
listmonk copied to clipboard

Dynamic custom e-mail headers

Open jeff-h opened this issue 2 years ago • 3 comments

I've been trying to get the POP3 bounce handling working; my SMTP server at this point is Mail-in-a-box. It seems MIAB is stripping certain headers, because when I inspect the raw source of a bounce notification email, there is no X-Listmonk-Subscriber in it.

I then tried adding that header in the SMTP config (see example below), hardcoding an example subscriber UUID and it all worked perfectly (odd, I know); I sent a test email to that subscriber, it bounced, the bounce message contained the X-Listmonk-Subscriber and Listmonk picked it up and blocklisted the subscriber.

[
    {
        "Return-Path": "[email protected]",
        "X-Listmonk-Subscriber": "5bbca912-efa8-4437-aaad-96a371b6b207"
    }
]

Obviously other than for testing I can't hardcode the subscriber UUID like that; is there any way to have this filled dynamically? I tried variations on "X-Listmonk-Subscriber": subscriber.UUID etc without success.

jeff-h avatar Apr 27 '23 01:04 jeff-h

Headers do not support dynamic parameters. However, the original header being stripped and the same custom header working, that is weird.

They use the exact same mechanism in the same context.

h := textproto.MIMEHeader{}
h.Set(models.EmailHeaderCampaignUUID, msg.Campaign.UUID)
h.Set(models.EmailHeaderSubscriberUUID, msg.Subscriber.UUID)


// Attach any custom headers.
if len(msg.Campaign.Headers) > 0 {
	for _, set := range msg.Campaign.Headers {
		for hdr, val := range set {
			h.Add(hdr, val)
		}
	}
}

knadh avatar Apr 27 '23 08:04 knadh

However, the original header being stripped and the same custom header working, that is weird.

There really must be some difference — the outcome seemed odd initially so I tried it several times with the same outcome every time.

After your reply above I decided I must have been making things up so I just tried it again, and the exact same thing happens:

If I have this in my SMTP custom headers textfield, the X-Listmonk-Subscriber makes it through in my email:

[
    {
        "Return-Path": "[email protected]",
        "X-Listmonk-Subscriber": "testing-testing-123"
    }
]

If I have this:

[
    {
        "Return-Path": "[email protected]"
    }
]

...it doesn't.

Not sure if it helps, but here are the raw messages:

With the header
Return-Path: <>
Delivered-To: [email protected]
Received: from mail.mysmtpserver.com ([127.0.0.1])
	by mail.mysmtpserver.com with LMTP
	id C7WnOldGSmSumyIA0/SFmA
	(envelope-from <>)
	for <[email protected]>; Thu, 27 Apr 2023 19:54:31 +1000
X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on mail.mysmtpserver.com
X-Spam-Level: 
X-Spam-Status: No, score=-0.5 required=5.0 tests=BAYES_00,HTML_IMAGE_ONLY_32,
	HTML_MESSAGE,NO_RELAYS,PYZOR_CHECK,T_SCC_BODY_TEXT_LINE autolearn=no
	autolearn_force=no version=3.4.6
X-Spam-Report: 
	* -1.9 BAYES_00 BODY: Bayes spam probability is 0 to 1%
	*      [score: 0.0000]
	* -0.0 NO_RELAYS Informational: message was not relayed via SMTP
	*  0.0 HTML_MESSAGE BODY: HTML included in message
	*  0.0 HTML_IMAGE_ONLY_32 BODY: HTML: images with 2800-3200 bytes of
	*      words
	*  1.4 PYZOR_CHECK Listed in Pyzor
	*      (https://pyzor.readthedocs.io/en/latest/)
	* -0.0 T_SCC_BODY_TEXT_LINE No description available.
X-Spam-Score: -0.5
Received: by mail.mysmtpserver.com (Postfix)
	id E668322E2E; Thu, 27 Apr 2023 19:54:31 +1000 (AEST)
Date: Thu, 27 Apr 2023 19:54:31 +1000 (AEST)
From: [email protected] (Mail Delivery System)
Subject: Undelivered Mail Returned to Sender
To: [email protected]
Auto-Submitted: auto-replied
MIME-Version: 1.0
Content-Type: multipart/report; report-type=delivery-status;
	boundary="B2C4D201F3.1682589271/mail.mysmtpserver.com"
Content-Transfer-Encoding: 8bit
Message-Id: <[email protected]>

This is a MIME-encapsulated message.

--B2C4D201F3.1682589271/mail.mysmtpserver.com
Content-Description: Notification
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit

This is the mail system at host mail.mysmtpserver.com.

I'm sorry to have to inform you that your message could not
be delivered to one or more recipients. It's attached below.

For further assistance, please send mail to postmaster.

If you do so, please include this problem report. You can
delete your own text from the attached returned message.

                   The mail system

<[email protected]>: Host or domain name not found. Name service error for
    name=ohgiouhiu.asd type=AAAA: Host not found

--B2C4D201F3.1682589271/mail.mysmtpserver.com
Content-Description: Delivery report
Content-Type: message/delivery-status

Reporting-MTA: dns; mail.mysmtpserver.com
X-Postfix-Queue-ID: B2C4D201F3
X-Postfix-Sender: rfc822; [email protected]
Arrival-Date: Thu, 27 Apr 2023 19:54:31 +1000 (AEST)

Final-Recipient: rfc822; [email protected]
Original-Recipient: rfc822;[email protected]
Action: failed
Status: 5.4.4
Diagnostic-Code: X-Postfix; Host or domain name not found. Name service error
    for name=ohgiouhiu.asd type=AAAA: Host not found

--B2C4D201F3.1682589271/mail.mysmtpserver.com
Content-Description: Undelivered Message
Content-Type: message/rfc822
Content-Transfer-Encoding: 8bit

Return-Path: <[email protected]>
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=mysite.app;
	s=mail; t=1682589271;
	bh=nGti2oZG8xMYPo8WpbWVbE9PccAo9s/QHoPJBBZbW2U=;
	h=To:Subject:From:Date:From;
	b=NyhClXeX4cX8wMoYismkPnKBtR7EPSrSKzxTXHOu0300b/l4L7kx5FWfsn+qSeZ3p
	 3owQHHtE2wMe2NvV+XmD2f4wz5ezuCvAEnDB9YX4dYoGDAWkI/YeJ7AZSy8ZyE7/0T
	 WfA3EAIepAaa0zbDX0vbUQTBPVdyomVuyxHzSycchQXEHVv5XfZlryvNv7U4M3Aofe
	 XJ8XuUXPHTZIG0LgA6pQaWjAGL+xIdq0Baj3XiE0VjsP9N4/HgQRBJawLQlxAkMCI1
	 zhKmQvDuLZSTr3NsSsdM7Aeg1/ZYXO1T+Irkc2vqhW5etxh35UyrDtaOSBRCh/K9oE
	 MGEfkboPC9+yQ==
Received: from authenticated-user (mail.mysmtpserver.com [43.229.62.225])
	(using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)
	 key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256)
	(No client certificate requested)
	by mail.mysmtpserver.com (Postfix) with ESMTPSA id B2C4D201F3
	for <[email protected]>; Thu, 27 Apr 2023 19:54:31 +1000 (AEST)
Message-Id: <1682589271735859586.1.7117420467434460462@d7a0baa5a3e0>
Mime-Version: 1.0
X-Listmonk-Subscriber: hello-hello-123
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
To: <[email protected]>
Subject: Confirm subscription
From: "Myname" <[email protected]>
Date: Thu, 27 Apr 2023 09:54:31 +0000



<!doctype html>
<html>
    
        ...subscription confirmation HTML email body here...

</html>


--B2C4D201F3.1682589271/mail.mysmtpserver.com--
without the header
Return-Path: <>
Delivered-To: [email protected]
Received: from mail.mysmtpserver.com ([127.0.0.1])
	by mail.mysmtpserver.com with LMTP
	id 9XHfGUVFSmRtmSIA0/SFmA
	(envelope-from <>)
	for <[email protected]>; Thu, 27 Apr 2023 19:49:57 +1000
X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on mail.mysmtpserver.com
X-Spam-Level: 
X-Spam-Status: No, score=-0.5 required=5.0 tests=BAYES_00,HTML_IMAGE_ONLY_32,
	HTML_MESSAGE,NO_RELAYS,PYZOR_CHECK,T_SCC_BODY_TEXT_LINE autolearn=no
	autolearn_force=no version=3.4.6
X-Spam-Report: 
	* -1.9 BAYES_00 BODY: Bayes spam probability is 0 to 1%
	*      [score: 0.0000]
	* -0.0 NO_RELAYS Informational: message was not relayed via SMTP
	*  0.0 HTML_MESSAGE BODY: HTML included in message
	*  0.0 HTML_IMAGE_ONLY_32 BODY: HTML: images with 2800-3200 bytes of
	*      words
	*  1.4 PYZOR_CHECK Listed in Pyzor
	*      (https://pyzor.readthedocs.io/en/latest/)
	* -0.0 T_SCC_BODY_TEXT_LINE No description available.
X-Spam-Score: -0.5
Received: by mail.mysmtpserver.com (Postfix)
	id 6058723700; Thu, 27 Apr 2023 19:49:57 +1000 (AEST)
Date: Thu, 27 Apr 2023 19:49:57 +1000 (AEST)
From: [email protected] (Mail Delivery System)
Subject: Undelivered Mail Returned to Sender
To: [email protected]
Auto-Submitted: auto-replied
MIME-Version: 1.0
Content-Type: multipart/report; report-type=delivery-status;
	boundary="433DB22E4B.1682588997/mail.mysmtpserver.com"
Content-Transfer-Encoding: 8bit
Message-Id: <[email protected]>

This is a MIME-encapsulated message.

--433DB22E4B.1682588997/mail.mysmtpserver.com
Content-Description: Notification
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit

This is the mail system at host mail.mysmtpserver.com.

I'm sorry to have to inform you that your message could not
be delivered to one or more recipients. It's attached below.

For further assistance, please send mail to postmaster.

If you do so, please include this problem report. You can
delete your own text from the attached returned message.

                   The mail system

<[email protected]>: Host or domain name not found. Name service error for
    name=ohgiouhiu.asd type=AAAA: Host not found

--433DB22E4B.1682588997/mail.mysmtpserver.com
Content-Description: Delivery report
Content-Type: message/delivery-status

Reporting-MTA: dns; mail.mysmtpserver.com
X-Postfix-Queue-ID: 433DB22E4B
X-Postfix-Sender: rfc822; [email protected]
Arrival-Date: Thu, 27 Apr 2023 19:49:57 +1000 (AEST)

Final-Recipient: rfc822; [email protected]
Original-Recipient: rfc822;[email protected]
Action: failed
Status: 5.4.4
Diagnostic-Code: X-Postfix; Host or domain name not found. Name service error
    for name=ohgiouhiu.asd type=AAAA: Host not found

--433DB22E4B.1682588997/mail.mysmtpserver.com
Content-Description: Undelivered Message
Content-Type: message/rfc822
Content-Transfer-Encoding: 8bit

Return-Path: <[email protected]>
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=mysite.app;
	s=mail; t=1682588997;
	bh=nGti2oZG8xMYPo8WpbWVbE9PccAo9s/QHoPJBBZbW2U=;
	h=To:Subject:From:Date:From;
	b=ngPR7HEuhI2YZdaKtxhedaDVhN/8SjUGP/tI/T5Z7+FsEHVsTzsdJC4OMZgvSbQkY
	 AvAhqShVnv8KIYoFOzfHeT46hQwaxHG6fi8Rre3Zpwwx3NCAGXd94B/Bv5GwZd5Wy4
	 7Pjy/qq9CaZ0hP9HYb7HQCYcikvzx8SxvqRCrAL4qqeOe9vI0niMnSw+tH41o9EWeT
	 R49FJHDLDqlXzOOdOXw6pE3CMzd6lE30RU5GtY3KKGuhTa3b/1+ERxkvK1HI45Hrcp
	 kELsK4lF9yZMeVa7HYaC69TnZlj5al5KCBc58PdBiI9oSmvHbJRidJwnL6NGLy+kwG
	 gpuvc6Axz+Svg==
Received: from authenticated-user (mail.mysmtpserver.com [43.229.62.225])
	(using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)
	 key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256)
	(No client certificate requested)
	by mail.mysmtpserver.com (Postfix) with ESMTPSA id 433DB22E4B
	for <[email protected]>; Thu, 27 Apr 2023 19:49:57 +1000 (AEST)
Mime-Version: 1.0
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
To: <[email protected]>
Subject: Confirm subscription
Message-Id: <1682588997280843532.1.2120628143480758168@d7a0baa5a3e0>
From: "Myname" <[email protected]>
Date: Thu, 27 Apr 2023 09:49:57 +0000



<!doctype html>
<html>
    
        ...subscription confirmation HTML email body here...

</html>


--433DB22E4B.1682588997/mail.mysmtpserver.com--

Sorry I don't have better Go :(

jeff-h avatar Apr 27 '23 10:04 jeff-h

h.Set(models.EmailHeaderCampaignUUID, msg.Campaign.UUID)
h.Set(models.EmailHeaderSubscriberUUID, msg.Subscriber.UUID)

Is it interesting that under no scenario have my emails (all are subscription-confirmations) had X-Listmonk-Campaign either? It's not just X-Listmonk-Subscriber that's missing.

Are the headers for subscription confirmation emails perhaps prepared in a different location than here?

jeff-h avatar Apr 27 '23 10:04 jeff-h