1

I'm new in Rails. I'm trying to make a email notification about new messages of contact form using ActionMailer. I followed different tutorials, tried to apply different advices, but it didnt help. My setup_mail.rb file is:

config.action_mailer.delivery_method = :sendmail

   ActionMailer::Base.smtp_settings = {
   address:              'smtp.gmail.com',
   port:                 587,
   domain:               'gmail.com',
   user_name:            'my_user_name',
   password:             'my_password',
   authentication:       'plain',
   enable_starttls_auto: true  
}

I tried tp add this code to the development.rb file, my development.rb also has next code:

  config.action_mailer.raise_delivery_errors = true
  config.action_mailer.perform_deliveries = true

My contact_forms_controllers.rb has next code:

 def create
    @contact_form = ContactForm.new contact_form_params
    @contact_form.save
    redirect_to root_path
    AdminMailer.notification(@contact_form).deliver
 end

My admin_mailer.rb file is:

class AdminMailer < ApplicationMailer
    default from: "my_address@gmail.com"

    def notification(contact_form)
        mail(to: "my_address@gmail.com", subject: 'New message on your web-site')
    end
end

Everywhere my_address@gmail.com is my actual mail and my_password is my actual password.

I can create a message by help contact form without any errors. Has anybody idea, where can be a problem?

verrom
  • 419
  • 6
  • 18

2 Answers2

1

You are almost there but your development.rb should look like this :

config.action_mailer.perform_deliveries = true
config.action_mailer.delivery_method = :smtp

config.action_mailer.smtp_settings = {
  address:              'smtp.gmail.com',
  port:                 '587',
  domain:               'gmail.com',
  user_name:            'xxx@gmail.com',
  password:             'xxxxxx',
  authentication:       'login',
  enable_starttls_auto: true  
}
Prashant4224
  • 1,551
  • 1
  • 14
  • 21
  • If to change sendmail to smtp, occure next error: `getaddrinfo: Name or service not known`. If try to send a message with sendmail, console says that email was sent. But I do not receive it om gmail.. – verrom Nov 23 '15 at 10:53
  • @verrom : here the `user_name` and `password` that you will enter should be a valid gmail account.It should exists otherwise it would throw an error like in your case. – huzefa biyawarwala Nov 23 '15 at 11:01
  • It's a better practise to use something like a mailcatcher gem locally rather than real email service. Much better developer flow and happiness. – Troy SK Nov 23 '15 at 12:33
  • 1
    Thanks, thats helped! Perhaps it will help anybody: `error getaddrinfo: Name or service not known` was because of my internet connection. My computer is in the network with proxy server. When I tried to check mailer from another network without proxy, it works. – verrom Nov 25 '15 at 01:31
  • Glad that it helped ... :) – huzefa biyawarwala Nov 25 '15 at 02:56
0

Have you added this in development.rb:-

config.action_mailer.default_url_options = { :host => 'localhost:3000' }

And change sendmail to smtp:-

config.action_mailer.delivery_method = :smtp
user3506853
  • 814
  • 5
  • 3