@ -27,6 +27,24 @@ class Mailer < ActionMailer::Base
{ :host = > Setting . host_name , :protocol = > Setting . protocol }
end
class MailerProxy
def initialize ( cls )
@target = cls
end
def method_missing ( name , * args , & block )
if Setting . delayjob_enabled && Object . const_defined? ( 'Delayed' )
@target . delay . send ( name , * args , & block )
else
@target . send ( name , * args , & block ) . deliver
end
end
end
def self . run
MailerProxy . new ( self )
end
# author: alan
# 发送邀请未注册用户加入项目邮件
# 功能: 在加入项目的同时自动注册用户
@ -45,6 +63,12 @@ class Mailer < ActionMailer::Base
def send_for_user_activities ( user , date_to , days )
date_from = date_to - days . days
subject = " [ #{ user . show_name } : #{ l ( :label_day_mail ) } ] "
@subject = " #{ user . show_name } : #{ date_to } #{ l ( :label_day_mail ) } "
date_from = " #{ date_from } 23:59:59 "
date_to = " #{ date_to } 23:59:59 "
# 生成token用于直接点击登录
@user = user
token = Token . new ( :user = > user , :action = > 'autologin' )
@ -59,10 +83,11 @@ class Mailer < ActionMailer::Base
course_ids = courses . map { | course | course . id } . join ( " , " )
# 查询user的缺陷, 包括发布的, 跟踪的以及被指派的缺陷
@issues = Issue . find_by_sql ( " select DISTINCT i.* from issues i, watchers w
sql = " select DISTINCT i.* from issues i, watchers w
where ( i . assigned_to_id = #{user.id} or i.author_id = #{user.id}
or ( w . watchable_type = 'Issue' and w . watchable_id = i . id and w . user_id = #{user.id}))
and ( i . created_on between '#{date_from}' and '#{date_to}' ) order by i . created_on desc " )
and ( i . created_on between '#{date_from}' and '#{date_to}' ) order by i . created_on desc "
@issues = Issue . find_by_sql ( sql )
# @bids 查询课程作业, 包括老师发布的作业, 以及user提交作业
# @attachments查询课程课件更新
@ -114,15 +139,15 @@ class Mailer < ActionMailer::Base
@forums = Forum . find_by_sql ( " select DISTINCT * from forums where creator_id = #{ user . id } and (created_at between ' #{ date_from } ' and ' #{ date_to } ') order by created_at desc " )
@memos = Memo . find_by_sql ( " select DISTINCT m.* from memos m, forums f where (m.author_id = #{ user . id } or (m.forum_id = f.id and f.creator_id = #{ user . id } ))
and ( m . created_at between '#{date_from}' and '#{date_to}' ) order by m . created_at desc " )
if days == 1
subject = " [ #{ user . show_name } : #{ l ( :label_day_mail ) } ] "
@subject = " #{ user . show_name } : #{ date_to - 1 . days } #{ l ( :label_day_mail ) } "
else
subject = " [ #{ user . show_name } : #{ l ( :label_week_mail ) } ] "
@subject = " #{ user . show_name } : #{ l ( :label_week_mail ) } "
end
mail :to = > user . mail , :subject = > subject
has_content = [ @issues , @homeworks , @course_messages , @project_messages , @course_news , @project_news ,
@course_journal_messages , @user_journal_messages , @forums , @memos , @attachments , @bids ] . any? { | o |
! o . empty?
}
binding . pry if Rails . env . development?
#有内容才发,没有不发
mail :to = > user . mail , :subject = > subject if has_content
end
# 公共讨论区发帖、回帖添加邮件发送信息
@ -135,17 +160,15 @@ class Mailer < ActionMailer::Base
@issue_author_url = url_for ( user_activities_url ( @author ) )
recipients || = [ ]
#将帖子创建者邮箱地址加入数组
if @forum . creator . mail_notification != 'day' && @forum . creator . mail_notification != 'week'
recipients << @forum . creator . mail
end
recipients << @forum . creator . mail
#回复人邮箱地址加入数组
if @author . mail_notification != 'day' && @author . mail_notification != 'week'
recipients << @author . mail
end
recipients << @author . mail
# cc = wiki_content.page.wiki.watcher_recipients - recipients
@memo_url = url_for ( forum_memo_url ( @forum , ( @memo . parent_id . nil? ? @memo : @memo . parent_id ) ) )
mail :to = > recipients , :subject = > " [ #{ l ( :label_message_plural ) } : #{ memo . subject } #{ l ( :label_memo_create_succ ) } ] "
mail :to = > recipients ,
:subject = > " [ #{ l ( :label_message_plural ) } : #{ memo . subject } #{ l ( :label_memo_create_succ ) } ] " ,
:filter = > true
end
# Builds a Mail::Message object used to email recipients of the added journals for message.
@ -180,12 +203,8 @@ class Mailer < ActionMailer::Base
# 验证用户的收取邮件的方式
recipients || = [ ]
recipients1 || = [ ]
if @mail . mail_notification != 'week' && @mail . mail_notification != 'day'
recipients1 = @mail . mail
end
if journals_for_message . jour . author . mail_notification != 'week' && journals_for_message . jour . author . mail_notification != 'day'
recipients = journals_for_message . jour . author . mail
end
recipients1 = @mail . mail
recipients = journals_for_message . jour . author . mail
# modify by nwb
#如果是直接留言并且留言对象是课程
@ -197,25 +216,24 @@ class Mailer < ActionMailer::Base
#收件人邮箱
@recipients || = [ ]
@members . each do | teacher |
if teacher . user . mail_notification != 'week' && teacher . user . mail_notification != 'day'
@recipients << teacher . user . mail
end
@recipients << teacher . user . mail
end
mail :to = > @recipients ,
:subject = > " #{ l ( :label_your_course ) } #{ journals_for_message . jour . name } #{ l ( :label_have_message ) } "
:subject = > " #{ l ( :label_your_course ) } #{ journals_for_message . jour . name } #{ l ( :label_have_message ) } " ,
:filter = > true
elsif journals_for_message . jour . class . to_s . to_sym == :Bid
if ! journals_for_message . jour . author . notify_about? journals_for_message
return - 1
end
mail :to = > recipients , :subject = > @title
mail :to = > recipients , :subject = > @title , :filter = > true
elsif journals_for_message . jour . class . to_s . to_sym == :Contest
if ! journals_for_message . jour . author . notify_about? journals_for_message
return - 1
end
mail :to = > recipients , :subject = > @title
mail :to = > recipients , :subject = > @title , :filter = > true
else
mail :to = > recipients1 , :subject = > @title
mail :to = > recipients1 , :subject = > @title , :filter = > true
end
@ -250,9 +268,9 @@ class Mailer < ActionMailer::Base
subject = " [ #{ issue . project . name } - #{ issue . tracker . name } # #{ issue_id } ] ( #{ issue . status . name } ) #{ issue . subject } "
mail ( :to = > recipients ,
: subject = > subject )
mail :to = > recipients ,
:subject = > subject ,
: filter = > true
end
# issue.attachments.each do |attach|
# attachments["#{attach.filename}"] = File.read("#{attach.disk_filename}")
@ -298,9 +316,9 @@ class Mailer < ActionMailer::Base
@issue = issue
@journal = journal
# @issue_url = url_for(:controller => 'issues', :action => 'show', :id => issue, :anchor => "change-#{journal.id}")
mail ( :to = > recipients ,
: subject = > s )
mail :to = > recipients ,
:subject = > s ,
: filter = > true
end
def self . deliver_mailer ( to , cc , subject )
@ -386,7 +404,8 @@ class Mailer < ActionMailer::Base
@issue_author_url = url_for ( user_activities_url ( @author ) )
@document_url = url_for ( :controller = > 'documents' , :action = > 'show' , :id = > document )
mail :to = > document . recipients ,
:subject = > " [ #{ document . project . name } ] #{ l ( :label_document_new ) } : #{ document . title } "
:subject = > " [ #{ document . project . name } ] #{ l ( :label_document_new ) } : #{ document . title } " ,
:filter = > true
end
# Builds a Mail::Message object used to email recipients of a project when an attachements are added.
@ -424,21 +443,24 @@ class Mailer < ActionMailer::Base
@added_to = added_to
@added_to_url = added_to_url
mail :to = > recipients ,
:subject = > " [ #{ container . name } ] #{ l ( :label_attachment_new ) } "
:subject = > " [ #{ container . name } ] #{ l ( :label_attachment_new ) } " ,
:filter = > true
elsif container . class . name == 'Project'
redmine_headers 'Project' = > container . id
@attachments = attachments
@added_to = added_to
@added_to_url = added_to_url
mail :to = > recipients ,
:subject = > " [ #{ container . name } ] #{ l ( :label_attachment_new ) } "
:subject = > " [ #{ container . name } ] #{ l ( :label_attachment_new ) } " ,
:filter = > true
else
redmine_headers 'Project' = > container . project . identifier
@attachments = attachments
@added_to = added_to
@added_to_url = added_to_url
mail :to = > recipients ,
:subject = > " [ #{ container . project . name } ] #{ l ( :label_attachment_new ) } "
:subject = > " [ #{ container . project . name } ] #{ l ( :label_attachment_new ) } " ,
:filter = > true
end
end
@ -457,7 +479,8 @@ class Mailer < ActionMailer::Base
@news = news
@news_url = url_for ( :controller = > 'news' , :action = > 'show' , :id = > news )
mail :to = > news . recipients ,
:subject = > " [ #{ news . project . name } ] #{ l ( :label_news ) } : #{ news . title } "
:subject = > " [ #{ news . project . name } ] #{ l ( :label_news ) } : #{ news . title } " ,
:filter = > true
elsif news . course
redmine_headers 'Course' = > news . course . id
@author = news . author
@ -467,7 +490,8 @@ class Mailer < ActionMailer::Base
recipients = news . course . notified_users . select { | user | user . allowed_to? ( :view_files , news . course ) } . collect { | u | u . mail }
@news_url = url_for ( :controller = > 'news' , :action = > 'show' , :id = > news )
mail :to = > recipients ,
:subject = > " [ #{ news . course . name } ] #{ l ( :label_news ) } : #{ news . title } "
:subject = > " [ #{ news . course . name } ] #{ l ( :label_news ) } : #{ news . title } " ,
:filter = > true
end
end
@ -488,7 +512,8 @@ class Mailer < ActionMailer::Base
@news_url = url_for ( :controller = > 'news' , :action = > 'show' , :id = > news )
mail :to = > news . recipients ,
:cc = > news . watcher_recipients ,
:subject = > " Re: [ #{ news . project . name } ] #{ l ( :label_news ) } : #{ news . title } "
:subject = > " Re: [ #{ news . project . name } ] #{ l ( :label_news ) } : #{ news . title } " ,
:filter = > true
elsif news . course
redmine_headers 'Course' = > news . course . id
@author = comment . author
@ -500,7 +525,8 @@ class Mailer < ActionMailer::Base
recipients = news . course . notified_users . select { | user | user . allowed_to? ( :view_files , news . course ) } . collect { | u | u . mail }
mail :to = > recipients ,
:subject = > " [ #{ news . course . name } ] #{ l ( :label_news ) } : #{ news . title } "
:subject = > " [ #{ news . course . name } ] #{ l ( :label_news ) } : #{ news . title } " ,
:filter = > true
end
end
@ -523,7 +549,8 @@ class Mailer < ActionMailer::Base
@message_url = url_for ( message . event_url )
mail :to = > recipients ,
:cc = > cc ,
:subject = > " [ #{ message . board . project . name } - #{ message . board . name } - msg #{ message . root . id } ] #{ message . subject } "
:subject = > " [ #{ message . board . project . name } - #{ message . board . name } - msg #{ message . root . id } ] #{ message . subject } " ,
:filter = > true
elsif message . course
redmine_headers 'Course' = > message . course . id ,
'Topic-Id' = > ( message . parent_id || message . id )
@ -537,7 +564,8 @@ class Mailer < ActionMailer::Base
@message_url = url_for ( message . event_url )
mail :to = > recipients ,
:cc = > cc ,
:subject = > " [ #{ message . board . course . name } - #{ message . board . name } - msg #{ message . root . id } ] #{ message . subject } "
:subject = > " [ #{ message . board . course . name } - #{ message . board . name } - msg #{ message . root . id } ] #{ message . subject } " ,
:filter = > true
end
end
@ -559,7 +587,8 @@ class Mailer < ActionMailer::Base
:id = > wiki_content . page . title )
mail :to = > recipients ,
:cc = > cc ,
:subject = > " [ #{ wiki_content . project . name } ] #{ l ( :mail_subject_wiki_content_added , :id = > wiki_content . page . pretty_title ) } "
:subject = > " [ #{ wiki_content . project . name } ] #{ l ( :mail_subject_wiki_content_added , :id = > wiki_content . page . pretty_title ) } " ,
:filter = > true
end
# Builds a Mail::Message object used to email the recipients of a project of the specified wiki content was updated.
@ -583,7 +612,8 @@ class Mailer < ActionMailer::Base
:version = > wiki_content . version )
mail :to = > recipients ,
:cc = > cc ,
:subject = > " [ #{ wiki_content . project . name } ] #{ l ( :mail_subject_wiki_content_updated , :id = > wiki_content . page . pretty_title ) } "
:subject = > " [ #{ wiki_content . project . name } ] #{ l ( :mail_subject_wiki_content_updated , :id = > wiki_content . page . pretty_title ) } " ,
:filter = > true
end
# Builds a Mail::Message object used to email the specified user their account information.
@ -734,6 +764,25 @@ class Mailer < ActionMailer::Base
ActionMailer :: Base . delivery_method = saved_method
end
#过滤掉不是不合规则的收件人
def filter ( reps )
r_reps = [ ]
if reps . is_a? Array
reps . each do | r |
u = User . find_by_mail ( r )
if u && u . mail_notification == 'all'
r_reps << r
end
end
elsif reps . is_a? String
u = User . find_by_mail ( r )
if u && u . mail_notification == 'all'
r_reps << r
end
end
r_reps
end
def mail ( headers = { } )
headers . merge! 'X-Mailer' = > 'Redmine' ,
'X-Redmine-Host' = > Setting . host_name ,
@ -750,6 +799,11 @@ class Mailer < ActionMailer::Base
headers [ :cc ] . delete ( @author . mail ) if headers [ :cc ] . is_a? ( Array )
end
if headers [ :filter ]
headers [ :to ] = filter ( headers [ :to ] )
headers [ :cc ] = filter ( headers [ :cc ] )
end
if @author && @author . logged?
redmine_headers 'Sender' = > @author . login
end