Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/email-headers-tags.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"PostHog": minor
---

`SendCodeEmailOptions` takes `Headers` and `Tags`. `Headers` puts email headers, such as `X-Entity-Ref-ID`, on the email itself. `Tags` adds names and values to the email's events as `$email_tags`, so you can break down sent, delivered, opened, clicked and bounced emails by them. The client sends both as given and leaves an empty dictionary out of the request. PostHog checks the names, values and limits, and refuses a bad one with `invalid_header` or `invalid_tag`. A send from PostHog's test sender fails with `sandbox_fields_unsupported`, and a project that can't take them yet gets `field_not_accepted`.
10 changes: 10 additions & 0 deletions src/PostHog/Emails/EmailSender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -909,6 +909,16 @@ static PreparedSend PrepareSend(SendEmailOptions email, int defaultMaxRetries)
body["bcc"] = email.Bcc;
}

if (email.Headers is { Count: > 0 })
{
body["headers"] = email.Headers;
}

if (email.Tags is { Count: > 0 })
{
body["tags"] = email.Tags;
}

if (hashed.Files.Count > 0)
{
body["attachments"] = hashed.Files.Select(ReferenceBody).ToList();
Expand Down
43 changes: 43 additions & 0 deletions src/PostHog/Emails/SendEmailOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,49 @@ public sealed class SendCodeEmailOptions : SendEmailOptions
/// </remarks>
public IReadOnlyList<EmailAttachment>? Attachments { get; init; }

/// <summary>
/// Headers PostHog puts on the email itself, not on the HTTP request, such as
/// <c>new Dictionary&lt;string, string&gt; { ["X-Entity-Ref-ID"] = "order-1234" }</c>. Added in 2.32.0.
/// </summary>
/// <remarks>
/// At most 8 headers, each named once, ignoring case. PostHog takes <c>In-Reply-To</c>, <c>References</c>,
/// <c>List-Unsubscribe</c>, <c>List-Unsubscribe-Post</c>, <c>List-Id</c> and any <c>X-</c> header named with at
/// most 77 letters, digits and hyphens, except <c>X-PostHog-</c> and <c>X-SES-</c> headers. A value is 1 to 870
/// ASCII characters without line breaks or other control characters; only an <c>X-</c> header takes non-ASCII
/// text, at most 200 UTF-8 bytes. A header PostHog sets itself, such as <c>List-Unsubscribe</c> on a marketing
/// email, keeps PostHog's value.
/// <para>
/// The client sends the headers as given and leaves an empty dictionary out of the request. PostHog checks the
/// names and values, and refuses the send with <c>invalid_header</c> when one breaks a rule. A send from PostHog's
/// test sender fails with <c>sandbox_fields_unsupported</c>, and a project that can't take headers yet gets
/// <c>field_not_accepted</c>. A template made in PostHog can't take headers, so
/// <see cref="SendTemplateEmailOptions"/> doesn't have this property.
/// </para>
/// </remarks>
public IReadOnlyDictionary<string, string>? Headers { get; init; }

/// <summary>
/// Tags PostHog adds to the email's events, such as
/// <c>new Dictionary&lt;string, string&gt; { ["plan"] = "pro" }</c>. Added in 2.32.0.
/// </summary>
/// <remarks>
/// PostHog adds the tags as <c>$email_tags</c> to the <c>$workflows_email_sent</c>, delivered, opened, clicked
/// and bounced events of the email, so you can break them down by a property such as
/// <c>properties.$email_tags.plan</c>.
/// <para>
/// At most 10 tags. A name is 1 to 64 letters, digits, underscores (<c>_</c>) and hyphens (<c>-</c>), and starts
/// with a letter or digit. A value is 1 to 256 of the same characters.
/// </para>
/// <para>
/// The client sends the tags as given and leaves an empty dictionary out of the request. PostHog checks the names
/// and values, and refuses the send with <c>invalid_tag</c> when one breaks a rule. A send from PostHog's test
/// sender fails with <c>sandbox_fields_unsupported</c>, and a project that can't take tags yet gets
/// <c>field_not_accepted</c>. A template made in PostHog can't take tags, so
/// <see cref="SendTemplateEmailOptions"/> doesn't have this property.
/// </para>
/// </remarks>
public IReadOnlyDictionary<string, string>? Tags { get; init; }

/// <summary>
/// Whether the email is sensitive: set <c>true</c> when it carries login links, reset tokens or codes. A sensitive
/// email keeps its links unchanged unless <see cref="Tracking"/> turns clicks on, leaves its subject off
Expand Down
4 changes: 4 additions & 0 deletions src/PostHog/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ PostHog.Emails.SendCodeEmailOptions.Category.get -> string?
PostHog.Emails.SendCodeEmailOptions.Category.init -> void
PostHog.Emails.SendCodeEmailOptions.From.get -> string?
PostHog.Emails.SendCodeEmailOptions.From.init -> void
PostHog.Emails.SendCodeEmailOptions.Headers.get -> System.Collections.Generic.IReadOnlyDictionary<string!, string!>?
PostHog.Emails.SendCodeEmailOptions.Headers.init -> void
PostHog.Emails.SendCodeEmailOptions.Html.get -> string?
PostHog.Emails.SendCodeEmailOptions.Html.init -> void
PostHog.Emails.SendCodeEmailOptions.ReplyTo.get -> System.Collections.Generic.IReadOnlyList<string!>?
Expand All @@ -89,6 +91,8 @@ PostHog.Emails.SendCodeEmailOptions.Sensitive.get -> bool?
PostHog.Emails.SendCodeEmailOptions.Sensitive.init -> void
PostHog.Emails.SendCodeEmailOptions.Subject.get -> string!
PostHog.Emails.SendCodeEmailOptions.Subject.init -> void
PostHog.Emails.SendCodeEmailOptions.Tags.get -> System.Collections.Generic.IReadOnlyDictionary<string!, string!>?
PostHog.Emails.SendCodeEmailOptions.Tags.init -> void
PostHog.Emails.SendCodeEmailOptions.TemplateKey.get -> string!
PostHog.Emails.SendCodeEmailOptions.TemplateKey.init -> void
PostHog.Emails.SendCodeEmailOptions.Text.get -> string?
Expand Down
51 changes: 51 additions & 0 deletions tests/UnitTests/Emails/EmailsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,57 @@ await harness.Client.Emails.SendAsync(new SendCodeEmailOptions
Assert.Single(harness.Requests).Body);
}

