Sendgrid用于Ruby on Rails应用程序中的电子邮件功能

时间:2015-01-30 20:57:11

标签: ruby-on-rails email sendgrid

我想使用Sendgrid来管理我在朋友的帮助下开发的3.2.2版本rails应用程序的外发电子邮件。她在本地/开发构建中使用gmail在应用程序内部发送电子邮件。我需要启动并运行sendgrid。

我甚至无法让它在本地工作。

来自我的development.rb文件



config.action_mailer.default_url_options = { :host => 'localhost:3030' }
  
  config.action_mailer.smtp_settings = {
    :user_name      => ENV['EMAIL_USERNAME'],
    :password       => ENV['EMAIL_PASSWORD'],
    :domain         => 'myapplicationdomain.com',
    :address        => 'smtp.sendgrid.net',
    :port           => 587,
    :authentication => 'plain',
    :enable_starttls_auto => true
  }
  config.action_mailer.delivery_method = :smtp




然后我在我的应用程序的根目录中有一个包含以下内容的变量文件:



export EMAIL_USERNAME=sendgridusername
export EMAIL_PASSWORD=sendgridpassword
export MAIL_TO=report@myapplicationdomain.com




以下是我邮寄的代码



class StatusMailer < ActionMailer::Base
  default from: "reports@myapplicationdomain.com"
  def status_report(report)
      @greeting = "Hello"
      @report = report
      if ENV['MAIL_TO']
        email = ENV['MAIL_TO'] if ENV['MAIL_TO']
      else
        email = @report.user.email
      end
      @statuses = @report.statuses
      @reviewers = @report.user.reviewers
      bcc = []
      @reviewers.each do |reviewer|
        bcc.append(reviewer.email)
      end
      @bcc = bcc
      mail(to: email, bcc: bcc, subject: 'Status Report')
  end
end
&#13;
&#13;
&#13;

我错过了其他一些设置吗?那个变量中的MAIL_TO字段怎么样,我不确定应该设置什么,或者是否需要声明它。

我应该编辑另一个文件吗?几天前我有这个工作,但功能不知何故溜走了:0

Rails服务器说电子邮件已发送,但sendgrid没有记录;电子邮件也不会被分发列表中的地址接收。

提前感谢你的帮助。

2 个答案:

答案 0 :(得分:0)

您的config/environments/development.rb是否有以下设置?

config.action_mailer.raise_delivery_errors = true
config.action_mailer.perform_deliveries = true

如果没有,请将它们添加到配置文件中并重新启动服务器。

<强>更新

此错误表示您未经过身份验证。您确定ENV['EMAIL_USERNAME']ENV['EMAIL_PASSWORD']变量的值是否存在/正确吗?

答案 1 :(得分:0)

这篇文章: Sendgrid / email sending issues in Ruby on Rails (hosted on Heroku)

让我起来跑步。关键是将SMTP和sendgrid信息放在environment.rb文件中。

我无法解释为什么会产生差异,但事实确实如此。

相关问题