Skip to content

Instantly share code, notes, and snippets.

@tamoot
Created Dec 20, 2010
Embed
What would you like to do?
SMTP over SSL + GMailでメールを送信したい
require 'net/smtp'
require 'rubygems'
require 'tmail'
require 'kconv'
require 'tlsmail'
smtp_server = 'smtp.gmail.com'
smtp_port = 587
from_addr = 'gmail_user@gmail.com'
to_addrs = ['da@re.ka', 'do@nata.ka']
user = 'gmail_user'
pass = 'gmail_pass'
mail = TMail::Mail.new
mail.to = to_addrs
mail.from = from_addr
mail.subject = "メールのテストです".tojis
mail.date = Time.now
mail.mime_version = '1.0'
mail.set_content_type 'text', 'plain', {'charset' => 'iso-2022-jp'}
mail.body = "RubyでGMailを使ってメールを送信する\nRuby逆引きレシピAdvent Calendar\n13日目のサンプル".tojis
ssl_smtp = Net::SMTP.new(smtp_server, smtp_port)
ssl_smtp.enable_tls(OpenSSL::SSL::VERIFY_NONE)
ssl_smtp.start('localhost', user, pass, :plain) do |smtp|
smtp.send_mail(mail.to_s, from_addr, *to_addrs)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment