Created
November 30, 2010 12:16
-
-
Save znz/721603 to your computer and use it in GitHub Desktop.
net/smtp でメールを送るサンプル
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/ruby | |
# -*- coding: utf-8 -*- | |
require 'net/smtp' | |
require 'nkf' | |
require 'time' | |
input_password = proc do | |
STDERR.print "type password: " | |
begin | |
system("stty -echo") | |
pass = gets.chomp | |
ensure | |
system("stty echo") | |
end | |
STDERR.puts | |
pass | |
end | |
smtp_host = 'mx.example.jp' | |
smtp_port = 587 | |
helo_name = 'hello.example.jp' | |
smtp_auth_user = 'john' | |
from = "test\@example.jp" | |
bcc = from | |
subject = NKF.nkf('-WMm0j', "テストメール") | |
body = NKF.nkf('-Wm0j', <<BODY) | |
テストメールです。 | |
BODY | |
rfc822_date = Time.now.rfc822 | |
context = OpenSSL::SSL::SSLContext.new | |
ssl_params = {} | |
ssl_params[:verify_mode] = OpenSSL::SSL::VERIFY_PEER | |
store = OpenSSL::X509::Store.new | |
store.set_default_paths | |
ssl_params[:cert_store] = store | |
context.set_params(ssl_params) | |
smtp = Net::SMTP.new(smtp_host, smtp_port) | |
smtp.set_debug_output $stderr | |
smtp.enable_starttls(context) | |
smtp.start(helo_name, smtp_auth_user, input_password.call, :login) do |conn| | |
[ | |
"example\@example.jp", | |
"sample\@example.jp", | |
"test\@example.jp", | |
].each do |to| | |
message = <<-DATA | |
From: #{from} | |
To: #{to} | |
Subject: #{subject} | |
Date: #{rfc822_date} | |
Mime-Version: 1.0 | |
Content-Type: text/plain; charset=ISO-2022-JP | |
Content-Transfer-Encoding: 7bit | |
#{body} | |
DATA | |
conn.send_message message, from, to, bcc | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment