Rails 4邮件程序通过Gmail

时间:2013-11-28 04:01:50

标签: ruby-on-rails-4 actionmailer

我正在尝试从我的Rails 4应用发送电子邮件。当我通过应用程序发送它时,我没有收到错误,但没有发送电子邮件。如果我将控制器的update行代码输入到Rails控制台,那么电子邮件就会通过。

development.rb

TheCMF::Application.configure do
  # Settings specified here will take precedence over those in config/application.rb.

  # In the development environment your application's code is reloaded on
  # every request. This slows down response time but is perfect for development
  # since you don't have to restart the web server when you make code changes.
  config.cache_classes = false

  # Do not eager load code on boot.
  config.eager_load = false

  # Show full error reports and disable caching.
  config.consider_all_requests_local       = true
  config.action_controller.perform_caching = false

  # Don't care if the mailer can't send.
  config.action_mailer.delivery_method = :smtp
  config.action_mailer.raise_delivery_errors = false
  config.action_mailer.perform_deliveries = true
  config.action_mailer.default_url_options = { host: "localhost:3000" }

  # Print deprecation notices to the Rails logger.
  config.active_support.deprecation = :log

  # Raise an error on page load if there are pending migrations
  config.active_record.migration_error = :page_load

  # Debug mode disables concatenation and preprocessing of assets.
  # This option may cause significant delays in view rendering with a large
  # number of complex assets.
  config.assets.debug = true

  ActionMailer::Base.smtp_settings = {
      address: "smtp.gmail.com",
      enable_starttls_auto: true,
      port: 587,
      authentication: :plain,
      user_name: ENV["GMAIL_USERNAME"],
      password: ENV["GMAIL_PASSWORD"]
  }  
end

users_controller.rb     def update

  if current_user?(@user) && current_user.admin? && params[:user][:access] != "admin"
    flash[:error] = "An admin cannot remove ones admin status."
    render :edit
  elsif @user.access != "active" && params[:user][:access] == "active"
    flash[:notice] = "User has been activated"
    @user.access = "active"
    @user.save
    @user.send_user_activation
    redirect_to users_path
  elsif @user.update(user_params)
    flash[:notice] = "User has been updated."
    redirect_to users_path
  else
    flash[:error] = "User has not been updated."
    render :edit, layout: "admin"
  end    
end

如果我在Rails控制台中键入以下行,电子邮件将被正常发送:

user = User.find(5)
user.access = "active"
user.save
user.send_user_activation

send_user_activation

def send_user_activation
  ActivationMailer.activate_user(self).deliver
end

0 个答案:

没有答案