【发布时间】: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