Add Sendgrid Category to Digest Emails (#20595)

* add in header for digest emails

* add test
This commit is contained in:
Philip How 2024-02-06 19:02:10 +00:00 committed by GitHub
parent a5687b02e5
commit a900050fdc
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 22 additions and 2 deletions

View file

@ -7,6 +7,14 @@ class DigestMailer < ApplicationMailer
@unsubscribe = generate_unsubscribe_token(@user.id, :email_digest_periodic)
subject = generate_title
# set sendgrid category in the header using smtp api
# https://docs.sendgrid.com/for-developers/sending-email/building-an-x-smtpapi-header
if ForemInstance.sendgrid_enabled?
smtpapi_header = { category: ["Digest Email"] }.to_json
headers["X-SMTPAPI"] = smtpapi_header
end
mail(to: @user.email, subject: subject)
end

View file

@ -8,8 +8,11 @@ RSpec.describe DigestMailer do
describe "#digest_email" do
before do
allow(article).to receive(:title).and_return("test title")
allow(Settings::SMTP).to receive(:provided_minimum_settings?).and_return(true)
allow(Settings::SMTP).to receive(:from_email_address).and_return(from_email_address)
allow(Settings::SMTP).to receive_messages(
provided_minimum_settings?: true,
from_email_address: from_email_address,
)
allow(ForemInstance).to receive(:sendgrid_enabled?).and_return(true)
end
it "works correctly", :aggregate_failures do
@ -21,5 +24,14 @@ RSpec.describe DigestMailer do
expected_from = "#{Settings::Community.community_name} Digest <#{from_email_address}>"
expect(email["from"].value).to eq(expected_from)
end
it "includes the correct X-SMTPAPI header for SendGrid", :aggregate_failures do
email = described_class.with(user: user, articles: [article]).digest_email.deliver_now
expect(email.header["X-SMTPAPI"]).not_to be_nil
smtpapi_header = JSON.parse(email.header["X-SMTPAPI"].value)
expect(smtpapi_header).to have_key("category")
expect(smtpapi_header["category"]).to include("Digest Email")
end
end
end