Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Usage of custom headers #88

Closed
rmarescu opened this issue Dec 27, 2024 · 5 comments · Fixed by #89
Closed

Usage of custom headers #88

rmarescu opened this issue Dec 27, 2024 · 5 comments · Fixed by #89

Comments

@rmarescu
Copy link

Hey,

I'm working on a migration from SendGrid to Resend, and noticed that the usage of custom headers seems different from the Rails API.

Here is the sample code suggested:

    mail(
      to: ["[email protected]"],
      reply_to: "[email protected]",
      subject: "Hello from Rails",
      tags: {
        "name": "category", "value": "confirm_email"
      },
      headers: {
        "X-Entity-Ref-ID": "123"
      },
    )

When used as-is, it works well with Resend: the custom header X-Entity-Ref-ID shows up on the Resend UI, and also as a custom header within the recipient email.

However, using this setup with another provider (i.e. SendGrid), it yields different results. From Rails' perspective, headers key above is actually a custom header's field name. So this code would generate 2 custom headers with field names tags (expected) and headers.

Rails' recommended way of passing headers is through the headers method. That works as expected with SendGrid, but not with Resend.

Is this intended behaviour? As we migrate over from SendGrid, we need to support both email providers in parallel, and set custom headers for both.

Semi-related - I've noticed that, when using Rails with SMTP, none of the custom headers are present in the recipient's email.


Here is some sample code to reproduce the issue (redacted PII):

class UserMailer < ApplicationMailer
  # this domain must be verified with Resend
  default from: "[email protected]"

  before_action :set_custom_header

  def welcome_email(user)
    @user = user
    attachments["invoice.pdf"] = File.read(Rails.root.join("resources","invoice.pdf"))
    @url  = "http://example.com/login"
    mail(
      to: ["REDACTED"],
      subject: "Hello from Rails",
      tags: {
        "name": "category", "value": "confirm_email"
      },
      headers: {
        "X-Entity-Ref-ID": "123"
      },
    )
  end

  def set_custom_header
    headers["X-Custom-Header"] = "Custom Value"
  end
end

Partial raw email when sent via Resend (X-Custom-Header is not present):

Delivered-To: REDACTED
Received: by 2002:a05:7110:918d:b0:256:8b1c:e3e4 with SMTP id r13csp5192151gef;
        Thu, 26 Dec 2024 22:02:57 -0800 (PST)
Received: from a9-6.smtp-out.amazonses.com (a9-6.smtp-out.amazonses.com. [54.240.9.6])
        by mx.google.com with ESMTPS id af79cd13be357-7b9ac566827si1930238785a.699.2024.12.26.22.02.56
        for <REDACTED>
        (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128);
        Thu, 26 Dec 2024 22:02:56 -0800 (PST)
X-Entity-Ref-ID: 123
From: REDACTED
To: REDACTED
Subject: Hello from Rails
Message-ID: <0100019406b5f758-94a786b9-3c5d-4880-bc08-297e3df49c0a-000000@email.amazonses.com>
Date: Fri, 27 Dec 2024 06:02:56 +0000
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="--_NmP-f2fa0fa64da7a747-Part_1"
Feedback-ID: ::1.us-east-1.epbACCJLarUaEI78XZoGREJ9LamRjxMH5pM2kZb64rA=:AmazonSES
X-SES-Outgoing: 2024.12.27-54.240.9.6

----_NmP-f2fa0fa64da7a747-Part_1
Content-Type: text/html; charset=utf-8
Content-Transfer-Encoding: 7bit

Partial raw email when sent via SendGrid (X-Custom-Header is present as a header, alongside headers and tags)

Delivered-To: REDACTED
Received: by 2002:a05:7110:918d:b0:256:8b1c:e3e4 with SMTP id r13csp5193437gef;
        Thu, 26 Dec 2024 22:07:04 -0800 (PST)
Received: from o1.email.gumroad.com (o1.email.gumroad.com. [50.31.61.243])
        by mx.google.com with ESMTPS id af79cd13be357-7b9ac2ab6fesi1919907985a.10.2024.12.26.22.07.03
        for <REDACTED>
        (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128);
        Thu, 26 Dec 2024 22:07:04 -0800 (PST)
Date: Fri, 27 Dec 2024 06:07:02 +0000 (UTC)
From: REDACTED
Message-ID: <[email protected]>
Subject: Hello from Rails
Mime-Version: 1.0
Content-Type: multipart/mixed; boundary="--==_mimepart_676e43f112617_b7dcadc-58e"; charset=UTF-8
Content-Transfer-Encoding: 7bit
X-Custom-Header: Custom Value
tags: {:name=>"category", :value=>"confirm_email"}
headers: {:"X-Entity-Ref-ID"=>"123"}
To: REDACTED
X-Entity-ID: u001.G2CkTSMLqY3VOFZf2IjG8A==

----==_mimepart_676e43f112617_b7dcadc-58e
Content-Type: text/html; charset=us-ascii
Content-Transfer-Encoding: 7bit
@drish
Copy link
Collaborator

drish commented Dec 27, 2024

@rmarescu great catch, thanks a lot for opening this issue, I will get a fix in to support the Rails API headers method this weekend.

@briankerr
Copy link

We're also working on adding in the custom headers support when using vanilla Rails with SMTP.

@bukinoshita
Copy link
Member

@rmarescu custom headers support on SMTP was just released.

@drish drish closed this as completed in #89 Dec 31, 2024
@drish
Copy link
Collaborator

drish commented Dec 31, 2024

@rmarescu i just released version 0.17.0 that should include support for the Rails API headers method.

@drish
Copy link
Collaborator

drish commented Dec 31, 2024

also updated the resend-rails-example with custom headers usage example

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants