Using rails 7, mail_form gem for 'contact us' page and sendGrid. When I using Gmail settings for sending mail, it works and I get the 'contact us' form in my inbox but when I use sendGrid settings, it says the emails get sent but I don't see them in my inbox/junk/spam etc... folders. I am currently using Devise for Users and either gmail settings or sendGrid settings work to send devise emails. I set my email address as 'primary sender' through sendGrid settings which is why I am successfully receiving Devise emails. I can't figure out 'contact us' form is being successfully sent via both gmail and sendGrid yet not receiving them in my inbox. I receive Devise emails fine. Any ideas?
development.rb
# Don't care if the mailer can't send.
config.action_mailer.raise_delivery_errors = true
config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }
# config.action_mailer.smtp_settings = {
# :address => 'smtp.gmail.com',
# :port => 587,
# :user_name => '[email protected]',
# :password => Rails.application.credentials.gmail_password,
# :authentication => 'plain',
# :enable_starttls_auto => true
# }
ActionMailer::Base.smtp_settings = {
:user_name => 'apikey', # This is the string literal 'apikey', NOT the ID of your API key
:password => Rails.application.credentials.sendgrid_secret_api, # This is the secret sendgrid API key which was issued during API key creation
:domain => Rails.application.credentials.domain,
:address => 'smtp.sendgrid.net',
:port => 587,
:authentication => :plain,
:enable_starttls_auto => true
}
home_controller.rb
class HomeController < ApplicationController
# Landing page
def contact
@contact = Contact.new
end
def create_contact_message
@contact = Contact.new(params[:contact])
@contact.request = request
if @contact.deliver
flash[:success] = 'Message sent!'
redirect_to action: :contact
else
flash[:error] = 'Could not send message'
render :contact
end
end
end
models/contact.rb
class Contact < MailForm::Base
attribute :name, validate: true
attribute :email, validate: /\A([\w\.%\+\-]+)@([\w\-]+\.)+([\w]{2,})\z/i
attribute :subject, validate: true
attribute :message, validate: true
attribute :nickname, captcha: true
# Declare the e-mail headers. It accepts anything the mail method
# in ActionMailer accepts.
def headers
{
:subject => "Contact Form Inquiry",
:to => "[email protected]",
:from => "[email protected]" # <~ Primary send on sendGrid
}
end
end
contact.html.erb
<!-- Start Contact Form -->
<div class="col-lg-8 ">
<h3 class="text-center">Send a message</h3>
<%= simple_form_for(@contact, method: :post, url: create_contact_message_path, defaults: { input_html: { class: 'contact-form row' } }) do |f| %>
<div class="col-lg-12 mb-4">
<div class="form-floating">
<%= f.input_field :name, class: 'form-control form-control-lg light-300', placeholder: 'Name' %>
<label for="floatingname light-300">Name</label>
</div>
</div><!-- End Input Name -->
<div class="col-lg-12 mb-4">
<div class="form-floating">
<%= f.input_field :email, class: 'form-control form-control-log light-300', placeholder: 'Email' %>
<label for="floatingemail light-300">Email</label>
</div>
</div><!-- End Input Email -->
<div class="col-12">
<div class="form-floating mb-4">
<%= f.input_field :subject, class: 'form-control form-control-lg light-300', placeholder: 'Subject' %>
<label for="floatingsubject light-300">Subject</label>
</div>
</div><!-- End Input Subject -->
<div class="col-12">
<div class="form-floating mb-3">
<%= f.input_field :message, as: :text, class: 'form-control light-300', rows: '8', placeholder: 'Message', style: 'height: 200px' %>
<label for="floatingtextarea light-300">Message</label>
</div>
</div><!-- End Textarea Message -->
<div class="d-none">
<%= f.input :nickname %>
</div>
<div class="col-md-12 col-12 m-auto text-end">
<button type="submit" class="btn btn-secondary rounded-pill px-md-5 px-4 py-2 radius-0 text-light light-300">Send Message</button>
</div>
<% end %>
</div>
<!-- End Contact Form -->
console output after filling out contact form and submitting
14:59:10 web.1 | Started POST "/create_contact_message" for ::1 at 2022-06-09 14:59:10 -0700
14:59:10 web.1 | ActiveRecord::SchemaMigration Pluck (0.9ms) SELECT "schema_migrations"."version" FROM "schema_migrations" ORDER BY "schema_migrations"."version" ASC
14:59:10 web.1 | Processing by HomeController#create_contact_message as HTML
14:59:10 web.1 | Parameters: {"authenticity_token"=>"[FILTERED]", "contact"=>{"name"=>"Some dude", "email"=>"[email protected]", "subject"=>"Hello", "message"=>"message here. blah.", "nickname"=>""}}
14:59:10 web.1 | Rendering /home/ramona/.rbenv/versions/3.0.0/lib/ruby/gems/3.0.0/gems/mail_form-1.9.0/lib/mail_form/views/mail_form/contact.erb
14:59:10 web.1 | Rendered /home/ramona/.rbenv/versions/3.0.0/lib/ruby/gems/3.0.0/gems/mail_form-1.9.0/lib/mail_form/views/mail_form/contact.erb (Duration: 2.7ms | Allocations: 1181)
14:59:10 web.1 | MailForm::Notifier#contact: processed outbound mail in 8.8ms
14:59:11 web.1 | Delivered mail [email protected] (1300.2ms)
14:59:11 web.1 | Date: Thu, 09 Jun 2022 14:59:10 -0700
14:59:11 web.1 | From: [email protected]
14:59:11 web.1 | To: [email protected]
14:59:11 web.1 | Message-ID: <[email protected]>
14:59:11 web.1 | Subject: Contact Form Inquiry
14:59:11 web.1 | Mime-Version: 1.0
14:59:11 web.1 | Content-Type: text/html;
14:59:11 web.1 | charset=UTF-8
14:59:11 web.1 | Content-Transfer-Encoding: 7bit
14:59:11 web.1 |
14:59:11 web.1 | <h4 style="text-decoration:underline">Contact Form Inquiry</h4>
14:59:11 web.1 |
14:59:11 web.1 |
14:59:11 web.1 | <p><b>Name:</b>
14:59:11 web.1 | Some dude</p>
14:59:11 web.1 |
14:59:11 web.1 | <p><b>Email:</b>
14:59:11 web.1 | [email protected]</p>
14:59:11 web.1 |
14:59:11 web.1 | <p><b>Subject:</b>
14:59:11 web.1 | Hello</p>
14:59:11 web.1 |
14:59:11 web.1 | <p><b>Message:</b>
14:59:11 web.1 | message here. blah.</p>
14:59:11 web.1 |
14:59:11 web.1 |
14:59:11 web.1 | Redirected to http://localhost:3000/contact
14:59:11 web.1 | Completed 302 Found in 1339ms (ActiveRecord: 0.0ms | Allocations: 10384)
14:59:11 web.1 |
14:59:11 web.1 |
14:59:11 web.1 | Started GET "/contact" for ::1 at 2022-06-09 14:59:11 -0700
14:59:11 web.1 | Processing by HomeController#contact as HTML
14:59:11 web.1 | Rendering layout layouts/application.html.erb
14:59:11 web.1 | Rendering home/contact.html.erb within layouts/application
14:59:11 web.1 | Rendered home/contact.html.erb within layouts/application (Duration: 28.0ms | Allocations: 10826)
14:59:12 web.1 | Rendered layouts/_navbar.html.erb (Duration: 1.2ms | Allocations: 370)
14:59:12 web.1 | Rendered layouts/_flash.html.erb (Duration: 0.9ms | Allocations: 201)
14:59:12 web.1 | Rendered layouts/_footer.html.erb (Duration: 5.7ms | Allocations: 2302)
14:59:12 web.1 | Rendered layout layouts/application.html.erb (Duration: 125.8ms | Allocations: 39002)
14:59:12 web.1 | Completed 200 OK in 132ms (Views: 129.7ms | ActiveRecord: 0.0ms | Allocations: 40513)
14:59:12 web.1 |
14:59:12 web.1 |