ActionMailer単体でメールを送信したいんです
require 'action_mailer' ActionMailer::Base.smtp_settings = { address: 'smtp.gmail.com', port: 587, domain: 'smtp.gmail.com', authentication: :plain, user_name: '俺のメール', password: '俺のパスワード', enable_starttls_auto: true } ActionMailer::Base.raise_delivery_errors = true ActionMailer::Base.delivery_method = :smtp class HelloMailer < ActionMailer::Base default :from => 'hogehoge@sample.com' def createMessage(to, subject, body) mail(:to => to, :subject => subject) do |format| format.text { render :text => body } end end end HelloMailer.createMessage('宛先@gmail.com', 'hello', 'こんにちは、世界!').deliver # => Net::SMTPAuthenticationError: 534-5.7.9 Application-specific password required. Learn more at
なんでや。。Application-specific password?
そうだった、Googleアカウントは2段階認証にしてたんだった。
Googleのアプリケーション固有パスワードを作成してsmtp_settingsに設定したらうまく送信できた。
参考
- takeisa memo: [Ruby]Rails3.2のAction Mailer単体でメールを送信する
- ActionMailer Railsアプリからメールを送信 - 酒と泪とRubyとRailsと
- GMailのSMTPサーバをRedmineで使う - r-labs
実はそもそもSMTPとかPOPとかよく知らない状態だったので。。