ruby-on-rails – ActionMailer不会发送邮件?

ruby-on-rails – ActionMailer不会发送邮件?,第1张

概述我正在尝试在我的开发环境中设置一个简单的contact_us页面,用户可以使用我创建的表单发送查询.我有ActiveMailer和Contact模型/控制器/视图都设置但它似乎没有正常工作.有任何想法吗?我的日志似乎显示正在发送的邮件. 的ActionMailer class ContactConfirmation < ActionMailer::Base default from: "fro 我正在尝试在我的开发环境中设置一个简单的contact_us页面,用户可以使用我创建的表单发送查询.我有ActiveMailer和Contact模型/控制器/视图都设置但它似乎没有正常工作.有任何想法吗?我的日志似乎显示正在发送的邮件.

的ActionMailer

class ContactConfirmation < ActionMailer::Base  default from: "from@example.com"  def receipt(contact)    @contact = contact    mail to: 'myname@example.com',subject: contact.subject  endend

收据

<%= @contact.first_name %> <%= @contact.last_name %>Writes:<%= @contact.description %>

ContactsController

class ContactsController < ApplicationController  def new    @contact = Contact.new  end  def create    @contact = Contact.new(contact_params)    if @contact.submit_contact_info      redirect_to users_path,notice: 'Submission successful. Somebody will get back to you shortly.'    else      render :new    end  end  protected  def contact_params    params.require(:contact).permit(:first_name,:last_name,:email,:subject,:description)  endend

联系型号

class Contact < ActiveRecord::Base  valIDates_presence_of :email  valIDates :email,format: { with: /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i }  valIDates_presence_of :subject  valIDates_presence_of :description  valIDates_presence_of :first_name  valIDates_presence_of :last_name  def submit_contact_info    if save      ContactConfirmation.receipt(self).deliver      return true    end  endend

联系人在contacts / new.HTML.erb文件中呈现的表单

<%= simple_form_for @contact do |f| %>  <%= f.input :first_name %>  <%= f.input :last_name %>  <%= f.input :email %>  <%= f.input :subject %>  <%= f.input :description,as: :text %>  <%= f.submit 'submit Contact Form' %><% end %>

在Initializers文件夹中,我有一个smtp.rb文件:

if Rails.env.development?  ActionMailer::Base.delivery_method = :smtp  ActionMailer::Base.smtp_settings = {    address: "localhost",port: 1025  }end

更改我的配置后,现在在ContactConfirmation.receipt(self).deliver上收到以下错误:

在ContactsController #creore中的Errno :: ECONNREFUSED
连接被拒绝 – 连接(2)

def submit_contact_info    if save      ContactConfirmation.receipt(self).deliver      return true    end  end
解决方法 所以让我们从头开始:

在开发模式下,默认情况下不会传递邮件,也不会提交传递错误,这就是您没有错误和没有电子邮件的原因.要更改此项,请将以下内容添加到配置并重新启动服务器:

配置/环境/ development.rb

config.action_mailer.raise_delivery_errors = true config.action_mailer.perform_deliverIEs = true

现在您应该收到一些错误或收到电子邮件.正如您在评论中所写的那样,您收到了Connection refused错误,因此这意味着您的邮件程序守护程序未运行.你正在使用mailcatcher(因此1025端口)所以安装后你应该通过简单的mailcatcher运行它.现在你应该没有错误,你应该能够在浏览到localhost:1080后看到你的电子邮件.

总结

以上是内存溢出为你收集整理的ruby-on-rails – ActionMailer不会发送邮件?全部内容,希望文章能够帮你解决ruby-on-rails – ActionMailer不会发送邮件?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/langs/1281930.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-09
下一篇2022-06-09

发表评论

登录后才能评论

评论列表(0条)

    保存