Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
rubyで日本語のメール送信
# 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
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
#! /usr/bin/env ruby
require "./sendmail.rb"
sendmail("from@example.com", %w{to.1@example.com to.2@example.com to.3@example.com}, "日本語でメール送信するテスト 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