【问题标题】:Does Sendgrid require mail gem and how to set it up with action mailer?Sendgrid 是否需要邮件 gem 以及如何使用 action mailer 进行设置?
【发布时间】:2015-11-22 11:43:26
【问题描述】:

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 文档或 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

关于发生了什么变化以及如何在 Heruku/Rails4.1 上设置 sendgrid 的任何好的示例?

【问题讨论】:

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


    【解决方案1】:

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

    1. 转到yourappdirectory/config/initializers

    2. 创建一个名为mail.rb的文件

    3. 将上面的代码粘贴到那里

    就是这样。配置好之后就可以开始使用了。

    【讨论】:

      【解决方案2】:

      Heroku 为 sendgrid 提供插件。 https://elements.heroku.com/addons/sendgrid

      通过添加它,它会自动为您设置环境变量,即 SENDGRID_USERNAME 发送GRID_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 类将保持与通常使用的相同。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-11-18
        • 1970-01-01
        • 2011-05-15
        • 1970-01-01
        • 2014-09-09
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多