我将通过本教程(链接:https://www.murdo.ch/blog/build-a-contact-form-with-ruby-on-rails-part-1)在我现有的rails应用程序上创建一个简单的联系我们表单。完成教程并测试我的开发环境后,我注意到我的smtp设置干扰了我的联系我们表单。基本上我只想向电子邮件地址发送邮件,所以我不需要发件人邮件地址。我尝试删除development.rb文件中的SMTP设置,但单击提交按钮时出现此错误:
在MessagesController中创建的Errno :: ECONNREFUSED 连接被拒绝 - 连接(2)for" localhost" 1025港口
我尝试用不同的方式解决但我又回到了同样的错误。这是我的代码:
model/message.rb:
class Message
include ActiveModel::Model
attr_accessor :name, :email, :body
validates :name, :email, :body, presence: true
end
这是我的留言控制器:
class MessagesController < ApplicationController
def new
@message = Message.new
end
def create
@message = Message.new message_params
if @message.valid?
MessageMailer.contact_me(@message).deliver_now
redirect_to new_message_url, notice: "Message received"
else
render :new
end
end
def message_params
params.require(:message).permit(:name, :email, :body)
end
end
我的观点:new.html.erb:
<%= form_for @message, url: create_message_url do |f| %>
<%= notice %>
<%= @message.errors.full_messages.join(', ') %>
<%= f.text_field :name, placeholder: 'name' %>
<%= f.email_field :email, placeholder: 'email' %>
<%= f.text_area :body, placeholder: 'body' %>
<%= f.submit 'Send' %>
<% end %>
我的梅勒:
class MessageMailer < ApplicationMailer
def contact_me(message)
@greeting = 'Hi'
@body = message.body
mail to: "notifications@example.com", from: message.email
end
end
我的Development.rb / SMTP设置代码:
config.action_mailer.delivery_method = :smtp
config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = true
config.action_mailer.default :charset => "utf-8"
config.action_mailer.smtp_settings = {
:address => 'localhost',
:port => 587,
:domain => 'localhost:3000',
:user_name => 'example@gmail.com',
:password => '######',
:authentication => 'plain',
:enable_starttls_auto => true
}
我只是想摆脱smtp设置,但不幸的是我不能,是否有任何方式来覆盖这些设置或替代解决方案,或者我错过了什么, 任何帮助将不胜感激, 提前致谢