https://github.com/dikshabittern/PostApplication
How To Use Mailer Function
1=> Use the following command to generate a mailer as follows −
rails g mailer UserMailer
2=> This will create a file user_mailer.rb in the app\mailer directory:-
(app/mailer/user_mailer.rb)
3=> Let's create one method as follows :-
def welcome_email(user)
@user = user
@url = 'http://www.gmail.com'
mail(to: @user.email, subject: 'Welcome to My Awesome Site')
end
4=> Create a file called welcome_email.html.erb in app/views/user_mailer/.
<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 successfully signed up to example.com,your username is:
<%= @user.login %>.<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>
5=> add_email smtp setting:- config/environments/development
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = true
config.action_mailer.default_options = {from: 'localhost:3000'}
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: 'smtp.gmail.com',
port: 587,
domain: 'localhost',
user_name: 'testing.bittern@gmail.com',
password: 'bittern12345',
authentication: 'plain',
enable_starttls_auto: true }
6=>
send_email:-
UserMailer.welcome_email(User.first).deliver_laterUse Friendship_Mailer in Post Application Project
1=> Use the following command to generate a mailer as follows −
rails g mailer FriendshipMailer
2=> This will create a file friendship_mailer.rb in the app\mailer directory:-
(app/mailer/friendship_mailer.rb) and add this code
def send_request(friendship)
@sender = User.find(friendship.sender_id)
@receiver = User.find(friendship.receiver_id)
mail(to: @receiver.email, subject: 'New Friend Request')
end
3=> Then, call and create a method in friendship.rb model after create a send request method in friendship_controller.rb and add this code in friendship.rb model.
after_create :send_request_mail
def send_request_mail
FriendshipMailer.send_request(self).deliver
end
4=> Next, create a view send_request.html.rb in friendship_mailer folder(friendship_mailer/send_request.html.erb)and add
<h2>Hi <%= @receiver.name %>,</h2>
<p><%= @sender.name %> send you a new friend request.</p>
5=> same as follow these steps for accepted and rejected view.
Modify (app/mailer/friendship_mailer.rb) and add this code
def accept_request(friendship)
@sender = User.find(friendship.sender_id)
@receiver = User.find(friendship.receiver_id)
mail(to: @sender.email, subject: ' Request Accepted')
end
def reject_request(friendship)
@sender = User.find(friendship.sender_id)
@receiver = User.find(friendship.receiver_id)
mail(to: @sender.email, subject: ' Request Rejected')
end
6=> create a accepted_request.html.erb and add this code..
<h2>Hi <%= @sender.name %>,</h2>
<p><%= @receiver.name %> has accepted your friend request.</p>
7=> create a rejected request.html.erb and add this code..
<h2>Hi <%= @sender.name %>,</h2>
<p><%= @receiver.name %> has Rejected your friend request.</p>
No comments:
Post a Comment