1
votes

I'm really struggling with this. Can anyone see what's wrong here? I can't seem to get the mailer to work... I am trying to build a contact form on my site, so users can send me emails from within the site. I create a "contact" with a name, email, subject, and body, then deliver it as an email.

controllers/contacts_controller.rb

class ContactsController < ApplicationController
  def index
  end

  def new
    @contact = Contact.new
  end

  def create
    @contact = Contact.new(contact_params)
    respond_to do |format|
      if @contact.save
        UserMailer.mail_jill(@contact).deliver

        format.html { redirect_to(@contact, :notice => 'User was successfully created.') }
      else
        format.html { render :action => "new" }
        format.json { render :json => @contact.errors, :status => :unprocessable_entity }
      end
    end
  end


  def contact_params
    params.require(:contact).permit(:name, :email, :subject, :body)
  end
end

mailers/user_mailer.rb

class UserMailer < ActionMailer::Base
  def mail_jill(message)
    @name = message.name
    @email = message.email
    @subject = message.subject
    @body = message.body

    mail(to: '<my_email_address>', subject: @subject, body: @body, from: @email)
  end
end

views/user_mailer/mail_jill.html.erb:

<h1>From, <%= @contact.name %></h1>
<p>
@contact.body
</p>
<p>-<%= @contact.email %></p>

$RAILS_ENV.rb:

ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.sendmail_settings = {
  :address              => "smtp.gmail.com",
  :port                 => "465",
  :domain               => "gmail.com",
  :user_name            => "<my_gmail_address>",
  :password             => "<my_gmail_password>",
  :authentication       => "plain",
  :enable_starttls_auto => true
}

config/environments/

development.rb:

  # mailer
  config.active_support.deprecation = :log
  config.action_mailer.delivery_method = :smtp
  config.action_mailer.perform_deliveries = true
  config.action_mailer.default_url_options = { :host => "localhost:3000" }
  config.action_mailer.delivery_method = :smtp

  config.action_mailer.smtp_settings = {
   :address              => ENV['address'],
   :port                 => ENV['port'],
   :user_name            => ENV['gmail_username'],
   :password             => ENV['gmail_password'],
   :authentication       => ENV['authentication'],
   :enable_starttls_auto => true

Full error output from command line after submitting an email from live site:

2016-04-15T23:40:12.932560+00:00 app[web.1]: Sent mail to [email protected] (348.5ms)
2016-04-15T23:40:12.932577+00:00 app[web.1]: Date: Fri, 15 Apr 2016 23:40:12 +0000
2016-04-15T23:40:12.932579+00:00 app[web.1]: From: [email protected]
2016-04-15T23:40:12.932579+00:00 app[web.1]: To: [email protected]
2016-04-15T23:40:12.932581+00:00 app[web.1]: Message-ID: <57117bdc8f495_33f9c165af25c8233d@effd0ada-665b-4e24-ba0a-1968e9127267.mail>
2016-04-15T23:40:12.932583+00:00 app[web.1]: Subject: hi
2016-04-15T23:40:12.932583+00:00 app[web.1]: Mime-Version: 1.0
2016-04-15T23:40:12.932584+00:00 app[web.1]: Content-Type: text/plain;
2016-04-15T23:40:12.932585+00:00 app[web.1]:  charset=UTF-8
2016-04-15T23:40:12.932585+00:00 app[web.1]: Content-Transfer-Encoding: 7bit
2016-04-15T23:40:12.932586+00:00 app[web.1]: 
2016-04-15T23:40:12.932587+00:00 app[web.1]: hiiiii
2016-04-15T23:40:12.932687+00:00 app[web.1]: Completed 500 Internal Server Error in 367ms
2016-04-15T23:40:12.933562+00:00 app[web.1]: 
2016-04-15T23:40:12.933579+00:00 app[web.1]: Net::SMTPAuthenticationError (530-5.5.1 Authentication Required. Learn more at
2016-04-15T23:40:12.933580+00:00 app[web.1]: ):
2016-04-15T23:40:12.933581+00:00 app[web.1]:   app/controllers/contacts_controller.rb:12:in `create'
2016-04-15T23:40:12.933582+00:00 app[web.1]: 
2016-04-15T23:40:12.933583+00:00 app[web.1]: 
2
what exactly is happening when you try to send the mail? are you getting an error?sixty4bit
@sixty4bit I added the full error output^Jillian Hoenig
Is the port number correct?hypern
@hypern I'm not sure. I tried a few. How do I find that out?Jillian Hoenig

2 Answers

2
votes

try this

config.action_mailer.smtp_settings = {
  address:              'smtp.gmail.com',
  port:                 587,
  domain:               'gmail.com',
  user_name:            ENV['gmail_username'],
  password:             ENV['gmail_password'],
  authentication:       'plain',
  enable_starttls_auto: true
}
1
votes

Got it to work. Thanks you Zhiliang for your answer that got me closer. In the end it was my $RAILS_ENV file that was wrong. I deleted that from my environments folder and added smtp_settings.rb file to initializers folder. Here are the contents of initializers/smtp_settings.rb:

ActionMailer::Base.smtp_settings = {
    :address => "smtp.gmail.com",
    :port => 587,
    :domain => "gmail.com",
    :user_name => "<my_email_address>",
    :password => "<my_gmail_password>",
    :authentication => :plain,
    :enable_starttls_auto => true
}