In my Ruby on Rails application I am trying to send a user a confirmation email when they register on the site. I have followed this link: http://guides.rubyonrails.org/action_mailer_basics.html.
And have created the following files:
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.erb:
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
I have tried edited this code many times, including:
application_mailer.rb:
class ApplicationMailer < ActionMailer::Base
# default sender: "[email protected]"
default from: "[email protected]"
layout 'mailer'
end
users_controller.rb:
UserMailer.welcome_email(@user).deliver_now
And have also tried other things. The most interesting thing is that when I change the user_mailer.rb to:
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')
mail(to: '[email protected]', subject: 'Welcome to My Awesome Site')
end
end
Which has the email address hardcoded in, still nothing works. I am getting no error messages and a user can still register but no emails are being received.
I am using the following:
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
Can someone please help? Is it because I am trying to run an email system on my own machine through localhost:3000 without using a server? Is it something to do with SMTP?
Has anyone got any ideas?
config/environments/development.rb
do you haveconfig.action_mailer.raise_delivery_errors = true
andconfig.action_mailer.perform_deliveries = true
? and can you post yourconfig.action_mailer.smtp_settings
setup? – basiam