Answer the question
In order to leave comments, you need to log in
Ruby: how to send Russian email?
Greetings!
I have this code:
# -*- coding: UTF-8 -*-
require 'net/smtp'
def send_email(to, subject, message,opts={})
opts[:server] ||= 'smtp.mymail.ru'
opts[:port] ||= 25
opts[:domain] ||= "mail.from.domain"
opts[:user_name] ||="user"
opts[:password] ||= "password"
opts[:authentication] ||="plain"
opts[:from] ||= '[email protected]'
opts[:from_alias] ||= 'me'
opts[:subject] ||= subject
opts[:body] ||= message
msg = <<END_OF_MESSAGE
From: #{opts[:from_alias]} <#{opts[:from]}>
To: <#{to}>
Subject: #{opts[:subject]}
Content-type: text/html
#{opts[:body]}
END_OF_MESSAGE
Net::SMTP.start(opts[:server], opts[:port], opts[:domain], opts[:user_name], opts[:password], opts[:authentication]) do |smtp|
smtp.send_message msg, opts[:from], to
end
end
subject='Заголовок'
messages='Тело письма'
send_email('[email protected]',subject, message)
Answer the question
In order to leave comments, you need to log in
Try the pony gem. We use it to send Russian letters with a bang (and even Chinese ones).
require 'pony'
Pony.mail(
:to => '[email protected]',
:from => '[email protected],
:subject => 'mail subject',
:headers => headers,
:body => 'mail content',
:charset => 'utf-8',
:via => :smtp,
:via_options => {
:address => 'smtp.site.com',
:port => '25',
:user_name => '[email protected]',
:password => 'somepassword',
:authentification => :login, # :plain, :login, :cram_md5, no auth by default
:domain => "site.com" # the HELO domain provided by the client to the server
}
)
* This source code was highlighted with Source Code Highlighter.
Specifically, I can’t say anything about the net-smtp gem, but I can recommend using the gem mail: github.com/mikel/mail for this purpose. No problems with encoding and sending.
Used in Rails 3.
In principle, you can see how the problem of encodings is solved there.
Look at the headers that come up, try the transcoder and add the default encoding Content-type: text/html; charset=utf-8
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question