Action Mailer Basics
1. Create the Mailer
rails generate mailer UserMailer
2. Create User:-
(app/mailers/user_mailer.rb)
class
UserMailer < ApplicationMailer
default from:
'notifications@example.com'
def
welcome_email
@user
=
user
@url
=
'http://example.com/login'
mail(to:
@user
.email, subject:
'Welcome to My Awesome Site'
)
end
end
3. Create View:- (app/views/user_mailer/welcome_email.html.erb)
<html>
<head>
<meta content='text/html; charset=UTF-8' http-equiv='Content-Type' />
</head>
<body>
<h1>Welcome to-<%= @user.name %></h1>
<p>
You have successfully signed up to gmail.com,
your username is: <%= @user.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>
<head>
<meta content='text/html; charset=UTF-8' http-equiv='Content-Type' />
</head>
<body>
<h1>Welcome to-<%= @user.name %></h1>
<p>
You have successfully signed up to gmail.com,
your username is: <%= @user.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>
4. Add email SMTP settings:- (config/environments/development.rb
config.active_record.dump_schema_after_migration = false
config.action_mailer.default_url_options = { host: 'localhost:3000'}
ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.perform_deliveries = true
ActionMailer::Base.raise_delivery_errors = true
config.action_mailer.smtp_settings = {
address: "smtp.sendgrid.net",
port: 587,
domain: "localhost",
authentication: "plain",
enable_starttls_auto: true,
user_name: "testing.bitterntec",
:password => "bittern1234"
}
config.action_mailer.raise_delivery_errors = true
config.action_mailer.default_url_options = { host: 'localhost:3000'}
ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.perform_deliveries = true
ActionMailer::Base.raise_delivery_errors = true
config.action_mailer.smtp_settings = {
address: "smtp.sendgrid.net",
port: 587,
domain: "localhost",
authentication: "plain",
enable_starttls_auto: true,
user_name: "testing.bitterntec",
:password => "bittern1234"
}
config.action_mailer.raise_delivery_errors = true
5. Send email:- UserMailer.welcome_email(@user).deliver
No comments:
Post a Comment