I get my mail from gmail.com with gmail gem.
gm = Gmail.connect addr, pass
in_m = gm.inbox.find(:before => 5.days.ago).last
puts in_m.text_part.body # shows "Привет ...."
puts in_m.subject # shows "=?KOI8-R?B?z9Qg09XQxdLXwcraxdLB?="
puts in_m.subject.encoding # shows #<Encoding:US-ASCII>
I tried
in_m.subject.encode("UTF-8")
in_m.subject.force_encoding("KOI8-R").encode("UTF-8")
in_m.subject.force_encoding("US-ASCII").encode("UTF-8")
this not help me
How i can encode the subject of my mail?
Thanks..
String like "=?KOI8-R?B?z9Qg09XQxdLXwcraxdLB?=" is the mime encoded word and this decoded Base64, charset=KOI8-R. Structure of mimeWord is =?charset?decode type?decoded string ?=. So if get part of string "z9Qg09XQxdLXwcraxdLB" and decoded this with Base64, then encode to UTF-8 all is OK. Base64.decode64("z9Qg09XQxdLXwcraxdLB").encode("UTF-8"). Question is closed
Related
I want my app to response with body utf-8 and iso-8859-1 encoded
per requests with Accept-Charset="utf-8" or Accept-Charset="iso-8859-1".
The response body is always JSON.
In my controller, when I doing this
render(json: data, status: :created)
It response with Content-Type="application/json; charset=utf-8" as well.
But how to make a response with body iso-8859-1 encoded when request Accept-Charset="iso-8859-1"?
In order to do this, you can use the method force_encoding and encoding for example
data = {'name'=>'raghav'}.to_json
data.encoding #This would return what encoding the value as #<Encoding:UTF-8>
new_data = data.force_encoding('ISO-8859-1') #This would force the encoding
new_data.encoding #<Encoding:ISO-8859-1>
Also to do this on the specific case you can always read the request.headers hash to determine the encoding.
There is also another method called encode the main difference between these are force_encoding changes the way string is being read from bytes, and encode changes the way string is written without changing the output (if possible)
My code is
resp = HTTParty.get("http://sandbox.api.simsimi.com/request.p?key=e7501386-fca8-4723-b278-36755e917526&lc=ko&ft=1.0&text=#{params[:content]}")
and params[:content] is "안녕" now.
if i run this code,
I get following error
URI must be ascii only "http://sandbox.api.simsimi.com/request.p?key=e7501386-fca8-4723-b278-36755e917526&lc=ko&ft=1.0&text=\u00ED\u0095\u0098\u00EC\u009D\u00B4" (URI::InvalidURIError)
How can i send Korean string in URL?
(.encode("utf-8) is not working..)
As the error message says "URI must be ascii only", you may encode Korean to URI format as below.
require 'uri'
str = "안녕".force_encoding('ASCII-8BIT') # "\xEC\x95\x88\xEB\x85\x95"
URI::encode(str) # %EC%95%88%EB%85%95
Further info lies here: Ruby url encoding string
I already read the following: Send request to page with windows-1251 encoding from python
Yet I cannot print a correct string. I have tried nearly every combination of u''.join, encode and decode I could think about...
import requests
url = 'http://www.multitran.ru/c/m.exe'
payload = {'CL': '1', 's': 'foo', 'l1':'1'}
r = requests.post(url, data=payload)
# ????????? print(u''.join(r.text))
I'd be very glad to get a solution and an explanation, because I really do not get it!
I'm using ActionMailer to send emails, it works when given a normal email address, here is my code snippet
logger = Logger.new('logfile.log')
logger.info(#user.email)
mail(
charset: "UTF-8",
to: #user.email,
from: #from,
subject: #subject
)
in the log it shows the email address just fine with special characters.
but then I go to my development log and see this in the mail object, everything else is right
To: =?UTF-8?B?w7FAw7EuY29tPg==?=
I've tried to wrap it in quotes and using a different format like so:
("\"#{#user.name}\" <#{#user.email}>")
which translates to:
"name name" <test_ñ#yahoo.com>
no luck on these either, I just get a similar gibberish
=?UTF-8?Q?=22name_name=22_<test=5F=C3=B1#yahoo.com>?=
also tried to use "test_ñ"#yahoo.com
same results:
=?UTF-8?Q?=22test=5F=C3=B1=22#yahoo.com>?=
what am I missing here? is it something with encoding configs?
That's not gibberish. That's MIME encoding (RFC 2047). Everything's fine.
You can paste that line in MIME online decoder and ensure it decodes to To: my_user#exampl.com (email address redacted).
My app sends out an email with a URL in it. The url contains a query string attribute that is encrypted. I CGI escaped the encrypted value so that symbols like + * . etc are escaped. The escaped URL appears in the email as expected, but when we click on the link, the encrypted values are decrypted.
For Example, the url in the email is as follows
http://development.com/activate/snJAmJxkMo3WZ1sG27Aq?album_id=2&email=5M%2BjE1G6UB26tw/Ah%2Bzr1%2BJSSxeAoP6j&owner_id=4
email=5M%2BjE1G6UB26tw/Ah%2Bzr1%2BJSSxeAoP6j
when we click on this link the url in the browser appears as
http://development.com/activate/snJAmJxkMo3WZ1sG27Aq?album_id=2&email=5M+jE1G6UB26tw/Ah+zr1+JSSxeAoP6j&owner_id=4
email=5M+jE1G6UB26tw/Ah+zr1+JSSxeAoP6j
The + is substituted with space. As a result
params[:email] = 5M jE1G6UB26tw/Ah zr1 JSSxeAoP6j
which gives me a 404.
Is there any way I can avoid this situation. How can I make the url in the browser also appear as
http://development.com/activate/snJAmJxkMo3WZ1sG27Aq?album_id=2&email=5M%2BjE1G6UB26tw/Ah%2Bzr1%2BJSSxeAoP6j&owner_id=4
in the browser?
In order to avoid this situation I Hex encoded the email attribute so that the it contains only alphabets and numbers. Used these are the methods to Hex encode and decode.
convert string2hex:
def hexdigest_to_string(string)
string.unpack('U'*string.length).collect {|x| x.to_s 16}.join
end
convert hex2string
def hexdigest_to_digest(hex)
hex.unpack('a2'*(hex.size/2)).collect {|i| i.hex.chr }.join
end