mirror of
https://github.com/discourse/discourse.git
synced 2025-05-22 16:21:18 +08:00
Work in Progress: Reply By Email:
- Add support classes and settings to enable reply by email - Split out Email builder to be more OO, add many specs
This commit is contained in:
@ -1,32 +0,0 @@
|
||||
# Help us build an email
|
||||
module Email
|
||||
|
||||
module Builder
|
||||
|
||||
def build_email(to, email_key, params={})
|
||||
params[:site_name] = SiteSetting.title
|
||||
params[:base_url] = Discourse.base_url
|
||||
params[:user_preferences_url] = "#{Discourse.base_url}/user_preferences"
|
||||
|
||||
body = I18n.t("#{email_key}.text_body_template", params)
|
||||
|
||||
# Are we appending an unsubscribe link?
|
||||
if params[:add_unsubscribe_link]
|
||||
body << "\n"
|
||||
body << I18n.t("unsubscribe_link", params)
|
||||
headers 'List-Unsubscribe' => "<#{params[:user_preferences_url]}>"
|
||||
end
|
||||
|
||||
mail_args = {
|
||||
to: to,
|
||||
subject: I18n.t("#{email_key}.subject_template", params),
|
||||
body: body
|
||||
}
|
||||
mail_args[:from] = params[:from] || SiteSetting.notification_email
|
||||
mail_args[:charset] = 'UTF-8'
|
||||
mail(mail_args)
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
19
lib/email/incoming_message.rb
Normal file
19
lib/email/incoming_message.rb
Normal file
@ -0,0 +1,19 @@
|
||||
module Email
|
||||
|
||||
class IncomingMessage
|
||||
|
||||
attr_reader :reply_key,
|
||||
:body_plain
|
||||
|
||||
def initialize(reply_key, body)
|
||||
@reply_key = reply_key
|
||||
@body = body
|
||||
end
|
||||
|
||||
def reply
|
||||
@reply ||= EmailReplyParser.read(@body).visible_text
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
68
lib/email/message_builder.rb
Normal file
68
lib/email/message_builder.rb
Normal file
@ -0,0 +1,68 @@
|
||||
# Builds a Mail::Mesage we can use for sending. Optionally supports using a template
|
||||
# for the body and subject
|
||||
module Email
|
||||
|
||||
module BuildEmailHelper
|
||||
def build_email(*builder_args)
|
||||
builder = Email::MessageBuilder.new(*builder_args)
|
||||
headers(builder.header_args) if builder.header_args.present?
|
||||
mail(builder.build_args)
|
||||
end
|
||||
end
|
||||
|
||||
class MessageBuilder
|
||||
|
||||
def initialize(to, opts=nil)
|
||||
@to = to
|
||||
@opts = opts || {}
|
||||
end
|
||||
|
||||
def subject
|
||||
subject = @opts[:subject]
|
||||
subject = I18n.t("#{@opts[:template]}.subject_template", template_args) if @opts[:template]
|
||||
subject
|
||||
end
|
||||
|
||||
def body
|
||||
body = @opts[:body]
|
||||
body = I18n.t("#{@opts[:template]}.text_body_template", template_args) if @opts[:template]
|
||||
|
||||
if @opts[:add_unsubscribe_link]
|
||||
body << "\n"
|
||||
body << I18n.t('unsubscribe_link', template_args)
|
||||
end
|
||||
|
||||
body
|
||||
end
|
||||
|
||||
def template_args
|
||||
@template_args ||= { site_name: SiteSetting.title,
|
||||
base_url: Discourse.base_url,
|
||||
user_preferences_url: "#{Discourse.base_url}/user_preferences" }.merge!(@opts)
|
||||
end
|
||||
|
||||
def build_args
|
||||
mail_args = { to: @to,
|
||||
subject: subject,
|
||||
body: body,
|
||||
charset: 'UTF-8' }
|
||||
|
||||
mail_args[:from] = @opts[:from] || SiteSetting.notification_email
|
||||
|
||||
if @opts[:from_alias]
|
||||
mail_args[:from] = "#{@opts[:from_alias]} <#{mail_args[:from]}>"
|
||||
end
|
||||
mail_args
|
||||
end
|
||||
|
||||
def header_args
|
||||
result = {}
|
||||
if @opts[:add_unsubscribe_link]
|
||||
result['List-Unsubscribe'] = "<#{template_args[:user_preferences_url]}>" if @opts[:add_unsubscribe_link]
|
||||
end
|
||||
result
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
30
lib/email/receiver.rb
Normal file
30
lib/email/receiver.rb
Normal file
@ -0,0 +1,30 @@
|
||||
#
|
||||
# Handles an incoming message
|
||||
#
|
||||
require_dependency 'email/incoming_message'
|
||||
|
||||
module Email
|
||||
class Receiver
|
||||
|
||||
def self.results
|
||||
@results ||= Enum.new(:unprocessable)
|
||||
end
|
||||
|
||||
def initialize(incoming_message)
|
||||
@incoming_message = incoming_message
|
||||
end
|
||||
|
||||
def process
|
||||
|
||||
if @incoming_message.blank? || @incoming_message.reply_key.blank?
|
||||
return Email::Receiver.results[:unprocessable]
|
||||
end
|
||||
|
||||
log = EmailLog.where(reply_key: @incoming_message.reply_key).first
|
||||
return Email::Receiver.results[:unprocessable] if log.blank?
|
||||
|
||||
nil
|
||||
end
|
||||
|
||||
end
|
||||
end
|
Reference in New Issue
Block a user