I'm trying to build a contact us page in my rails 5 app using the mailform gem. I followed this youtube video explaining the process: https://www.youtube.com/watch?v=QIoORYeBdhs
However, I can't get the emails to actually deliver in either development or production mode, and I'm not really sure why. Any help would be much appreciated!
Here's what I have:
Welcome.rb:
class Welcome < MailForm::Base
attribute :name, :validate => true
attribute :email, :validate => /\A([\w+\-].?)+@[a-z\d\-]+(\.[a-z]+)*\.[a-z]+\z/i
attribute :message, :validate => true
attribute :nickname, :captcha => true
def headers
{
:subject => "Contact from About Site",
:to => "[email protected]",
:from => %("#{name}" <#{email}>)
}
end
end
welcome_controller:
class WelcomeController < ApplicationController
def index
@welcome = Welcome.new
end
def create
@welcome = Welcome.new(params[:welcome])
@welcome.request = request
if @welcome.deliver
flash.now[:error] = nil
else
flash.now[:error] = 'Cannot send message.'
render :index
end
end
end
The form itself: (index.html.erb)
<section id="contact">
<div class="container">
<%= form_for welcome_index_path do |f| %>
<%= f.label :name %>
<%= f.text_field :name, required: true %>
<br>
<%= f.label :email %>
<%= f.email_field :email, required: true %>
<br>
<%= f.label :message %>
<%= f.text_area :message, as: :text %>
<div class="hidden">
<%= f.label :nickname %>
<%= f.text_field :nickname, hint: "Leave this field blank" %>
</div>
<br>
<%= f.submit 'Send Message', class: "btn btn-primary btn-large" %>
<% end %>
</div>
</section>
And now my config files:
production.rb, using heroku with sendgrid:
# email
config.action_mailer.default_url_options = {host: 'https://my_heroku_app.herokuapp.com'}
config.action_mailer.delivery_method = :smtp
ActionMailer::Base.smtp_settings = {
:address => 'smtp.sendgrid.net',
:port => '587',
:authentication => :plain,
:user_name => ENV["SENDGRID_USERNAME"],
:password => ENV["SENDGRID_PASSWORD"],
:domain => 'heroku.com',
:enable_starttls_auto => true
}
development.rb, using gem mailcatcher:
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = true
config.action_mailer.default :charset => "utf-8"
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {:address => "localhost", :port => 1025}
My own answer It seems actually the code above works just fine, and there was simply a delay in sendgrid starting up. It took about two days, I'm not sure why, but now it's working fine without a real modification to the code