Skip to content

Instantly share code, notes, and snippets.

@tomoya55
Created July 8, 2009 23:29
Show Gist options
  • Select an option

  • Save tomoya55/143297 to your computer and use it in GitHub Desktop.

Select an option

Save tomoya55/143297 to your computer and use it in GitHub Desktop.
require "rubygems"
require "nkf"
require "kconv"
require "net/smtp"
# Hog - Japanese ISO2022JP Mail Sender
# ==== Description
# sending Japanase Mail with ISO-2022-JP encoding.
# See the bottom of this source for a sample usage.
class Hog
def self.mail(options)
message, from, to = build_mail(options)
Net::SMTP.start('localhost', 25) do |smtp|
smtp.send_mail message, from, to
end
true
rescue
false
end
def self.gmail(options)
require "tlsmail"
account = options[:account]
login = options[:login]
message, from, to = build_mail(options)
Net::SMTP.enable_tls(OpenSSL::SSL::VERIFY_NONE)
Net::SMTP.start('smtp.gmail.com', '587', "localhost", account, login, :plain) do |smtp|
smtp.send_message message, from, to
end
true
rescue
false
end
def self.build_mail(options)
to = options[:to]
sender = options[:sender] || options[:from] || ''
from = options[:from] || 'hog@unknown'
subject = options[:subject] || ''
body = options[:body] || ''
message = <<EOT
From: #{iso2022jp_mime(sender)} <#{from}>
To: #{to.to_a.join(",\n ")}
Subject: #{iso2022jp_mime(subject)}
Date: #{Time::now.strftime("%a, %d %b %Y %X %z")}
Mime-Version: 1.0
Content-Type: text/plain; charset=ISO-2022-JP
Content-Transfer-Encoding: 7bit
#{NKF.nkf("-Wjm0", body)}
EOT
[message, from, to]
end
def self.iso2022jp_mime(subject)
"=?ISO-2022-JP?B?" + Kconv.tojis(subject).split(//,1).pack('m').chomp.delete("\r\n") + "?="
end
end
# Usage
if __FILE__ == $0
ja_txt = open('japanese.txt').read
Hog.mail(:to => 'someone@example.com', :from => 'other@example.com', :subject => 'Hello', :body => ja_txt)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment