9

我正在使用 Ruby on Rails 创建一个简单的非盈利应用程序。我必须设置以下设置才能使用 Gmail 发送电子邮件:

Depot::Application.configure do

config.action_mailer.delivery_method = :smtp

config.action_mailer.smtp_settings = {
    address:"smtp.gmail.com",
    port:587,
    domain:"domain.of.sender.net",
    authentication: "plain",
    user_name:"dave",
    password:"secret",
    enable_starttls_auto: true
}

end

我对这些东西完全陌生,不知道我到底应该做什么。

  1. 如果我有 gmail 帐户,如何填充上述设置?我是否需要购买域名并且可以从谷歌购买才能使用上述设置?
  2. 在我的电脑上设置邮件服务器更好吗?我看过 教程,但据我所知,我仍然需要购买一个域名。

此外,正如这里所说:

设置电子邮件服务器是一个困难的过程,涉及许多不同的程序,每个程序都需要正确配置。

由于这个和我糟糕的技能,我正在寻找最简单的解决方案。

我已经阅读了 rails action mailer教程并且对这些参数的用途有所了解,但是关于 Gmail 和邮件服务器的事情根本不清楚。

4

2 回答 2

19

您的邮件程序的配置应该/可以在两者中定义developmentproduction此配置的目的是当您设置它时,将使用actionmailer这些 SMTP 选项。你可以有一个简单的邮件,如下所示:

梅勒

class UserMailer < ActionMailer::Base
  default :from => DEFAULT_FROM
  def registration_confirmation(user)
    @user = user
    @url = "http://portal.herokuapp.com/login"
    mail(:to => user.email, :subject => "Registered")

  end
end

控制器

 def create
    @title = 'Create a user'
    @user = User.new(params[:user])

    if @user.save
      UserMailer.registration_confirmation(@user).deliver
      redirect_to usermanagement_path
      flash[:success] = 'Created successfully.'
    else
      @title = 'Create a user'
      render 'new'
    end
  end

所以这里发生的是,当create使用该操作时,这会触发邮件程序UserMailer查看上面的 UserMailer 它使用 ActionMailer 作为基础。按照下面显示的 SMTP 设置,可以在config/environments/production.rbdevelopment.rb 和 development.rb中定义

您将拥有以下内容:

  config.action_mailer.default_url_options = { :host => 'portal.herokuapp.com' }
  config.action_mailer.delivery_method = :smtp
  config.action_mailer.smtp_settings = {
      :address              => 'smtp.gmail.com',
      :port                 => 587,
      :domain               => 'gmail.com',
      :user_name            => 'EMAIL_ADDRESS@gmail.com',
      :password             => 'pass',
      :authentication       => 'login',
      :enable_starttls_auto => true
  }

如果您想在开发模式下定义 SMTP 设置,您将替换

config.action_mailer.default_url_options = { :host => 'portal.herokuapp.com' }

config.action_mailer.default_url_options = { :host => 'IP ADDRESS HERE:3000' }

这应该是一个足够彻底的解释,可以让你朝着正确的方向前进。

于 2013-04-18T23:31:06.690 回答
2

一旦我将上述答案更改为

authentication: 'plain' 

并包括

config.action_mailer.raise_delivery_errors = true 

在我的开发环境中。

于 2014-03-04T21:51:59.287 回答