首页 > 解决方案 > Rails 邮件程序不发送电子邮件

问题描述

当我尝试使用 rails (5.1.4) 邮件程序时,我没有收到任何我应该做的电子邮件。我按照官方的 Rails 指南来制作它。我的 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

表单正确显示闪存“一些消息”,并且控制台中没有写入错误。

标签: ruby-on-rails

解决方案


您需要deliver_now打电话给邮寄者。

AppointementMailer.appointement_information(@appointement['email'], @appointement['body']).deliver_now

推荐阅读