Rails 邮件程序中 Ruby 中的联系表无法正常工作
Contact form in Ruby on Rails mailer not working
我正在制作简单的联系表,没有任何花哨的宝石,我想我已经完成了,除了在生产中没有任何效果。
我在 Cloud9
IDE 中使用 rails 4.2.0
。对于生产,我使用 Heroku
,对于邮件 Mailgun
服务 在 Heroku 中启用。
当我尝试在开发环境中发送邮件时,我在服务器控制台中看到正在发送电子邮件,但是当我尝试在生产环境中发送邮件时,它不会发送电子邮件,也不会将我重定向回联系表单页面(就像在开发环境中一样)。也许我只是没有正确使用 Mailgun,如果是的话,你能给一些好的 gmail
邮件程序教程,这样我就可以让它在 Heroku 和 Digital Ocean 托管平台上工作。
messages_controller.rb
class MessagesController < ApplicationController
def new
@message = Message.new
end
def create
@message = Message.new(message_params)
if @message.valid?
MessageMailer.message_me(@message).deliver_now
redirect_to new_message_path, notice: "Thankyou for your message."
else
render :new
end
end
private
def message_params
params.require(:message).permit(:name, :email, :subject, :content)
end
end
message_mailer.rb
class MessageMailer < ApplicationMailer
# use your own email address here
default :to => "MYMAIL@gmail.com"
def message_me(msg)
@msg = msg
mail(from: @msg.email, subject: @msg.subject, body: @msg.content)
end
end
config/environments/production.rb
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:port => ENV['587'],
:address => ENV['smtp.mailgun.org'],
:user_name => ENV['SANDBOX USERNAME GIVEN BY MAILGUN'],
:password => ENV['PASWORD'],
:domain => 'MYAPP.herokuapp.com', #eg: 'yourappname.herokuapp.com'
:authentication => :plain,
}
models/message.rb
class Message
include ActiveModel::Model
attr_accessor :name, :email, :subject, :content
validates :name, :email, :subject, :content, presence: true
end
views/messages/new.html.erb
<%= form_for @message do |f| %>
<% if @message.errors.any? %>
<div id="error_explanation">
<h2><%= "#{pluralize(@message.errors.count, "error")} prohibited this message from being sent:" %></h2>
<ul>
<% @message.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
<ul>
</div>
<% end %>
<div class="field">
<%= f.label :name %>
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :email %>
<%= f.email_field :email %>
</div>
<div class="field">
<%= f.label :subject %>
<%= f.text_field :subject %>
</div>
<div class="field">
<%= f.label :content %>
<%= f.text_area :content %>
</div>
<div class="actions">
<%= f.submit 'Send', class: 'button' %>
</div>
<% end %>
SMTP 配置看起来不对。它应该从环境变量加载配置值,但您似乎试图将值放在那里:
config.action_mailer.smtp_settings = {
:port => ENV['587'],
:address => ENV['smtp.mailgun.org']
:user_name => ENV['SANDBOX USERNAME GIVEN BY MAILGUN'],
:password => ENV['PASWORD'],
:domain => 'MYAPP.herokuapp.com', #eg: 'yourappname.herokuapp.com'
:authentication => :plain,
}
实际上应该这样设置:
config.action_mailer.smtp_settings = {
:port => 587,
:address => 'smtp.mailgun.org'
:user_name => ENV['MAILGUN_USERNAME'],
:password => ENV['MAILGUN_PASSWORD'],
:domain => 'MYAPP.herokuapp.com', #eg: 'yourappname.herokuapp.com'
:authentication => :plain,
}
然后您需要在 Cloud 9 运行 面板中设置那些 MAILGUN_USERNAME 和 MAILGUN_PASSWORD 环境变量。
我正在制作简单的联系表,没有任何花哨的宝石,我想我已经完成了,除了在生产中没有任何效果。
我在 Cloud9
IDE 中使用 rails 4.2.0
。对于生产,我使用 Heroku
,对于邮件 Mailgun
服务 在 Heroku 中启用。
当我尝试在开发环境中发送邮件时,我在服务器控制台中看到正在发送电子邮件,但是当我尝试在生产环境中发送邮件时,它不会发送电子邮件,也不会将我重定向回联系表单页面(就像在开发环境中一样)。也许我只是没有正确使用 Mailgun,如果是的话,你能给一些好的 gmail
邮件程序教程,这样我就可以让它在 Heroku 和 Digital Ocean 托管平台上工作。
messages_controller.rb
class MessagesController < ApplicationController
def new
@message = Message.new
end
def create
@message = Message.new(message_params)
if @message.valid?
MessageMailer.message_me(@message).deliver_now
redirect_to new_message_path, notice: "Thankyou for your message."
else
render :new
end
end
private
def message_params
params.require(:message).permit(:name, :email, :subject, :content)
end
end
message_mailer.rb
class MessageMailer < ApplicationMailer
# use your own email address here
default :to => "MYMAIL@gmail.com"
def message_me(msg)
@msg = msg
mail(from: @msg.email, subject: @msg.subject, body: @msg.content)
end
end
config/environments/production.rb
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:port => ENV['587'],
:address => ENV['smtp.mailgun.org'],
:user_name => ENV['SANDBOX USERNAME GIVEN BY MAILGUN'],
:password => ENV['PASWORD'],
:domain => 'MYAPP.herokuapp.com', #eg: 'yourappname.herokuapp.com'
:authentication => :plain,
}
models/message.rb
class Message
include ActiveModel::Model
attr_accessor :name, :email, :subject, :content
validates :name, :email, :subject, :content, presence: true
end
views/messages/new.html.erb
<%= form_for @message do |f| %>
<% if @message.errors.any? %>
<div id="error_explanation">
<h2><%= "#{pluralize(@message.errors.count, "error")} prohibited this message from being sent:" %></h2>
<ul>
<% @message.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
<ul>
</div>
<% end %>
<div class="field">
<%= f.label :name %>
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :email %>
<%= f.email_field :email %>
</div>
<div class="field">
<%= f.label :subject %>
<%= f.text_field :subject %>
</div>
<div class="field">
<%= f.label :content %>
<%= f.text_area :content %>
</div>
<div class="actions">
<%= f.submit 'Send', class: 'button' %>
</div>
<% end %>
SMTP 配置看起来不对。它应该从环境变量加载配置值,但您似乎试图将值放在那里:
config.action_mailer.smtp_settings = {
:port => ENV['587'],
:address => ENV['smtp.mailgun.org']
:user_name => ENV['SANDBOX USERNAME GIVEN BY MAILGUN'],
:password => ENV['PASWORD'],
:domain => 'MYAPP.herokuapp.com', #eg: 'yourappname.herokuapp.com'
:authentication => :plain,
}
实际上应该这样设置:
config.action_mailer.smtp_settings = {
:port => 587,
:address => 'smtp.mailgun.org'
:user_name => ENV['MAILGUN_USERNAME'],
:password => ENV['MAILGUN_PASSWORD'],
:domain => 'MYAPP.herokuapp.com', #eg: 'yourappname.herokuapp.com'
:authentication => :plain,
}
然后您需要在 Cloud 9 运行 面板中设置那些 MAILGUN_USERNAME 和 MAILGUN_PASSWORD 环境变量。