メールを送信できるようになる

Railsからメールを送信してみる!

 

メール送信 - 気にすんな - アットウィキ

開発中にいちいちメールが飛ぶのがウザいとお思いのあなたへーletter_opener - リア充爆発日記

Rails の ActionMailer でメール送信処理

Gmail を使って Net::SMTPAuthenticationError が出力される場合の解決法 - 大学生からの Web 開発

 

手順

・コンフィグ設定

メーラー生成

・app/mailers/notice_mailer.rbを修正して、メール送信処理を作る

・app/views/notice_mailer/sendmail_confirm.text.erbに本文テンプレートをかく

・メール送信アクションを作って叩く

 

コンフィグ設定

ドメインだったり、送り先のメアド、PWだったりを設定。

普通に書いちゃったからGitHubとかにあげるのはやめよう?

メアドパスワードバレてガバガバになるぞ

 

メーラー生成

rails g mailer NoticeMailer sendmail_new_comment

 

メール送信処理

app/mailers/notice_mailer.rb

def sendmail_new_comment(blog, entry, comment)
@blog = blog
@entry = entry
@comment = comment
mail to: "aaa@gmail.com",
subject: "題名"
end

 

送信内容

新しいコメントが登録されました。

承認または削除してください。

Blog: <%= @blog.title %>
Entry: <%= @entry.title %>
Comment: <%= @comment.body %>

 

メール送信アクション

def create
@comment = Comment.new(comment_params)
@comment.save
# binding.pry
NoticeMailer.sendmail_new_comment(@blog, @entry, @comment).deliver

 

 

letter_opener

いちいちメールきたらうざいよね?って時の開発用

Gemfile

group :development do
  gem "letter_opener", "~> 1.1.0"
end

config/environment/development.rb

  config.action_mailer.delivery_method = :letter_opener

 

URLの出力 

config.action_mailer.default_url_options= { host:'localhost:3000' }

 

URL: < <%= url_for([@blog, @entry]) %> >

 

なぜかurl_forをおこなう前にどこでもいいから < がないと機能しない。