gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 01:05:41 24 4
gpt4 key购买 nike

我正在尝试在我的开发环境中设置一个简单的 contact_us 页面,用户可以在其中使用我创建的表单发送查询。我已经设置了 ActiveMailer 和联系人模型/ Controller / View ,但它似乎无法正常工作。有任何想法吗?我的日志似乎显示正在发送邮件。

行动 postman

class ContactConfirmation < ActionMailer::Base
default from: "from@example.com"

def receipt(contact)
@contact = contact

mail to: 'myname@example.com',
subject: contact.subject
end
end

收据
<%= @contact.first_name %> <%= @contact.last_name %>

Writes:

<%= @contact.description %>

联系人 Controller
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)
end
end

接触模型
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
end
end

在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#create 中的 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 不会发送邮件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21074759/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com