【问题标题】:sending email not working with rails发送电子邮件不适用于 Rails
【发布时间】:2018-01-20 07:08:20
【问题描述】:

我构建了一个示例应用程序来尝试使邮件程序正常工作。该应用程序托管在https://blooming-brushlands-80122.herokuapp.com, 源代码是here。这是我与动作邮件相关的配置:

config.action_mailer.delivery_method = :sendmail
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = true
config.action_mailer.default_options = {from: 'no-reply@example.com'}

用于发送邮件

 def create
      @user = User.new(user_params)

      respond_to do |format|
        if @user.save

          # Sends email to user when user is created.
          ExampleMailer.sample_email(@user).deliver

          format.html { redirect_to @user, notice: 'User was successfully created.' }
          format.json { render :show, status: :created, location: @user }
        else
          format.html { render :new }
          format.json { render json: @user.errors, status: :unprocessable_entity }
        end
      end
    end

该应用不会向用户发送电子邮件。

有人知道为什么吗?

【问题讨论】:

  • 请不要大喊大叫
  • 改了标题_
  • 发布您发送邮件的代码以及您的配置文件
  • 更新了问题

标签: ruby-on-rails ruby ruby-on-rails-4 smtp sendmail


【解决方案1】:

此答案假设您使用的是 Heroku 的免费 sendgrid 插件,因为我看到您使用的是 sendmail。要添加它,只需登录 heroku 并将其添加为免费资源。由于您没有指定是尝试在本地还是在生产中发送电子邮件,所以我为两者添加了配置。在您的 config/environment/development.rb 文件中,您应该执行以下操作以从本地发送:

  config.action_mailer.delivery_method = :smtp  #Yours used sendmail, change to this.
  config.action_mailer.perform_deliveries = true #Needed if this is dev env. file
  ActionMailer::Base.smtp_settings = {
    :address        => 'smtp.sendgrid.net',
    :port           => '587',
    :authentication => :plain,
    :user_name      => ENV['KUNZIG_SENDGRID_USERNAME'],
    :password       => ENV['KUNZIG_SENDGRID_PASSWORD'],
    :domain         => 'localhost:3000',
    :enable_starttls_auto => true
  }

显然用您自己的环境替换用户名和密码。 Heroku 分配的变量。 Heroku 的配置。 vars 可以在设置选项卡下找到。只需点击“显示”按钮。

您的 environment/production.rb 文件将类似于:

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

请注意,唯一的变化是域名和环境变量,因为当您将 Sengrid 添加为插件时,Heroku 会自动设置这些变量。此外,将控制器中的 deliver 调用更改为 deliver_now,这样我们就知道这不是您的后台工作人员配置的问题。

【讨论】:

  • 嘿嘿,试过你说的还是不发送:/我把你请求的更改也推送到了github
  • 您是在 Heroku 上测试您的应用还是在本地测试应用?
  • 在 heroku 上 :) 它已启动并运行,甚至日志显示没有错误,但未收到电子邮件,您可以在此处找到应用程序 blooming-brushlands-80122.herokuapp.com
  • 不要推送到 github 然后大声笑推送到 heroku!运行“git add -A”,然后运行“git commit -m 'Added config changes for email”,然后运行“git push heroku master”
  • 哈哈哈我集成了heroku github。好消息它有效!但是帐户被暂停了大声笑。因为我想给自己发这么多邮件哈哈哈谢谢你的帮助!!!
猜你喜欢
  • 2011-03-03
  • 1970-01-01
  • 1970-01-01
  • 2013-07-28
  • 1970-01-01
  • 2014-12-31
  • 2013-05-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多