-
Notifications
You must be signed in to change notification settings - Fork 939
Support non-ASCII addresses (IDN) #1444
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
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 |
---|---|---|
|
@@ -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 | ||
|
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 :-)