Reputation: 892
I'm making simple contact form without any fancy gems and I think I'm finished, except nothing works in production.
I'm using rails 4.2.0
in Cloud9
IDE. For production I'm using Heroku
and for mailer Mailgun
service witch is enabled in Heroku.
When i try to send mail in development environment i see email being sent in server console, but when I try to do it in production, it doesn't send email and it doesn't redirect me back to contact form page(as it do in development environment). Maybe I'm just not using Mailgun correctly, if so, can you give some good gmail
tutorial for mailer, so I can make it work on Heroku and Digital Ocean hosting platform.
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 => "[email protected]"
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 %>
Upvotes: 1
Views: 816
Reputation: 24337
The SMTP configuration looks wrong. It should be loading the config values from environment variables, but you appear to be trying to place the values in there:
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,
}
This should actually be setup like:
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,
}
Then you will need to set those MAILGUN_USERNAME and MAILGUN_PASSWORD environment variables in your Cloud 9 Run panel.
Upvotes: 3