使用Action Mailer发送电子邮件

时间:2015-05-25 23:10:25

标签: ruby ruby-on-rails-3 gmail actionmailer

我正在尝试使用Action Mailer(Ruby on Rails)向example@gmail.com发送电子邮件。正确执行sendactivation方法,并显示消息“Email sent”。但是,我从未收到任何电子邮件。实际上,输出“测试”从未打印过。我的webapp托管在Heroku Cedar-10上。

class UsersController < ApplicationController
  def sendactivation
      UserMailer.welcome_email()
      render :json => {
        :result => "Email sent"
      }
    end
  end
end

class UserMailer < ActionMailer::Base
  def welcome_email()
    mail(to: "example@gmail.com",
         body: "Hello",
         content_type: "text/html",
         subject: "Already rendered!")
    puts "Test"
  end
end

这是我在config / environment / production.rb上的配置。实际上我想用Office 365发送它,但我想用Gmail帐户调试会更容易。

  config.action_mailer.delivery_method = :smtp
  config.action_mailer.smtp_settings = {
    address:              'smtp.gmail.com',
    port:                 587,
    user_name:            'example@gmail.com',
    password:             '########',
    authentication:       'plain',
    enable_starttls_auto: true  }

我做错了什么?我的Gmail配置是否需要更改?

答案:除了标记的答案外,我还需要在welcome_email方法中设置“发件人”地址。

2 个答案:

答案 0 :(得分:1)

UserMailer.welcome_email.deliver_now

来自Rails Guide:见2.1.4节

class SendWeeklySummary
  def run
    User.find_each do |user|
      UserMailer.weekly_summary(user).deliver_now
    end
  end
end

答案 1 :(得分:1)

致电方法传递:

UserMailer.welcome_email.deliver
相关问题