Ruby mail gem - character set in the delivery block

I am using a zip stone to send emails with UTF-8 content using this code

Mail.defaults do ... end Mail.deliver do from " user@example.com " to " otheruser@example.com " subject "Mäbülö..." body "Märchenbücher lösen Leseschwächen." end 

This works, but gives a warning

 Non US-ASCII detected and no charset defined. Defaulting to UTF-8, set your own if this is incorrect. 

Now, after repeated polling attempts related to the creation of documentation related to the mail, as well as to the source code, I still cannot establish the encoding. Message.rb has a charset= method, but when I add a call to charset, for example:

 Mail.deliver do from " user@example.com " to " otheruser@example.com " charset "UTF-8" subject "Mäbülö..." body "Märchenbücher lösen Leseschwächen." end 

I get this ArgumentError:

 /usr/local/lib/ruby/gems/1.9.1/gems/mail-2.4.4/lib/mail/message.rb:1423:in `charset': wrong number of arguments (1 for 0) (ArgumentError) 

How to configure the encoding in the delivery block?

+4
source share
3 answers

mail.charset() returns the current encoding, it does not allow you to set it and does not accept any arguments.

For this you need to use mail.charset = ...

In fact, this is possible inside the block with:

 Mail.deliver do from " user@example.com " to " otheruser@example.com " subject "Mäbülö..." body "Märchenbücher lösen Leseschwächen." charset = "UTF-8" end 

This is also possible without a block:

 mail = Mail.new mail.charset = 'UTF-8' mail.content_transfer_encoding = '8bit' mail.from = ... mail.to = ... mail.subject = ... mail.text_part do body ... end mail.html_part do content_type 'text/html; charset=UTF-8' body ... end mail.deliver! 
+10
source

you need to set the encoding for individual parts. The answer to this question is maxdec. Make sure you also do this for text_part.

This works for me.

 mail = Mail.deliver do charset='UTF-8' content_transfer_encoding="8bit" require 'pry';binding.pry to ' xxx@xxx.yy ' from ' yyy@yyy.ss ' subject "Tet with äöüß" text_part do content_type "text/plain; charset=utf-8" body <<-EOF this is a test with äöüß EOF end end mail.deliver! 
+1
source

Add

 # encoding: utf-8 

at the beginning of the file.

-2
source

Source: https://habr.com/ru/post/1483812/


All Articles