Created
March 27, 2009 16:59
-
-
Save koseki/86778 to your computer and use it in GitHub Desktop.
rubyで日本語のメール送信
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Send Japanese mail using Gmail SMTP server. You need tlsmail. | |
# $ sudo gem install tlsmail | |
require "rubygems" | |
require "tlsmail" | |
require "nkf" | |
require "net/smtp" | |
def sendgmail(from, to, subject, body, user, pass, host = "smtp.gmail.com", port = 587) | |
body = <<EOT | |
From: #{from} | |
To: #{to.to_a.join(",\n ")} | |
Subject: #{NKF.nkf("-WMm0", 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 | |
Net::SMTP.enable_tls(OpenSSL::SSL::VERIFY_NONE) | |
Net::SMTP.start(host, port, "localhost.localdomain", user, pass, "plain") do |smtp| | |
smtp.send_mail body, from, to | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "nkf" | |
require "net/smtp" | |
def sendmail(from, to, subject, body, host = "localhost", port = 25) | |
body = <<EOT | |
From: #{from} | |
To: #{to.to_a.join(",\n ")} | |
Subject: #{NKF.nkf("-WMm0", 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 | |
Net::SMTP.start(host, port) do |smtp| | |
smtp.send_mail body, from, to | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env ruby | |
require "./sendmail.rb" | |
sendmail("[email protected]", %w{[email protected] [email protected] [email protected]}, "日本語でメール送信するテスト MIME Bエンコード ASCII 日本語 ASCIIと日本語 ASCII ASCII あいうえおあいうえおあいうえお", <<EOT) | |
日本語でメール送信するテストです。 | |
こんにちは。 | |
ありがとう。 | |
さようなら。 | |
EOT |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment