0

I'm trying to generate a form so visitors can send an email to a fixed address without cluttering the database. When I test the form, rails returns this error...

Net::SMTPAuthenticationError in ContactsController#create 

It appears the answer is to allow access to gmail for less secure apps. How can I retain the functionality without lowering security?

controllers/contacts_controller.rb

class ContactsController < ApplicationController
  def new
    @contact = Contact.new
  end

  def create
    @contact = Contact.new(params[:contact])
    if @contact.valid?
      ContactMailer.contact_submit(@contact).deliver
      flash[:notice] = "Thank you for your email, I'll respond shortly"
      redirect_to new_contact_path
    else
      render :new
    end
  end
end

mailers/contact_mailer.rb

class ContactMailer < ActionMailer::Base
  default to: "#{ENV['GMAIL_USERNAME']}@gmail.com"

  def contact_submit(msg)
    @msg = msg
    mail(from: @msg.email, name: @msg.name, message: @msg.message)
  end
end

models/contact.rb

class Contact
  include ActiveModel::Validations
  include ActiveModel::Conversion
  extend ActiveModel::Naming

  attr_accessor :name, :email, :message

  validates_format_of :email, :with => /\A([\w\.%\+\-]+)@([\w\-]+\.)+([\w]{2,})\z/i
  validates_presence_of :message
  validates_presence_of :name

  def initialize(attributes = {})
    attributes.each do |name, value|
      send("#{name}=", value)
    end
  end

  def persisted?
    false
  end
end

config/environments/development.rb

  config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }
  config.action_mailer.delivery_method = :smtp
  config.action_mailer.perform_deliveries = true
  config.action_mailer.default :charset => 'utf-8'
  config.action_mailer.smtp_settings = {
    address: 'smtp.gmail.com',
    port: 587,
    domain: 'localhost:3000',
    user_name: ENV['GMAIL_USERNAME'],
    password: ENV['GMAIL_PASSWORD'],
    authentication: 'plain',
    enable_starttls_auto: true
  }

config/environments/production.rb

  config.action_mailer.default_url_options = { host: ENV['WEBSITE'] }
  config.action_mailer.delivery_method = :smtp
  config.action_mailer.smtp_settings = {
    address: 'smtp.gmail.com',
    port: 587,
    domain: ENV['WEBSITE'],
    user_name: ENV['GMAIL_USERNAME'],
    password: ENV['GMAIL_PASSWORD'],
    authentication: 'plain',
    enable_starttls_auto: true
  }
n.milsht
  • 163
  • 3
  • 17

1 Answers1

1

Did you configure your SMTP Settings in ActiveMailer?

Furthermore you will very likely run into a Spam Problem when you just use a random email address from with the google smtp server.

The better idea is to use a fixed sender address (e.g. your own one) and put the original address in the text. That's how most email forms work.

Edit: According to another StackOverflow answer, you need to enable less secure apps in your email settings.

Community
  • 1
  • 1
leifg
  • 8,668
  • 13
  • 53
  • 79
  • I remember I encountered a similar problem when I tried to mail to my own gmail address. IIRC it has to do with G safety settings, yes. I solved it with a free mail service, like Mailtrap. Would that be an option for you? Mailtrap catches all the mails you send from your app. – Mauddev Jan 22 '17 at 20:10
  • I did configure the stmp settings, the code is listed above. I'm also using a fixed email address, it's in my env file and referenced in the above code as an ENV constant. – n.milsht Jan 22 '17 at 21:03
  • So have you configured your settings in your GMail account? to this SO answer: http://stackoverflow.com/questions/25872389/rails-4-how-to-correctly-configure-smtp-settings-gmail#answer-32019587 – leifg Jan 22 '17 at 21:17
  • The predicate of my question is looking for a way around lowering these security settings. – n.milsht Jan 22 '17 at 22:26
  • Sorry, didn't see that. I guess in this case: there is no way in using GMail with Rails and SMTP. You probably have to use a service like Mandrill or similar. – leifg Jan 22 '17 at 23:27