重构邮件推送, 不使用 API, 使用 Rails4 的 preview email

This commit is contained in:
yafeilee 2014-05-25 00:54:52 +08:00
parent d9255068b7
commit 023a135480
19 changed files with 125 additions and 132 deletions

View File

@ -0,0 +1,15 @@
class CommentMailer < ActionMailer::Base
domain = ENV['DOMAIN_NAME'] || 'example.com'
default from: "no-reply@#{domain}"
def new(comment_id, to)
@comment = Comment.find(comment_id)
mail to: to, subject: '博主, 你的博客有新的评论'
end
def reply(comment_id, to)
@comment = Comment.find(comment_id)
mail to: to, subject: '客官, 你评论过的博客有新的回复'
end
end

View File

@ -0,0 +1,10 @@
class PostMailer < ActionMailer::Base
domain = ENV['DOMAIN_NAME'] || 'example.com'
default from: "no-reply@#{domain}"
def new(post_id, to)
@post = Post.find(post_id)
mail to: to, subject: '客官, 新博客来了'
end
end

View File

@ -14,21 +14,18 @@ class Comment
validates_presence_of :post_id
def reply_emails
Comment.where(post_id: self.post_id).where(:id.ne => self.id).collect(&:email).uniq
Comment.where(post_id: self.post_id).collect(&:email).uniq - [ self.email ]
end
after_create do
if ENV['SENDCLOUD_USER'].present? && ENV['ADMIN_USER'].present? && ENV['ADMIN_USER'] =~ /@/
if ENV['MAIL_SERVER'].present? && ENV['ADMIN_USER'].present? && ENV['ADMIN_USER'] =~ /@/
Rails.logger.info 'comment created, comment worker start'
NewCommentWorker.perform_async(self.name, self.content, self.post.title, ENV['ADMIN_USER'])
NewCommentWorker.perform_async(self.id.to_s, ENV['ADMIN_USER'])
end
if ENV['SENDCLOUD_USER'].present?
if ENV['MAIL_SERVER'].present?
Rails.logger.info 'comment created, reply worker start'
reply_emails.each do |comment|
next if Subscribe.unsubscribe?(email)
NewReplyPostWorker.perform_async(self.name, self.post.title, self.content, self.post.id.to_s, email)
end
NewReplyPostWorker.perform_async(self.id.to_s)
end
end
end

View File

@ -23,10 +23,8 @@ class Post
validates :type, :presence=>true, :inclusion => { :in => [ TECH, LIFE, CREATOR ] }
after_create do
if ENV['SENDCLOUD_USER'].present?
Subscribe.subscribe_list.each do |email|
NewPostWorker.perform_async(self.title, email)
end
if ENV['MAIL_SERVER'].present?
NewPostWorker.perform_async(self.id.to_s)
end
end

View File

@ -0,0 +1,12 @@
p
| 你的博客有新的评论:
p
span 来自:
= mail_to @comment.email, @comment.name
p 评论内容:
pre
| #{@comment.content}
p
| 博客链接:
= link_to @comment.post.title, blog_url(@comment.post)

View File

@ -0,0 +1,9 @@
你的博客有新的评论:
来自: <%= @comment.name %> <<%= @comment.email %>>
评论内容: <%= @comment.content %>
博客标题: <%= @comment.post.title %>
博客链接: <%= blog_url(@comment.post) %>

View File

@ -0,0 +1,11 @@
p 你评论过的博客有新的回复:
p 评论人: #{@comment.name}
p 评论内容:
pre #{@comment.content}
p
| 被评论博客:
= link_to @comment.post.title, blog_url(@comment.post)

View File

@ -0,0 +1,9 @@
你评论过的博客有新的回复:
评论人: <%= @comment.name %>
评论内容: <%= @comment.content %>
被评论博客: <%= @comment.post.title %>
访问这里回复: <%= blog_url(@comment.post) %>

View File

@ -0,0 +1,3 @@
p
| 博客链接:
= link_to @post.title, blog_url(@post)

View File

@ -0,0 +1,3 @@
博客标题: <%= @post.title %>
博客链接: <%= blog_url(@post) %>

View File

@ -5,40 +5,9 @@ class NewCommentWorker
3
end
def perform(name, content, title, to)
logger.info "[mail] new comment mail: name=#{name}, content=#{content}, title=#{title}, to=#{to}"
response = send_mail(name, content, title, to)
logger.info "[mail] result is #{response}"
ensure
logger.info "[mail] new comment mail end: name=#{name}, content=#{content}, title=#{title}, to=#{to}"
end
def send_mail(name, content, title, to)
response = RestClient.post "https://sendcloud.sohu.com/webapi/mail.send.xml",
{
:api_user => "postmaster@#{ENV['SENDCLOUD_USER']}.sendcloud.org",
:api_key => ENV['SENDCLOUD_PASSWORD'],
:from => ENV['SENDCLOUD_FROM'],
:fromname => ENV['SENDCLOUD_FROMNAME'],
:to => to,
:subject => "你的博客又有新的评论",
:html => <<-EOF
<p>, :</p>
<p>: #{name}</p>
<p>: #{content[0..15]}...</p>
<p>: #{title}</p>
<p>&nbsp;</p>
<p>---------- 退:</p>
<p><a href="http://yafeilee.me/unsubscribe?id=5F46EF">退</a></p>
EOF
}
return response
def perform(comment_id, to)
logger.info "new comment mail: #{comment_id}"
CommentMailer.new(comment_id.to_s, to).deliver
end
end

View File

