我从事 Rails 和 Action Mailer 工作。我创建了一个表格。当访客填写表单并发送时,它应该触发表单的发送,将表单的数据记录在我的数据库中,将包含表单数据的自动电子邮件发送到访客选择的俱乐部的电子邮件,然后重定向设置后,会出现一条闪烁消息,确认一切顺利。除了电子邮件发送之外,一切正常。我被困了几天了,似乎找不到它不起作用的原因。我尝试通过GMail发送电子邮件,并且该帐户使用双重身份验证。 这是我的配置开发代码:
# Configurations pour l'environnement de développement
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: 'smtp.gmail.com',
port: 587,
domain: 'gmail.com',
user_name: '[email protected]',
password: 'abcdefghijk',
authentication: 'plain',
enable_starttls: true,
open_timeout: 5,
read_timeout: 5
}
这是我的试验控制器:
def create
@trial = Trial.new(trial_params)
club_name = params[:trial][:club]
club = Club.find_by(name: club_name)
if club
@trial.club_id = club.id
if @trial.save
ClubMailer.rendezvous_email(@trial, club).deliver_now
flash[:notice] = "Your message was successfully sent! 👍 You will be contacted soon by the club."
redirect_to root_path
else
flash.now[:danger] = "An error occurred. Please try again."
render :new
end
else
flash.now[:danger] = "Club not found."
render :new
end
end
这是我的邮件程序/club_mailer 文件:
class ClubMailer < ApplicationMailer
def rendezvous_email(trial, club)
@trial = trial
@club = club
begin
mail(to: @club.email, subject: "Free trial in #{club.name} club! 🎉")
rescue => e
Rails.logger.error "Error sending email: #{e.message}"
raise e
end
end
end
感谢 Dbugger、Mike 和 Smathy 的回复和帮助。我解决了这个问题,终于是凭证问题(主密钥)了!