I can not send emails from my application in rails, the version of rails I use is 5.1.2 .
I attach pieces of code:
app / mailers / example_mailer.rb
class ExampleMailer < ApplicationMailer
default from: '[email protected]'
def sample_email(user)
@user = user
mail(to: @user.email, subject: 'Sample Email')
end
end
app / views / example_mailer / sample_email.html.erb
<!DOCTYPE html>
<html>
<head>
<meta content='text/html; charset=UTF-8' http-equiv='Content-Type' />
</head>
<body>
<h1>Hi <%= @user.name %></h1>
<p>
Sample mail sent using smtp.
</p>
</body>
</html>
test / mailers / previews / example_mailer_preview.rb
class ExampleMailerPreview < ActionMailer::Preview
def sample_mail_preview
ExampleMailer.sample_email(User.first)
end
end
/config/application.yml
gmail_username: '[email protected]' gmail_password: 'xxx'
/config/environments/production.rb
config.action_mailer.delivery_method = :smtp
# SMTP settings for gmail
config.action_mailer.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:user_name => ENV['gmail_username'],
:password => ENV['gmail_password'],
:authentication => "plain",
:enable_starttls_auto => true
}
app / controllers / users_controller.rb
def create
@user = User.new(user_params)
respond_to do |format|
if @user.save
# Sends email to user when user is created.
ExampleMailer.sample_email(@user).deliver
format.html { redirect_to @user, notice: 'User was successfully created.' }
format.json { render :show, status: :created, location: @user }
else
format.html { render :new }
format.json { render json: @user.errors, status: :unprocessable_entity }
end
end
end
I attach the logout
Started GET "/users/1/edit" for 127.0.0.1 at 2017-07-26 06:39:40 -0400
Processing by UsersController#edit as HTML
Parameters: {"id"=>"1"}
User Load (1.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 1]]
Rendering users/edit.html.erb within layouts/application
Rendered users/_form.html.erb (6.0ms)
Rendered users/edit.html.erb within layouts/application (103.3ms)
Completed 200 OK in 419ms (Views: 355.3ms | ActiveRecord: 1.0ms)
Started PATCH "/users/1" for 127.0.0.1 at 2017-07-26 06:39:45 -0400
Processing by UsersController#update as HTML
Parameters: {"utf8"=>"V", "authenticity_token"=>"7wdXKYpv+o+tNlmBBSdgIcOa0d07f6fUlFnJqZC1zbVyfLrhct57mLZQBd0EOP9Ckmsw9LoAlSfeMt2wjg1eLw==", "user"=>{"name"=>"rafael Guillermo R", "email"=>"[email protected]"}, "commit"=>"Update User", "id"=>"1"}
User Load (1.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 1]]
(0.0ms) begin transaction
SQL (2.0ms) UPDATE "users" SET "name" = ?, "updated_at" = ? WHERE "users"."id" = ? [["name", "rafael Guillermo R"], ["updated_at", "2017-07-26 10:39:45.599798"], ["id", 1]]
(137.4ms) commit transaction
Rendering example_mailer/sample_email.html.erb within layouts/mailer
Rendered example_mailer/sample_email.html.erb within layouts/mailer (0.0ms)
Rendering example_mailer/sample_email.text.erb within layouts/mailer
Rendered example_mailer/sample_email.text.erb within layouts/mailer (1.0ms)
ExampleMailer#sample_email: processed outbound mail in 364.2ms
Sent mail to [email protected] (2021.4ms)
Date: Wed, 26 Jul 2017 06:39:46 -0400
From: [email protected]
To: [email protected]
Message-ID: <[email protected]>
Subject: Sample Email
Mime-Version: 1.0
Content-Type: multipart/alternative;
boundary="--==_mimepart_597871721a39a_1d342c7440479395";
charset=UTF-8
Content-Transfer-Encoding: 7bit
----==_mimepart_597871721a39a_1d342c7440479395
Content-Type: text/plain;
charset=UTF-8
Content-Transfer-Encoding: 7bit
Hi rafael Guillermo R
Sample mail sent using smtp.
----==_mimepart_597871721a39a_1d342c7440479395
Content-Type: text/html;
charset=UTF-8
Content-Transfer-Encoding: 7bit
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style>
/* Email styles need to be inline */
</style>
</head>
<body>
<!DOCTYPE html>
<html>
<head>
<meta content='text/html; charset=UTF-8' http-equiv='Content-Type' />
</head>
<body>
<h1>Hi rafael Guillermo R</h1>
<p>
Sample mail sent using smtp.
</p>
</body>
</html>
</body>
</html>
----==_mimepart_597871721a39a_1d342c7440479395--
Redirected to http://localhost:3000/users/1
Completed 302 Found in 2552ms (ActiveRecord: 140.4ms)
Started GET "/users/1" for 127.0.0.1 at 2017-07-26 06:39:48 -0400
Processing by UsersController#show as HTML
Parameters: {"id"=>"1"}
User Load (1.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 1]]
Rendering users/show.html.erb within layouts/application
Rendered users/show.html.erb within layouts/application (3.0ms)
Completed 200 OK in 345ms (Views: 299.2ms | ActiveRecord: 1.0ms)