当我尝试使用rails(5.1.4)邮件时,我没有收到像我应该做的任何电子邮件。我按照官方的导轨指南来制作它。我的config / environment / development.rb:
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: 'smtp.gmail.com',
port: 587,
domain: 'gmail.com',
user_name: 'username@gmail.com',
password: 'app-password-from-google',
authentication: 'plain',
enable_starttls_auto: true
}
通过此配置,我在mailers / appointement_mailer.rb中制作了一个新邮件:
class AppointementMailer < ApplicationMailer
default :from => "username@gmail.com"
def appointement_information(user_email, data)
@email = user_email
@body = data
mail(:to => "username@gmail.com", :subject => "xxx")
end
end
此邮件程序由控制器控制的表单触发,它位于controllers / contacts_controller.rb下:
def new
end
def create
@appointement = appointement_params
AppointementMailer.appointement_information(@appointement['email'], @appointement['body'])
flash[:notice] = "Some message"
redirect_to articles_path
end
private
def appointement_params
params.require('appointement').permit(:email, :body)
end
表单正确显示闪存“Some Message”,并且控制台中没有写入错误。
答案 0 :(得分:1)
您需要在致电邮件时致电deliver_now
。
AppointementMailer.appointement_information(@appointement['email'], @appointement['body']).deliver_now