Rails中Action Mailer结合Gmail无法自动发送邮件问题
使用Rails Action Mailer发送Gmail邮件失败排查
我用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: 'myemail@gmail.com', password: 'abcdefghijk', authentication: 'plain', enable_starttls: true, open_timeout: 5, read_timeout: 5 }
Trials控制器create方法代码
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
ClubMailer代码
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
内容的提问来源于stack exchange,提问作者Laure Dev
相关产品推荐
相关产品推荐

