LoginSignup
39

More than 5 years have passed since last update.

posted at

RubyでGmailのSMTPサーバを使ってメールを送信する

Rubyコード

以下のコードでGmailのSMTPサーバ経由でメールが送信できる。

require 'mail'

mail = Mail.new

options = { :address              => "smtp.gmail.com",
            :port                 => 587,
            :domain               => "smtp.gmail.com",
            :user_name            => '<username>@gmail.com',
            :password             => '<password>',
            :authentication       => :plain,
            :enable_starttls_auto => true  }        
mail.charset = 'utf-8'
mail.from "from@example.com"
mail.to "to@example.com"    
mail.subject "メールタイトル"
mail.body "メール本文"
mail.delivery_method(:smtp, options)
mail.deliver


参考URL

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
What you can do with signing up
39