简体   繁体   中英

Sending mail with Rails 4 in development environment

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?

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  
}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM