无法在轨道上的ruby中发送电子邮件

时间:2013-12-11 06:05:42

标签: ruby-on-rails ruby

我在delopment.rb上有配置,看起来像这样

Wiyo::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.raise_delivery_errors = true

  # 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

  config.action_mailer.perform_deliveries = true 

  #config.action_mailer.delivery_method = :smtp
  #config.action_mailer.smtp_settings = { :address => "localhost", :port => 1025 }

  #config.action_mailer.default_url_options = {
    #:host => "localhost:3000"
  #}


  config.action_mailer.delivery_method = :smtp
  config.action_mailer.smtp_settings = {
    :address => "smtp.gmail.com",
    :port => 587,
    :domain => 'gmail',
    :user_name => "atomambition@gmail.com",
    :password => "********",
    :authentication => 'plain',
    :enable_starttls_auto => true
  }
end

在预订梅勒控制器中,我有

class BookingMailer < ActionMailer::Base
  layout 'email'

  default from: "atomambition@gmail.com"

  def test_email
    mail(to: 'avashpdl@yahoo.com', subject: "test email from Avash Poudel")
  end
end

我已经从另一个控制器调用了test_email,比如

def about
  BookingMailer.test_email.deliver
  render:"about"
end

我被困住了。如何发送电子邮件。如果成功发送电子邮件,如何显示消息?

1 个答案:

答案 0 :(得分:0)

您缺少&#34; default_url_options&#34;,请查看以下配置。

设置梅勒是非常直接的fr。以下是您需要流动的步骤:

创建邮件程序

$ rails生成邮件程序UserMailer

编辑邮件程序

class UserMailer < ActionMailer::Base
  default from: 'from@example.com'

  def welcome_email(user)
    @user = user
    @url  = 'http://example.com/login'
    mail(to: @user.email, subject: 'Welcome to My Awesome Site')
  end
end

环境配置

确保按照环境进行以下配置。

config.action_mailer.default_url_options = { :host => 'localhost:3000' }
config.action_mailer.delivery_method = :smtp
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = true
config.action_mailer.default :charset => "utf-8"

config.action_mailer.smtp_settings = {
  :enable_starttls_auto => true,
  :address => "smtp.gmail.com",
  :port => 587,
  :domain => "gmail.com",
  :authentication => :login,
  :user_name => 'xxxxxx@gmail.com',
  :password => 'xxxxxxxxxx',
}