Reputation: 829
What I want to do:
Send an email to a user confirming that they have successfully registered.
What I have tried to do:
I have followed this guide: http://guides.rubyonrails.org/action_mailer_basics.html
And so have ran the command: 'rails generate mailer UserMailer', and have added/edited the following files to look like this:
app/mailers/application_mailer.rb:
class ApplicationMailer < ActionMailer::Base
default sender: "[email protected]"
layout 'mailer'
end
app/mailers/user_mailer.rb:
class UserMailer < ApplicationMailer
default from: '[email protected]'
def welcome_email(user)
@user = user
@url = 'http://localhost:3000/users/login'
mail(to: @user.email, subject: 'Welcome to My Awesome Site')
end
end
app/views/user_mailer/welcome_email.html.erb:
<!DOCTYPE html>
<html>
<head>
<meta content='text/html; charset=UTF-8' http-equiv='Content-Type' />
</head>
<body>
<h1>Welcome to example.com, <%= @user.first_name %></h1>
<p>
You have successfully signed up to example.com,
your username is: <%= @user.first_name %><%= @user.last_name %>.<br>
</p>
<p>
To login to the site, just follow this link: <%= @url %>.
</p>
<p>Thanks for joining and have a great day!</p>
</body>
</html>
app/views/user_mailer/welcome_email.txt:
Welcome to example.com, <%= @user.first_name %>
===============================================
You have successfully signed up to example.com,
your username is: <%= @user.first_name %><%= @user.last_name %>.
To login to the site, just follow this link: <%= @url %>.
Thanks for joining and have a great day!
app/controller/users_controller.rb:
def create
@user = User.new(user_params)
if @user.save
# login is achieved by saving a user's 'id' in a session variable,
# accessible to all pages
session[:user_id] = @user.id
UserMailer.welcome_email(@user).deliver_later
redirect_to films_path
else
render action: "new"
end
end
Other Details:
Rails version: rails 4.2.0
Operating System: Windows 7
I am connecting to the rails server through localhost:3000 - it is running on my own machine.
I have tried sending emails to a gmail and a tiscali account but neither have worked.
Schema.rb:
create_table "users", force: :cascade do |t|
t.string "password_digest"
t.string "role"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "first_name"
t.string "last_name"
t.string "house_no"
t.string "street"
t.string "town"
t.string "postcode"
t.string "email"
t.date "date_of_birth"
end
Please note that I'm fairly new to Ruby. Can anyone please help?
Upvotes: 0
Views: 186
Reputation: 4116
class ApplicationMailer < ActionMailer::Base
default from: "[email protected]"
layout 'mailer'
end
I changed default sender to default from and during testing, I'd also change deliver_later to deliver_now, instead of queuing it, it happens right away
And change this app/views/user_mailer/welcome_email.txt to welcome_email.txt.erb
Also I'd setup gem letter opener, it's super easy to setup, it'll open emails on browser tabs and you'll get to see your emails, maybe that happens automatically in the version of rails you're using, but it's good to know.
Upvotes: 0