Creating and Sending Mail
1.CREATE A Notice MAILER
rails g mailer NoticeMailer decomp_change
create app/mailers/notice_mailer.rb
invoke erb
create app/views/notice_mailer
create app/views/notice_mailer/decomp_change.text.erb
app/mailers/notice_mailer.rb
class NoticeMailer < ActionMailer::Base
default from: "from@example.com"
def decomp_change
@greeting = "Hi"
mail to: "to@example.org"
end
end
Sending Attachments in Mail
class NoticeMailer < ActionMailer::Base
default from: "from@example.com"
def decomp_change (notice)
@notice = notice
@last_tweet = @notice.tweets.last
attachments['z.pdf'] = File.read("#{Rails.root}/public/notice.pdf")
mail to: @notice.email, subject: 'Your decomp stage has changed'
end
end
2.MAILER VIEWS
app/views/notice_mailer/decomp_change.text.erb
Greetings <%= @notice.name %>,
Your decomposition state is now <%= @notice.decomp %> and your
last tweet was: <%= @last_tweet.body %>
Good luck!
app/views/notice_mailer/decomp_change.html.erb
<h1>Greetings <%= @notice.name %>,</h1>
<p>Your decomposition state is now <%= @notice.decomp %> and your
last tweet was: <%= @last_tweet.body %></p>
<%= link_to "View yourself", notice_url(@notice) %>
3.SENDING MAIL
app/models/notice.rb
class Notice < ActiveRecord::Base
after_save :decomp_change_notification, if: :decomp_changed?
private
def decomp_change_notification
noticeMailer.decomp_change(self).deliver
end
end
4.config
config/environments/development.rb
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:user_name => 'poshboytl',
:password => ENV['GMAIL_PASS'],
:authentication => 'plain',
:enable_starttls_auto => true
}
reference:
http://railscasts-china.com/episodes/how-to-send-emails-in-rails
分享到:
相关推荐
Now in version 4, Rails is mature and powerful, and to use it effectively you need more than a few Google searches. You'll find no substitute for the guru's-eye-view of design, testing, deployment, ...
Rails 3 in Action 2011年9月新鲜出炉,针对最新的Rails 3.1进行说明
What’s New In Rails 2.1: A Well Produced, Free E-Book
Rails 4 in Action, Second Edition.pdf
Rubyisms in Rails
Detection of critical defects in rails using ultrasonic surface wave
8 Setting up a Todo application in Rails 8 Analyzing folder structure of a Rails application 10 Creating views 12 Styling views using CSS 14 Redesigning the Todo application 16 Challenges in styling ...
Scrum Poker in Rails5, docker-compose
WeChat in Rails 的 API、命令和消息处理
唔,1分应该还是有人下的吧,共同学习进步,Ruby on Rails is an open source web framework.... "Rails 4 in Action" is a fully-revised second edition of "Rails 3 in Action." This hands-on, compreh...
kanban, Kanban 看板 is a Trello clone in Rails and Backbone.js.zip
roadie-rails, 使HTML电子邮件适合 Rails rockstars roadie 使HTML电子邮件适合 Rails rockstars 。这里 gem 将 Rails 应用程序与Roadie钩子,以帮助你生成HTML电子邮件。安装将这个 gem 添加到你的Gemfile,
We still start with a step-by-step walkthrough of building a real application, and in-depth chapters look at the built-in Rails features. This edition now gives new Ruby and Rails users more ...
rails 2.3.2离线安装rails 2.3.2离线安装rails 2.3.2离线安装rails 2.3.2离线安装rails 2.3.2离线安装rails 2.3.2离线安装rails 2.3.2离线安装rails 2.3.2离线安装rails 2.3.2离线安装rails 2.3.2离线安装rails ...
Bootstrap is a free, open source CSS and JS framework that helps you create websites in minutes, and Ruby on Rails, or Rails as it's known, is an open source web application framework written in the ...
[Pragmatic Bookshelf] Crafting Rails Applications Expert Practices for Everyday Rails Development (E-Book) ☆ 图书概要:☆ Rails 3 is a huge step forward. You can now easily extend the framework, ...
The more confident reader might be able to get started in Rails using just this book, extensive online resources, and his wits, but there are other publications that are more introductory in nature ...