A
A
antivir2011-08-18 10:36:08
ruby
antivir, 2011-08-18 10:36:08

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)

Russian text comes in abracadabra.

Answer the question

In order to leave comments, you need to log in

5 answer(s)
Z
zizop, 2011-08-18
@zizop

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.

M
mpetrunin, 2011-08-18
@mpetrunin

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.

R
Renat Ibragimov, 2011-08-18
@MpaK999

Look at the headers that come up, try the transcoder and add the default encoding Content-type: text/html; charset=utf-8

I
inox, 2011-08-18
@inox

And if you try like this:
messages = u'Тело письма'

A
akzhan, 2011-11-12
@akzhan

try

if RUBY_VERSION >= '1.9'
  Encoding.internal_encoding = 'UTF-8'
  Encoding.external_encoding = 'UTF-8'
end

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question