【问题标题】:Ruby on Rails - Not receiving 'Welcome Email' in ProductionRuby on Rails - 在生产中未收到“欢迎电子邮件”
【发布时间】:2019-10-23 22:04:39
【问题描述】:

我正在尝试在通过 Action Mailer 创建新用户时发送欢迎电子邮件。我已经按照这里的指南进行操作:https://guides.rubyonrails.org/action_mailer_basics.html

在localhost:3000上创建用户时邮件成功,但是当我在生产环境(Heroku)部署和测试时,没有成功。

更糟糕的是,我在 Heroku 日志中没有看到错误。

我现在不确定要检查什么。由于配置相同,我已尝试将所有内容移至 application.rb 文件(请参阅:ActionMailer doesn't work in production

我认为它可能会发送延迟消息,但我已经尝试过 UserMailer.with(user: @user).welcome_email.deliver_later 以及 deliver_now

config/application.rb

config.action_mailer.delivery_method = :smtp
    config.action_mailer.smtp_settings = {
      address:              'smtp.gmail.com',
      port:                 587,
      domain:               'gmail.com',
      user_name:            ENV["GMAIL_USERNAME"],
      password:             ENV["GMAIL_PASSWORD"],
      authentication:       'plain',
      enable_starttls_auto: true }

config/environments/production.rb

config.action_mailer.raise_delivery_errors = false
config.action_mailer.perform_caching = false

mailers/user_mailer

class UserMailer < ApplicationMailer
  default from: 'pickleballsocial@gmail.com'

  def welcome_email
    @user = params[:user]
    @url  = 'https://pickleballsocial.herokuapp.com'
    mail(to: @user.email, subject: 'Welcome Pickleball Social')
  end
end

users_controller

def create
    @user = User.new(user_params)

    respond_to do |format|
      if @user.save
        session[:user_id] = @user.id
        # Tell the UserMailer to send a welcome email after save
        UserMailer.with(user: @user).welcome_email.deliver_later

        format.html { redirect_to(@user, notice: 'User was successfully created.') }
        format.json { render json: @user, status: :created, location: @user }

        #redirect_to user_path(@user)
      else
        format.html { render action: 'new' }
        format.json { render json: @user.errors, status: :unprocessable_entity }
      end
    end
  end

【问题讨论】:

  • 我假设您已经确认来自ENV 的用户名和密码与您在localhost 上测试的相同?
  • @lurker 哇。谢谢,很高兴能谦虚。

标签: ruby-on-rails ruby heroku actionmailer


【解决方案1】:

我认为您的代码没有任何重大问题。我要检查的第一件事是确保凭据正确。如果这不是问题,没有日志信息,我不确定问题所在。

【讨论】:

    【解决方案2】:

    来自guides

    注意:自 2014 年 7 月 15 日起,Google 增加了安全措施,现在阻止来自它认为不太安全的应用程序的尝试。您可以在此处更改您的 Gmail 设置以允许尝试。如果您的 Gmail 帐户启用了 2 因素身份验证,那么您将需要设置应用程序密码并使用该密码而不是您的常规密码。或者,您可以使用另一个 ESP 发送电子邮件,方法是将上面的“smtp.gmail.com”替换为您的提供商的地址。

    您能通过该文章链接检查您的 gmail 安全设置吗?我想知道生产主机是否被阻止是因为它来自似乎不太安全的东西(托管服务器,而不是您的本地计算机)

    【讨论】:

      猜你喜欢
      • 2011-12-16
      • 2013-06-08
      • 2017-06-15
      • 1970-01-01
      • 1970-01-01
      • 2020-01-18
      • 1970-01-01
      • 2015-05-29
      • 1970-01-01
      相关资源
      最近更新 更多