-
Notifications
You must be signed in to change notification settings - Fork 936
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
Support non-ASCII addresses (IDN) #1444
base: master
Are you sure you want to change the base?
Changes from all commits
2abad63
65a72fb
4c96392
9f2b136
ae549e2
68b352c
8fc7d72
8623156
044a190
a704e25
9db84f6
4b925e2
7ffb026
0ea61fd
d801386
f196c23
721fdcb
de05d2c
69162d6
407113a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -71,6 +71,18 @@ def address(output_type = :decode) | |
end | ||
end | ||
|
||
# Returns the address that is in the address itself with domain IDNA-encoded. | ||
# | ||
# a = Address.new('Mikel Lindsaar (My email address) <mikel@tést.lindsaar.net>') | ||
# a.address_idna #=> '[email protected]' | ||
def address_idna | ||
if d = domain_idna | ||
"#{local}@#{d}" | ||
else | ||
local | ||
end | ||
end | ||
|
||
# Provides a way to assign an address to an already made Mail::Address object. | ||
# | ||
# a = Address.new | ||
|
@@ -120,6 +132,17 @@ def domain(output_type = :decode) | |
Encodings.decode_encode(strip_all_comments(get_domain), output_type) if get_domain | ||
end | ||
|
||
# Returns the domain part (the right hand side of the @ sign in the email address) of | ||
# the address in IDNA encoding. | ||
# | ||
# a = Address.new('Mikel Lindsaar (My email address) <mikel@tést.lindsaar.net>') | ||
# a.domain_idna #=> 'xn--tst-bma.lindsaar.net' | ||
def domain_idna | ||
if d = domain | ||
Encodings.idna_encode(d) | ||
end | ||
end | ||
|
||
# Returns an array of comments that are in the email, or nil if there | ||
# are no comments | ||
# | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,9 +8,11 @@ class SmtpEnvelope #:nodoc: | |
|
||
attr_reader :from, :to, :message | ||
|
||
def initialize(mail) | ||
self.from = mail.smtp_envelope_from | ||
def initialize(mail, smtputf8_supported: false) | ||
# Net::SMTP::Address was added in net-smtp 0.3.1 (included in Ruby 3.1). | ||
@smtputf8_supported = smtputf8_supported && defined?(Net::SMTP::Address) | ||
self.to = mail.smtp_envelope_to | ||
self.from = mail.smtp_envelope_from | ||
self.message = mail.encoded | ||
end | ||
|
||
|
@@ -19,7 +21,10 @@ def from=(addr) | |
raise ArgumentError, "SMTP From address may not be blank: #{addr.inspect}" | ||
end | ||
|
||
@from = validate_addr 'From', addr | ||
addr = validate_addr 'From', addr | ||
addr = Net::SMTP::Address.new(addr, 'SMTPUTF8') if @smtputf8 | ||
|
||
@from = addr | ||
end | ||
|
||
def to=(addr) | ||
|
@@ -43,14 +48,31 @@ def message=(message) | |
|
||
private | ||
def validate_addr(addr_name, addr) | ||
if addr.bytesize > MAX_ADDRESS_BYTESIZE | ||
raise ArgumentError, "SMTP #{addr_name} address may not exceed #{MAX_ADDRESS_BYTESIZE} bytes: #{addr.inspect}" | ||
end | ||
|
||
if /[\r\n]/.match?(addr) | ||
raise ArgumentError, "SMTP #{addr_name} address may not contain CR or LF line breaks: #{addr.inspect}" | ||
end | ||
|
||
if !addr.ascii_only? | ||
if @smtputf8_supported | ||
# The SMTP server supports the SMTPUTF8 extension, so we can legally pass | ||
# non-ASCII addresses, if we specify the SMTPUTF8 parameter for MAIL FROM. | ||
@smtputf8 = true | ||
elsif Encodings.idna_supported? | ||
# The SMTP server does not announce support for the SMTPUTF8 extension, so do the | ||
# IDNa encoding of the domain part client-side. | ||
addr = Address.new(addr).address_idna | ||
end | ||
|
||
# If we cannot IDNa-encode the domain part, of if the local part contains | ||
# non-ASCII characters, there is no standards-complaint way to send the | ||
# mail via a server without SMTPUTF8 support. Our best chance is to just | ||
# pass the UTF8-encoded address to the server. | ||
end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Again, could the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the only place where I could remove the |
||
|
||
if addr.to_s.bytesize > MAX_ADDRESS_BYTESIZE | ||
raise ArgumentError, "SMTP #{addr_name} address may not exceed #{MAX_ADDRESS_BYTESIZE} bytes: #{addr.inspect}" | ||
end | ||
|
||
addr | ||
end | ||
end | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,6 +26,12 @@ | |
end | ||
end | ||
|
||
it "should allow us to instantiate an empty address object and call address_idna" do | ||
[nil, '', ' '].each do |input| | ||
expect(Mail::Address.new(input).address_idna).to be_nil | ||
end | ||
end | ||
|
||
it "should allow us to instantiate an empty address object and call local" do | ||
[nil, '', ' '].each do |input| | ||
expect(Mail::Address.new(input).local).to be_nil | ||
|
@@ -38,6 +44,12 @@ | |
end | ||
end | ||
|
||
it "should allow us to instantiate an empty address object and call domain_idna" do | ||
[nil, '', ' '].each do |input| | ||
expect(Mail::Address.new(input).domain_idna).to be_nil | ||
end | ||
end | ||
|
||
it "should allow us to instantiate an empty address object and call name" do | ||
[nil, '', ' '].each do |input| | ||
expect(Mail::Address.new(input).name).to be_nil | ||
|
@@ -119,7 +131,18 @@ | |
expect(a.domain).to eq result | ||
end | ||
|
||
it "should give back the formated address" do | ||
it "should give back the IDNA-encoded domain" do | ||
parse_text = 'Mikel Lindsaar <test@lindsäär.net>' | ||
result = 'xn--lindsr-fuaa.net' | ||
a = Mail::Address.new(parse_text) | ||
if Mail::Encodings.idna_supported? | ||
expect(a.domain_idna).to eq result | ||
else | ||
expect {a.domain_idna}.to raise_error("Must install simpleidn gem") | ||
end | ||
end | ||
|
||
it "should give back the formatted address" do | ||
parse_text = 'Mikel Lindsaar <[email protected]>' | ||
result = 'Mikel Lindsaar <[email protected]>' | ||
a = Mail::Address.new(parse_text) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -285,6 +285,97 @@ def redefine_verify_none(new_value) | |
expect(MockSMTP.deliveries[0][2]).to eq %w([email protected]) | ||
end | ||
|
||
it "IDNA-encodes non-ASCII From and To addresses without SMTPUTF8 support" do | ||
Mail.defaults do | ||
delivery_method :smtp | ||
end | ||
|
||
Mail.deliver do | ||
from "fröm@soméemail.com" | ||
to "tö@soméemail.com" | ||
end | ||
|
||
if Mail::Encodings.idna_supported? | ||
expect(MockSMTP.deliveries[0][1]).to eq 'frö[email protected]' | ||
expect(MockSMTP.deliveries[0][2]).to eq %w(tö@xn--somemail-d1a.com) | ||
else | ||
expect(MockSMTP.deliveries[0][1]).to eq 'fröm@soméemail.com' | ||
expect(MockSMTP.deliveries[0][2]).to eq %w(tö@soméemail.com) | ||
end | ||
end | ||
|
||
it "does not pass SMTPUTF8 parameter for ASCII From and To addresses with SMTPUTF8 support" do | ||
Mail.defaults do | ||
delivery_method :smtp | ||
end | ||
|
||
MockSMTP.capabilities = ['SMTPUTF8'] | ||
|
||
Mail.deliver do | ||
to "[email protected]" | ||
from "[email protected]" | ||
end | ||
|
||
expect(MockSMTP.deliveries[0][1]).to eq '[email protected]' | ||
expect(MockSMTP.deliveries[0][2]).to eq %w([email protected]) | ||
end | ||
|
||
it "passes SMTPUTF8 parameter for non-ASCII From address with SMTPUTF8 support" do | ||
Mail.defaults do | ||
delivery_method :smtp | ||
end | ||
|
||
MockSMTP.capabilities = ['SMTPUTF8'] | ||
|
||
Mail.deliver do | ||
from "fröm@soméemail.com" | ||
to "[email protected]" | ||
end | ||
|
||
if defined?(Net::SMTP::Address) | ||
from_address = MockSMTP.deliveries[0][1] | ||
expect(from_address).to be_instance_of Net::SMTP::Address | ||
expect(from_address.to_s).to eq 'fröm@soméemail.com' | ||
expect(from_address.parameters).to eq ['SMTPUTF8'] | ||
elsif Mail::Encodings.idna_supported? | ||
expect(MockSMTP.deliveries[0][1]).to eq 'frö[email protected]' | ||
else | ||
expect(MockSMTP.deliveries[0][1]).to eq 'fröm@soméemail.com' | ||
end | ||
|
||
expect(MockSMTP.deliveries[0][2]).to eq %w([email protected]) | ||
end | ||
|
||
it "passes SMTPUTF8 parameter for non-ASCII To address with SMTPUTF8 support" do | ||
Mail.defaults do | ||
delivery_method :smtp | ||
end | ||
|
||
MockSMTP.capabilities = ['SMTPUTF8'] | ||
|
||
Mail.deliver do | ||
to "tö@soméemail.com" | ||
from "[email protected]" | ||
end | ||
|
||
if defined?(Net::SMTP::Address) | ||
from_address = MockSMTP.deliveries[0][1] | ||
expect(from_address).to be_instance_of Net::SMTP::Address | ||
expect(from_address.to_s).to eq '[email protected]' | ||
expect(from_address.parameters).to eq ['SMTPUTF8'] | ||
|
||
expect(MockSMTP.deliveries[0][2]).to eq %w(tö@soméemail.com) | ||
else | ||
expect(MockSMTP.deliveries[0][1]).to eq '[email protected]' | ||
|
||
if Mail::Encodings.idna_supported? | ||
expect(MockSMTP.deliveries[0][2]).to eq %w(tö@xn--somemail-d1a.com) | ||
else | ||
expect(MockSMTP.deliveries[0][2]).to eq %w(tö@soméemail.com) | ||
end | ||
end | ||
end | ||
|
||
it "supports the null sender in the envelope from address" do | ||
Mail.deliver do | ||
to "[email protected]" | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Doesn't this result in two separate cases? You'll either return the local + domain or just the local. Wouldn't it be better to just return the local here and put the domain_idna check in the same area where we return the full address (ie, local plus domain) ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure I follow. This method just follows the same pattern as
Address#address
, i.e. it does not add@
if the address does not include a domain.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mikel Would you elaborate? :-)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mikel Ping :-)