Spirit
Spirit

Reputation: 307

How to send Email in Rails for real

I want to send email from my app to Gmail with real account My code base on this website https://guides.rubyonrails.org/action_mailer_basics.html, but when I run source, the email was shown on terminal, but it was not sent to my Gmail account. What should I do to send the email to my Gmail account, so I can view it in https://mail.google.com/mail/u/0/#inbox?

Here is my code:

user_mailer.rb

class UserMailer < ApplicationMailer
  default from: '[email protected]'

  def sample_email
    mail to: '[email protected]', subject: 'Test Mail Rails'
  end
end

user_controller.rb

def index
    UserMailer.sample_email.deliver_now
end

production.rb

config.action_mailer.delivery_method = :smtp
# SMTP settings for gmail
config.action_mailer.smtp_settings = {
  :address              => "mail.google.com",
  :port                 => 587,
  :user_name            => '[email protected]',
  :password             => '********',
  :authentication       => "plain",
  :enable_starttls_auto => true
}

Upvotes: 0

Views: 628

Answers (2)

Thomas
Thomas

Reputation: 2952

Try turning this setting for your receiving email on:

https://myaccount.google.com/lesssecureapps

Upvotes: 0

Thomas
Thomas

Reputation: 2952

To add email functionality to a rails project:

First run

rails g mailer <mailername>

This will give you this (you can append function names but lets keep it simple)

  create  app/mailers/mailername_mailer.rb
  invoke  erb
  create    app/views/mailername_mailer
  invoke  test_unit
  create    test/mailers/mailername_mailer_test.rb
  create    test/mailers/previews/mailername_mailer_preview.rb

Next go to app/mailers and alter the default from:

class ApplicationMailer < ActionMailer::Base
  default from: '[email protected]'
  layout 'mailer'
end

This is going to tell your application that you're sending FROM this email (you will have to provide credentials later...)

Next head into your created mailers file app/mailers/mailername_mailer.rb NOTICE: It inherits from ApplicationMailer so you'll get the 'default from' that we declared above.

class MailernameMailer < ApplicationMailer
  def request(arg)
    @arg = arg
    mail(to: "[email protected]", subject: @arg)
  end
end

This mailer is essentially a class, but the instantiating works a bit differently. It creates an instance when you call it statically...like you would a model. The mail(to: x) is the account you're going to mail this to. You can use this in your controller. Like this:

ExampleController.rb

def index
     MailernameMailer.request("Hi I'm paul").deliver_later
end

For the view:

A cool feature for emails is the previews built into rails. The test action can be called in your mailername_mailer_preview.rb file found in test/mailers/previews.

Here is what it should look like:

def request(args)
    @name = args
    MailernameMailer.request(@name)
  end

Just like above in the controller, you're going to add the instantiation and call the function..then use that data in your view found at: app/views/mailername_mailer/request.html.erb

and preview it at:

http://localhost:3000/rails/mailers/mailername_mailer

Your mailer action is going to route to a view with the same name as the function automatically...

Now for the validation of the sender account... (Gmail needs a valid account in order to send an email). Change your production.rb file to look like this..although you should probably get this working on development before moving to production, considering you don't always have to have best practices until you push to production.

  config.action_mailer.delivery_method = :smtp
  config.action_mailer.smtp_settings = {
      address: "smtp.gmail.com",
      port: 587,
      domain: "gmail.com",
      user_name: ENV["username"],
      password: ENV["password"],
      authentication: "plain"
  }
  config.action_mailer.default_url_options = {host: domain_name}

domain_name should just be localhost:3000 for development.

Upvotes: 1

Related Questions