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

Can't send "from" an email containing brackets #32

Open
ghiculescu opened this issue Dec 11, 2024 · 1 comment
Open

Can't send "from" an email containing brackets #32

ghiculescu opened this issue Dec 11, 2024 · 1 comment

Comments

@ghiculescu
Copy link

We are seeing errors in production when sending email via Mailtrap where the from address includes brackets. For example

mail(from: "foo (bar) <[email protected]>")

Leads to this

image

I tried writing a failing test, I couldn't get it to crash, but it does fail:

diff --git a/spec/mailtrap/mail_spec.rb b/spec/mailtrap/mail_spec.rb
index 6bd5f88..301d545 100644
--- a/spec/mailtrap/mail_spec.rb
+++ b/spec/mailtrap/mail_spec.rb
@@ -13,7 +13,7 @@ RSpec.describe Mailtrap::Mail do
     let(:message) { Mail::Message.new(**message_params) }
     let(:message_params) do
       {
-        from: 'Mailtrap Test <[email protected]>',
+        from: 'Mailtrap (Test) <[email protected]>',
         to: 'To 1 <[email protected]>, [email protected]',
         cc: '[email protected], Cc 2 <[email protected]>',
         bcc: '[email protected], [email protected]',
@@ -23,7 +23,7 @@ RSpec.describe Mailtrap::Mail do
       }
     end

-    its(:from) { is_expected.to eq({ name: 'Mailtrap Test', email: '[email protected]' }) }
+    its(:from) { is_expected.to eq({ name: 'Mailtrap (Test)', email: '[email protected]' }) }
     its(:to) { is_expected.to eq([{ name: 'To 1', email: '[email protected]' }, { email: '[email protected]' }]) }
     its(:cc) { is_expected.to eq([{ email: '[email protected]' }, { name: 'Cc 2', email: '[email protected]' }]) }
     its(:bcc) { is_expected.to eq([{ email: '[email protected]' }, { email: '[email protected]' }]) }

image

So I suspect there's something wrong in the gem here, and maybe the test just isn't accurately replicating how Action Mailer works.

@leonid-shevtsov
Copy link
Member

leonid-shevtsov commented Dec 11, 2024

You should quote the display name if it contains parenthesis, they are a special character according to the RFC; the relevant part is:

display-name = phrase
phrase          =   1*word / obs-phrase
obs-phrase      =   word *(word / "." / CFWS)
word            =   atom / quoted-string
atom            =   [CFWS] 1*atext [CFWS]
atext           =   ALPHA / DIGIT /    ; Printable US-ASCII
                       "!" / "#" /        ;  characters not including
                       "$" / "%" /        ;  specials.  Used for atoms.
                       "&" / "'" /
                       "*" / "+" /
                       "-" / "/" /
                       "=" / "?" /
                       "^" / "_" /
                       "`" / "{" /
                       "|" / "}" /
                       "~"

Or you can use the email+name form, and I'm pretty sure we will quote it for you:

{ email: '[email protected]', name: 'Mailtrap (Test)' }

But I admit that stripping the parens is sketchy, will look into it.

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

No branches or pull requests

2 participants