2008-03-10
GmailのSMTPサーバを利用したメールスクリプト
既にたくさんの方が書いているのでスクリプトとしての意味はないですが、お勉強目的でやってみました。
#!/usr/local/bin/ruby -Ku
require 'net/smtp'
require 'rubygems'
require 'tlsmail'
class GSMTP
def initialize(username, password)
@smtp = 'smtp.gmail.com'
@helo = 'gmai.com'
@username = username
@password = password
@from_addr = username
end
def send(to_addr, subject, text)
body = <<EOF
From: #{@from_addr}
To: #{to_addr}
Subject: #{subject}
Content-Type: text/plain; charset="UTF-8"
#{text}
EOF
gmail = Net::SMTP.new(@smtp, 25)
gmail.enable_tls(OpenSSL::SSL::VERIFY_NONE)
gmail.start(@helo, @username, @password, :login) do |smtp|
smtp.send_mail(body, @from_addr, to_addr)
end
end
end
mail = GSMTP.new("youraddress@gmail.com", "password")
mail.send('anyone@somehost.com', '件名', 'ここが本文。')