@ -5,38 +5,11 @@ class NewPostWorker
3
end
def perform(title, to)
logger.info "[mail] new post mail: title=#{title}, to=#{to}"
response = send_mail(title, to)
logger.info "[mail] result is #{response}"
ensure
logger.info "[mail] new post mail end: title=#{title}, to=#{to}"
end
def send_mail(title, to)
response = RestClient.post "https://sendcloud.sohu.com/webapi/mail.send.xml",
{
:api_user => "postmaster@#{ENV['SENDCLOUD_USER']}.sendcloud.org",
:api_key => ENV['SENDCLOUD_PASSWORD'],
:from => ENV['SENDCLOUD_FROM'],
:fromname => ENV['SENDCLOUD_FROMNAME'],
:to => to,
:subject => "#{ENV['SITE_NAME']} 又写了新博客",
:html => <<-EOF
<p>hi, , :</p>
<p>#{ENV['SITE_NAME']} 新博客到了: #{title}</p>
<p>访: http://yafeilee.me</p>
<p>&nbsp;</p>
<p>---------- 退:</p>
<p><a href="http://yafeilee.me/unsubscribe?id=5F46EF">退</a></p>
EOF
}
return response
def perform(post_id)
Subscribe.subscribe_list.each do |email|
Rails.logger.info "new post #{post_id}, send to #{email}"
PostMailer.new(post_id.to_s, email).deliver
end
end
end

View File

@ -5,47 +5,14 @@ class NewReplyPostWorker
3
end
# name: 评论人名字
# title: 博客标题
# content: 回复内容
# id: 博客id
# to: 邮件递送人
def perform(name, title, content, id, to)
logger.info "[mail] new reply mail: title=#{title}, content=#{content}, to=#{to}"
response = send_mail(name, title, content, id, to)
logger.info "[mail] result is #{response}"
ensure
logger.info "[mail] new reply mail end: title=#{title}, content=#{content}, to=#{to}"
end
def send_mail(name, title, content, id, to)
response = RestClient.post "https://sendcloud.sohu.com/webapi/mail.send.xml",
{
:api_user => "postmaster@#{ENV['SENDCLOUD_USER']}.sendcloud.org",
:api_key => ENV['SENDCLOUD_PASSWORD'],
:from => ENV['SENDCLOUD_FROM'],
:fromname => ENV['SENDCLOUD_FROMNAME'],
:to => to,
:subject => "博客回复通知",
:html => <<-EOF
<p>#{name}, 你好:</p>
<p>:</p>
<p>: #{title}</p>
<p>: #{content[0..20]}...</p>
<p>&nbsp;</p>
<p>: <a href="http://yafeilee.me/blogs/#{id}">#{title}</a></p>
<p>---------- 退:</p>
<p><a href="http://yafeilee.me/unsubscribe?id=5F46EF">退</a></p>
EOF
}
return response
def perform(comment_id)
logger.info "new reply mail"
comment = Comment.find(comment_id.to_s)
comment.reply_emails.each do |email|
next if email == ENV['ADMIN_USER']
logger.info "new reply mail to #{email}"
CommentMailer.reply(comment_id.to_s, email).deliver
end
end
end

View File

@ -23,5 +23,8 @@ module WBlog
config.generators do |g|
g.test_framework :rspec, view_specs: false
end
# action mailer
config.action_mailer.preview_path = "#{Rails.root}/spec/mailers/previews"
end
end

View File

@ -16,10 +16,8 @@ INTRODUCE: '这是李亚飞的博客, 李亚飞是暂住在深圳的一名 Ruby
# google analytics, 如果不需要可以留空
GOOGLE: 'UA-32883596-1'
# SENDCLOUD 邮件订阅功能, 留空用户名表示不需要创建博客后发送订阅邮件
# 更多参数细节请查阅: /app/workers/new_post_worker.rb
SENDCLOUD_USER: ''
SENDCLOUD_PASSWORD: ''
SENDCLOUD_FROM: ''
SENDCLOUD_FROMNAME: ''
SENDCLOUD_SITE_ADDRESS: ''
# 邮件订阅通知与评论邮件通知功能, 留空用户名表示不需要创建博客后发送订阅邮件
MAIL_SERVER: ''
DOMAIN_NAME: ''
MAIL_USERNAME: ''
MAIL_PASSWORD: ''

View File

@ -27,12 +27,12 @@ WBlog::Application.configure do
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
config.action_mailer.smtp_settings = {
address: "smtp.sendgrid.net",
address: ENV["MAIL_SERVER"],
port: 25,
domain: ENV["DOMAIN_NAME"],
authentication: "plain",
user_name: ENV["SENDGRID_USERNAME"],
password: ENV["SENDGRID_PASSWORD"]
user_name: ENV["MAIL_USERNAME"],
password: ENV["MAIL_PASSWORD"]
}
# Send email in development mode.
config.action_mailer.perform_deliveries = true

View File

@ -72,13 +72,15 @@ WBlog::Application.configure do
# Send deprecation notices to registered listeners.
config.active_support.deprecation = :notify
config.action_mailer.default_url_options = { :host => ENV["DOMAIN_NAME"] }
config.action_mailer.smtp_settings = {
address: "smtp.sendgrid.net",
address: ENV["MAIL_SERVER"],
port: 25,
domain: ENV["DOMAIN_NAME"],
authentication: "plain",
user_name: ENV["SENDGRID_USERNAME"],
password: ENV["SENDGRID_PASSWORD"]
user_name: ENV["MAIL_USERNAME"],
password: ENV["MAIL_PASSWORD"]
}

View File

@ -0,0 +1,9 @@
class CommentMailerPreview < ActionMailer::Preview
def new
CommentMailer.new(Comment.first, 'test@example.org')
end
def reply
CommentMailer.reply(Comment.first, 'test@example.org')
end
end

View File

@ -0,0 +1,5 @@
class PostMailerPreview < ActionMailer::Preview
def new
PostMailer.new(Post.first, 'to@example.org')
end
end