Joe Morano
Joe Morano

Reputation: 1875

How to send automatic emails with the click of a button with Rails?

Is there a way within Rails to send an automatic email when a form is submitted, containing information from that form? Specifically, I would like for a new user to recieve an email containing his new profile url when he submits the signup form.

Here's what my form looks like:

<h1>Add Something!</h1>
<p>
  <%= form_for @thing, :url => things_path, :html => { :multipart => true } do |f| %>

    <%= f.text_field :name, :placeholder => "Name of the thing" %>
    <%= f.label :display_picture %>
    <%= f.file_field :avatar %>
    <br>
    <%= f.submit "Submit", class: "btn btn-primary" %>
  <% end %>
</p>

Controller:

class ThingsController < ApplicationController

  def show
    @thing = Thing.find(params[:id])
  end

  def new
    @thing = Thing.new
    @things = Thing.all
  end

  def create
    @thing = Thing.new(thing_params)
    if @thing.save
      render :action => "crop"     
    else
      flash[:notice] = "Failed"
      redirect_to new_things_path
    end
  end

  private

    def thing_params
      params.require(:thing).permit(:name, :avatar)
    end

end

Thanks in advance!

Upvotes: 0

Views: 2265

Answers (2)

mohameddiaa27
mohameddiaa27

Reputation: 3597

Lets assume that we need to notify the user on create action.

  1. First off you must generate your mailer, which uses the Rails generate command:

rails generate mailer user_mailer

  1. The next thing to do would be to setup an SMTP transport for your mailer. in the config/environments/development.rb file, add the following configuration:

This is for gmail, (place your domain, username and password):

config.action_mailer.delivery_method = :smtp 
config.action_mailer.smtp_settings = {   
  address: 'smtp.gmail.com',   
  port: 587,   
  domain: 'example.com',   
  user_name: '<username>',   
  password:  '<password>',   
  authentication: 'plain',   
  enable_starttls_auto: true  
}
  1. Then we’d need to tell Rails the specifics the email you want to send, like who to send it to, where does it come from, as well as the actual Subject and Content. We do that by creating a method in the recently created mailer,

which we’ll name notify

class UserMailer < ActionMailer::Base
  default from: '[email protected]'

  def notify(user)
    @user = user
    mail(to: @user.email,subject: "Notification")
  end
end

4.Create a file called notify.html.erb in app/views/user_mailer/. This will be the template used for the email, formatted in html.erb:

Here is the content you may send:

<html>
  <head>
    <meta content='text/html; charset=UTF-8' http-equiv='Content-Type' />
  </head>
  <body>
    <h1>Welcome to example.com, <%= @user.name %></h1>
    <p>
      You have received a notification
    </p>
    <p>Thanks for joining and have a great day!</p>
  </body>
</html>
  1. Finally you can send the mail in your create action by:

delivery method:

UserMailer.notify(@user).deliver

Note that you can add more attributes to send more objects to the method notify

Read more about Mailers from this link

Upvotes: 6

Dbz
Dbz

Reputation: 2761

Check out the ActionMailer::Base class and also this cool gem called Letter Opener which will help you while testing because it opens sent emails in the browser.

Upvotes: 1

Related Questions