Tuesday, 15 May 2012

ruby - Rails 3.1 mail encoding issue -



ruby - Rails 3.1 mail encoding issue -

i have payment mailer class creates email:

class paymentmailer < actionmailer::base include localewrapper helper :application def payment_notification(payment) host = payment.try(:host) @payment = payment @user = payment.user using_locale((@user && @user.locale) || i18n.locale) { attachments["#{set_default_domain_name_from(host)}_receipt_#{time.now.strftime("%y_%m_%d")}.pdf"] = wickedpdf.new.pdf_from_string( render_to_string(pdf: "#{set_default_domain_name_from(host)}_receipt_#{time.now.strftime("%y_%m_%d")}.pdf", template: filtre_mail_template_by_host(host), layout: "pdf.html")) headers['precedence'] = 'bulk' mail(:to => @user.email, :subject => i18n.t("mailer_subjects.payment_completed"), :from => "no-reply@#{set_default_domain_name_from(host)}") |format| format.text(:content_transfer_encoding => "base64") format.html end } end

i have observer method saves email in database:

def delivered_email(email) rails.logger.info email.inspect html_body = (email.multipart? ? email.html_part : email).body.to_s text_body = (email.multipart? ? email.text_part : email).body.to_s outgoing_mail = outgoingmail.new({ :to => email.to.join(', '), :from => email.from.join(', '), :subject => email.subject.to_s, :sent_at => email.date, :html_body => html_body, :text_body => text_body, :multipart => email.multipart?, }) outgoing_mail.save save_attachments(email, outgoing_mail) end

problem multipart email's plain text version, returns ascii 8bit string , mysql throws mysql2::error: wrong string value error.

(rdb:1) text_body "n\x16\xa7\x93*.~\x8a\xf2\xa2\xea\xdc\xa2{k\x89\xbb\xad\x8a\x89\xd2y\xea]}\xabmi\xc8fz{_\xa2\xbaz\xcag\xa7\xb5\xd7\xadj)l" (rdb:1) text_body.encoding #<encoding:ascii-8bit>

i have tried using #force_encoding method, there no success:

(rdb:1) text_body.force_encoding("utf-8") "n\u0016\xa7\x93*.~\x8a\xf2\xa2\xeaܢ{k\x89\xbb\xad\x8a\x89\xd2y\xea]}\xabmi\xc8fz{_\xa2\xbaz\xcag\xa7\xb5\u05edj)l"

why getting ascii 8bit string instead of utf-8?

ruby-on-rails ruby ruby-on-rails-3

No comments:

Post a Comment