From 3fad0cd06c2ff828dd9bb0bb18080572ee992f4f Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Sat, 19 Feb 2005 21:51:16 +0000 Subject: Added support for charsets for both subject and body. The default charset is now UTF-8 #673 [Jamis Buck] git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@699 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- actionmailer/lib/action_mailer/base.rb | 35 ++++++++++++++++++++++++++++------ 1 file changed, 29 insertions(+), 6 deletions(-) (limited to 'actionmailer/lib/action_mailer') diff --git a/actionmailer/lib/action_mailer/base.rb b/actionmailer/lib/action_mailer/base.rb index 61701d62b7..8b04c42ee0 100644 --- a/actionmailer/lib/action_mailer/base.rb +++ b/actionmailer/lib/action_mailer/base.rb @@ -83,10 +83,18 @@ module ActionMailer #:nodoc: @@deliveries = [] cattr_accessor :deliveries - attr_accessor :recipients, :subject, :body, :from, :sent_on, :headers, :bcc, :cc + @@default_charset = "utf-8" + cattr_accessor :default_charset + + @@encode_subject = true + cattr_accessor :encode_subject + + attr_accessor :recipients, :subject, :body, :from, :sent_on, :headers, :bcc, :cc, :charset, :encode_subject def initialize @bcc = @cc = @from = @recipients = @sent_on = @subject = @body = nil + @charset = @@default_charset.dup + @encode_subject = @@encode_subject @headers = {} end @@ -104,14 +112,22 @@ module ActionMailer #:nodoc: end end - def mail(to, subject, body, from, timestamp = nil, headers = nil) #:nodoc: - deliver(create(to, subject, body, from, timestamp, headers)) + def mail(to, subject, body, from, timestamp = nil, headers = {}, + encode = @@encode_subject, charset = @@default_charset + ) #:nodoc: + deliver(create(to, subject, body, from, timestamp, headers, charset)) end - def create(to, subject, body, from, timestamp = nil, headers = nil) #:nodoc: + def create(to, subject, body, from, timestamp = nil, headers = {}, + encode = @@encode_subject, charset = @@default_charset + ) #:nodoc: m = TMail::Mail.new - m.to, m.subject, m.body, m.from = to, subject, body, from + m.to, m.subject, m.body, m.from = to, + ( encode ? quoted_printable(subject,charset) : subject ), body, from m.date = timestamp.respond_to?("to_time") ? timestamp.to_time : (timestamp || Time.now) + + m.set_content_type "text", "plain", { "charset" => charset } + headers.each do |k, v| m[k] = v end @@ -124,6 +140,12 @@ module ActionMailer #:nodoc: send("perform_delivery_#{delivery_method}", mail) if perform_deliveries end + def quoted_printable( text, charset ) + text = text.gsub( /[^a-z ]/i ) { "=%02x" % $&[0] }. + gsub( / /, "_" ) + "=?#{charset}?Q?#{text}?=" + end + private def perform_delivery_smtp(mail) Net::SMTP.start(server_settings[:address], server_settings[:port], server_settings[:domain], @@ -153,7 +175,8 @@ module ActionMailer #:nodoc: end mail = create(mailer.recipients, mailer.subject, mailer.body, - mailer.from, mailer.sent_on, mailer.headers) + mailer.from, mailer.sent_on, mailer.headers, + mailer.encode_subject, mailer.charset) mail.bcc = mailer.bcc unless mailer.bcc.nil? mail.cc = mailer.cc unless mailer.cc.nil? -- cgit v1.2.3