高木のブログ

Rubyでメールを送信する方法(Gmail)

· 51 words · 1 minutes to read
Tags: Ruby

gemのインストール

$ sudo gem install mail

Rubyソースコード

require 'mail'

mail = Mail.new

options = {
  :address              => 'smtp.gmail.com',
  :port                 => 587,
  :domain               => 'smtp.gmail.com',
  :user_name            => 'アカウント@gmail.com',
  :password             => 'パスワード',
  :authentication       => :plain,
  :enable_starttls_auto => true
}

mail.charset = 'utf-8'
mail.from '送信元アドレス@gmail.com'
mail.to '送信先アドレス@gmail.com'
mail.subject '件名'
mail.body '内容'
mail.delivery_method(:smtp, options)
mail.deliver

Categories


Tags