Sendgrid是否需要邮件gem以及如何使用动作邮件设置它?

时间:2015-08-27 22:25:36

标签: ruby-on-rails ruby-on-rails-4 heroku sendgrid

Heroku文档说: https://devcenter.heroku.com/articles/sendgrid#ruby-rails

To send out emails through SendGrid, you need to configure the Mail class to have the correct values:
require 'mail'

Mail.defaults do
  delivery_method :smtp, {
    :address => 'smtp.sendgrid.net',
    :port => '587',
    :domain => 'heroku.com',
    :user_name => ENV['SENDGRID_USERNAME'],
    :password => ENV['SENDGRID_PASSWORD'],
    :authentication => :plain,
    :enable_starttls_auto => true
  }
end

Mail gem docs或heroku都没有明确指定Mail类的位置以及如何配置它。

过去我刚刚使用了Action Mailer这样的

class UserMailer < ActionMailer::Base
  default from: 'careerswitch.me@gmail.com'

  # Subject can be set in your I18n file at config/locales/en.yml
  # with the following lookup:
  #
  #   en.user_mailer.signup_confirmation.subject

  def signup_confirmation(user,subject_param='Welcome!')
    @user = user

    mail to: user.email,
         subject: subject_param  do |format|
         #format.text {render __method__}
         format.html {render __method__}
    end
  end

end

关于改变了什么以及如何在Herouku / Rails4.1上设置sendgrid的任何好例子?

2 个答案:

答案 0 :(得分:0)

这些是初始化设置。 应该是的生成器,但我认为没有。至少上次我是手动完成的。

  1. 转到yourappdirectory/config/initializers

  2. 创建名为mail.rb

  3. 的文件
  4. 将代码粘贴在上面

  5. 这就是全部。一旦你配置它,你就会好起来。

答案 1 :(得分:0)

Heroku为sendgrid提供了附加功能。 https://elements.heroku.com/addons/sendgrid

通过添加它,它将自动为您设置环境变量,即 SENDGRID_USERNAME SENDGRID_PASSWORD

配置附加组件后,您必须将其放入production.rb

ActionMailer::Base.smtp_settings = {
 :user_name => ENV['SENDGRID_USERNAME'],
 :password => ENV['SENDGRID_PASSWORD'],
 :domain => 'appname.herokuapp.com',
 :address => "smtp.sendgrid.net",
 :port => 587,
 :authentication => :plain,
 :enable_starttls_auto => true
}

Mailer类将保持与通常使用的相同。