How to Generate Email View and Send It Out Within Rails Application
- If we want to send email within rails app, we could use generator for creating mailer class and view.
1
| $ rails g mailer NewsMailer # NewsMailer is the name of mailer class
|
- After that, we could get generated files under app folder.
1
2
3
4
5
6
| create app/mailers/news_mailer.rb
create app/mailers/application_mailer.rb
invoke erb
create app/views/news_mailer
create app/views/layouts/mailer.text.erb
create app/views/layouts/mailer.html.erb
|
- Next, we need to edit mailer class (news_mailer.rb contains empty mailer so we need to edit it.)
1
2
3
4
5
6
7
8
9
| class NewsMailer < ApplicationMailer
default from: 'news@demo.com'
def news_email(user, news)
@user = user
@news = news
mail(to: @user.email, subject: @news.title)
end
end
|
- create the view news_email.html.erb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
| <!DOCTYPE html>
<html>
<head>
<meta content='text/html; charset=UTF-8' http-equiv='Content-Type' />
</head>
<body>
<h1>hi, <%= @user.name %></h1>
<hr>
<h2><%= @news.title %></h2>
<p>
<%= @news.content.html_safe %>
</p>
</body>
</html>
|
- create send_news action in your controller.
1
2
3
4
5
6
7
| #...
def send_news
@users = User.all
@news = News.find(1)
NewsMailer.news_email(@users,@news).deliver
end
#...
|
- Finally, do not forget to add config info to your config/enviroments/$RAILS_ENV.rb file. (here we use gmail)
1
2
3
4
5
6
7
8
9
| config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: 'smtp.gmail.com',
port: 587,
domain: 'demo.com',
user_name: '<username>',
password: '<password>',
authentication: 'plain',
enable_starttls_auto: true }
|