[Fact]
public async Task SendsHeadersAndTagsAsGiven()
{
await using var harness = new EmailHarness();
harness.Respond(HttpStatusCode.Accepted, """{"message_id": "01a18356-c56c-0001-e494-7666bc915188"}""");

await harness.Client.Emails.SendAsync(new SendCodeEmailOptions
{
To = "user@example.com",
TemplateKey = "order-shipped",
Subject = "Your order shipped",
Text = "On its way!",
Headers = new Dictionary<string, string> { ["X-Entity-Ref-ID"] = "order-1234" },
Tags = new Dictionary<string, string> { ["plan"] = "pro", ["flow"] = "order-shipped" },
});

Assert.Equal(
"""{"template_key":"order-shipped","to":"user@example.com","subject":"Your order shipped","text":"On its way!","headers":{"X-Entity-Ref-ID":"order-1234"},"tags":{"plan":"pro","flow":"order-shipped"}}""",
Assert.Single(harness.Requests).Body);
}

[Fact]
public async Task LeavesEmptyHeadersAndTagsOut()
{
await using var harness = new EmailHarness();
harness.Respond(HttpStatusCode.Accepted, """{"message_id": "01a18356-c56c-0001-e494-7666bc915188"}""");

await harness.Client.Emails.SendAsync(new SendCodeEmailOptions
{
To = "user@example.com",
TemplateKey = "receipt_2",
Subject = "Your receipt",
Text = "Thanks!",
Headers = new Dictionary<string, string>(),
Tags = new Dictionary<string, string>(),
});

Assert.Equal(
"""{"template_key":"receipt_2","to":"user@example.com","subject":"Your receipt","text":"Thanks!"}""",
Assert.Single(harness.Requests).Body);
}

[Theory]
[InlineData("Headers")]
[InlineData("Tags")]
public void ATemplateSendCannotCarryCodeOnlyFields(string field)
{
Assert.NotNull(typeof(SendCodeEmailOptions).GetProperty(field));
Assert.Null(typeof(SendTemplateEmailOptions).GetProperty(field));
}

public static TheoryData<string, SendCodeEmailOptions> InvalidCodeSends => new()
{
{ "Cc", new SendCodeEmailOptions { To = "a@example.com", TemplateKey = "k", Subject = "s", Html = "h", Cc = [" "] } },
Expand Down
Loading