首页 > 解决方案 > Ruby on Rails - 在生产中未收到“欢迎电子邮件”

问题描述

当通过 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_laterdeliver_now

配置/应用程序.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 }

配置/环境/production.rb

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

邮件程序/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

标签: ruby-on-railsrubyherokuactionmailer

解决方案


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


推荐阅读