2013-06-11 03:33:37 +08:00
|
|
|
#
|
|
|
|
# A helper class to send an email. It will also handle a nil message, which it considers
|
|
|
|
# to be "do nothing". This is because some Mailers will decide not to do work for some
|
|
|
|
# reason. For example, emailing a user too frequently. A nil to address is also considered
|
|
|
|
# "do nothing"
|
|
|
|
#
|
|
|
|
# It also adds an HTML part for the plain text body
|
|
|
|
#
|
|
|
|
require_dependency 'email/renderer'
|
2013-07-03 02:13:46 +08:00
|
|
|
require 'uri'
|
2013-06-11 03:33:37 +08:00
|
|
|
|
|
|
|
module Email
|
|
|
|
class Sender
|
|
|
|
|
|
|
|
def initialize(message, email_type, user=nil)
|
|
|
|
@message = message
|
|
|
|
@email_type = email_type
|
|
|
|
@user = user
|
|
|
|
end
|
|
|
|
|
|
|
|
def send
|
|
|
|
return if @message.blank?
|
|
|
|
return if @message.to.blank?
|
|
|
|
return if @message.body.blank?
|
|
|
|
|
|
|
|
@message.charset = 'UTF-8'
|
|
|
|
|
|
|
|
opts = {}
|
|
|
|
|
|
|
|
# Only use the html template on digest emails
|
|
|
|
opts[:html_template] = true if (@email_type == 'digest')
|
|
|
|
|
|
|
|
renderer = Email::Renderer.new(@message, opts)
|
|
|
|
|
|
|
|
@message.html_part = Mail::Part.new do
|
|
|
|
content_type 'text/html; charset=UTF-8'
|
|
|
|
body renderer.html
|
|
|
|
end
|
|
|
|
|
|
|
|
@message.text_part.content_type = 'text/plain; charset=UTF-8'
|
|
|
|
|
2013-06-25 23:35:26 +08:00
|
|
|
# Set up the email log
|
2013-06-11 03:33:37 +08:00
|
|
|
to_address = @message.to
|
|
|
|
to_address = to_address.first if to_address.is_a?(Array)
|
2013-06-14 06:11:10 +08:00
|
|
|
email_log = EmailLog.new(email_type: @email_type,
|
|
|
|
to_address: to_address,
|
|
|
|
user_id: @user.try(:id))
|
|
|
|
|
2013-07-03 02:13:46 +08:00
|
|
|
|
|
|
|
@message.header['List-Id'] = Email::Sender.list_id_for(SiteSetting.title, Discourse.base_url)
|
|
|
|
|
2013-06-19 03:54:02 +08:00
|
|
|
add_header_to_log('X-Discourse-Reply-Key', email_log, :reply_key)
|
|
|
|
add_header_to_log('X-Discourse-Post-Id', email_log, :post_id)
|
|
|
|
add_header_to_log('X-Discourse-Topic-Id', email_log, :topic_id)
|
2013-06-13 22:56:16 +08:00
|
|
|
|
2013-06-25 23:35:26 +08:00
|
|
|
# Remove headers we don't need anymore
|
|
|
|
@message.header['X-Discourse-Topic-Id'] = nil
|
|
|
|
@message.header['X-Discourse-Post-Id'] = nil
|
|
|
|
@message.header['X-Discourse-Reply-Key'] = nil
|
|
|
|
|
|
|
|
@message.deliver
|
|
|
|
|
|
|
|
# Save and return the email log
|
2013-06-13 22:56:16 +08:00
|
|
|
email_log.save!
|
|
|
|
email_log
|
|
|
|
|
2013-06-11 03:33:37 +08:00
|
|
|
end
|
|
|
|
|
2013-07-03 02:13:46 +08:00
|
|
|
def self.list_id_for(site_name, base_url)
|
|
|
|
|
|
|
|
host = "localhost"
|
|
|
|
if base_url.present?
|
|
|
|
begin
|
|
|
|
uri = URI.parse(base_url)
|
|
|
|
host = uri.host.downcase if uri.host.present?
|
|
|
|
rescue URI::InvalidURIError
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-07-03 02:46:40 +08:00
|
|
|
"\"#{site_name.gsub(/\"/, "'")}\" <discourse.forum.#{Slug.for(site_name)}.#{host}>"
|
2013-07-03 02:13:46 +08:00
|
|
|
end
|
|
|
|
|
2013-06-14 06:11:10 +08:00
|
|
|
private
|
|
|
|
|
|
|
|
def add_header_to_log(name, email_log, email_log_field)
|
|
|
|
header = @message.header[name]
|
|
|
|
return unless header
|
|
|
|
|
|
|
|
val = header.value
|
|
|
|
email_log[email_log_field] = val if val.present?
|
|
|
|
end
|
|
|
|
|
2013-06-11 03:33:37 +08:00
|
|
|
end
|
|
|
|
end
|