How to configure action mailer (should I register domain)?

23,050

Solution 1

The configuration of your mailer should/can be defined in both development and production the purpose of this configuration is that when you set this up when you use the actionmailer these SMTP options will be used. You could have a simple mailer like the following:

Mailer

class UserMailer < ActionMailer::Base
  default :from => DEFAULT_FROM
  def registration_confirmation(user)
    @user = user
    @url = "http://portal.herokuapp.com/login"
    mail(:to => user.email, :subject => "Registered")

  end
end

Controller

 def create
    @title = 'Create a user'
    @user = User.new(params[:user])

    if @user.save
      UserMailer.registration_confirmation(@user).deliver
      redirect_to usermanagement_path
      flash[:success] = 'Created successfully.'
    else
      @title = 'Create a user'
      render 'new'
    end
  end

So what happens here is that when the create action is being used this fires the mailer UserMailer Looking at the above UserMailer it uses the ActionMailer as the base. Following the SMTP setup shown below which can be defined in both config/environments/production.rb and development.rb

You would have the following:

  config.action_mailer.default_url_options = { :host => 'portal.herokuapp.com' }
  config.action_mailer.delivery_method = :smtp
  config.action_mailer.smtp_settings = {
      :address              => 'smtp.gmail.com',
      :port                 => 587,
      :domain               => 'gmail.com',
      :user_name            => '[email protected]',
      :password             => 'pass',
      :authentication       => 'login',
      :enable_starttls_auto => true
  }

If you want to define the SMTP settings in development mode you would replace

config.action_mailer.default_url_options = { :host => 'portal.herokuapp.com' }

with

config.action_mailer.default_url_options = { :host => 'IP ADDRESS HERE:3000' }

This should be a thorough enough explanation to kick start you in the right direction.

Solution 2

The above answer worked for me in development once I changed it to

authentication: 'plain' 

and included

config.action_mailer.raise_delivery_errors = true 

in my development environment.

Share:
23,050
gotqn
Author by

gotqn

Free Tibet From China Domination http://freetibet.org/about/10-facts-about-tibet

Updated on July 09, 2022

Comments

  • gotqn
    gotqn almost 2 years

    I am creating a simple non-profit application with Ruby on Rails. I have to set up the following settings in order to be able to send emails with Gmail:

    Depot::Application.configure do
    
    config.action_mailer.delivery_method = :smtp
    
    config.action_mailer.smtp_settings = {
        address:"smtp.gmail.com",
        port:587,
        domain:"domain.of.sender.net",
        authentication: "plain",
        user_name:"dave",
        password:"secret",
        enable_starttls_auto: true
    }
    
    end
    

    I am completely new with this stuff and have no idea what exactly I should do.

    1. How to populate the settings above if I have gmail account? Do I need to buy a domain and can be it bought from google in order to use the settings above?
    2. Is it better to set up mail server on my PC? I looked though this tutorial but as far as I understand I still need to buy a domain.

    Also, as it is said here:

    Setting up an email server is a difficult process involving a number of different programs, each of which needs to be properly configured.

    because of this and my poor skills I am looking for the simplest solution.

    I have read the rails action mailer tutorial and have an idea about what these parameters are used for, but the things around the Gmail and the mail server are not clear at all.