2011-02-01 09:57:29 +08:00
|
|
|
#
|
2015-02-14 04:06:56 +08:00
|
|
|
# Copyright (C) 2011 - 2015 Instructure, Inc.
|
2011-02-01 09:57:29 +08:00
|
|
|
#
|
|
|
|
# This file is part of Canvas.
|
|
|
|
#
|
|
|
|
# Canvas is free software: you can redistribute it and/or modify it under
|
|
|
|
# the terms of the GNU Affero General Public License as published by the Free
|
|
|
|
# Software Foundation, version 3 of the License.
|
|
|
|
#
|
|
|
|
# Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
|
|
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
|
|
|
|
# A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
|
|
|
|
# details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Affero General Public License along
|
|
|
|
# with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
#
|
|
|
|
|
2015-04-09 01:21:08 +08:00
|
|
|
require 'mail'
|
|
|
|
|
2011-04-07 02:46:06 +08:00
|
|
|
class Mailer < ActionMailer::Base
|
|
|
|
|
2011-02-01 09:57:29 +08:00
|
|
|
attr_reader :email
|
|
|
|
|
2013-10-05 05:14:44 +08:00
|
|
|
# define in rails3-style
|
2014-01-17 06:08:04 +08:00
|
|
|
def create_message(m)
|
2013-10-05 05:14:44 +08:00
|
|
|
# notifications have context, bounce replies don't.
|
|
|
|
headers('Auto-Submitted' => m.context ? 'auto-generated' : 'auto-replied')
|
|
|
|
|
|
|
|
params = {
|
2014-05-22 06:06:13 +08:00
|
|
|
from: from_mailbox(m),
|
2013-10-05 05:14:44 +08:00
|
|
|
to: m.to,
|
|
|
|
subject: m.subject
|
|
|
|
}
|
|
|
|
|
2014-08-13 06:35:48 +08:00
|
|
|
reply_to = reply_to_mailbox(m)
|
|
|
|
params[:reply_to] = reply_to if reply_to
|
2013-10-05 05:14:44 +08:00
|
|
|
|
|
|
|
mail(params) do |format|
|
|
|
|
format.text{ render text: m.body }
|
2013-11-12 04:56:13 +08:00
|
|
|
format.html{ render text: m.html_body } if m.html_body
|
2013-02-21 13:16:24 +08:00
|
|
|
end
|
2011-02-01 09:57:29 +08:00
|
|
|
end
|
2014-05-22 06:06:13 +08:00
|
|
|
|
|
|
|
private
|
2014-08-15 02:32:48 +08:00
|
|
|
def quoted_address(display_name, address)
|
|
|
|
addr = Mail::Address.new(address)
|
|
|
|
addr.display_name = display_name
|
|
|
|
addr.format
|
|
|
|
end
|
|
|
|
|
2014-05-22 06:06:13 +08:00
|
|
|
def from_mailbox(message)
|
2014-08-15 02:32:48 +08:00
|
|
|
quoted_address(message.from_name || HostUrl.outgoing_email_default_name, HostUrl.outgoing_email_address)
|
2014-05-22 06:06:13 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def reply_to_mailbox(message)
|
|
|
|
address = IncomingMail::ReplyToAddress.new(message).address
|
|
|
|
return address unless message.reply_to_name.present?
|
2014-08-13 06:35:48 +08:00
|
|
|
return nil unless address.present?
|
2014-08-15 02:32:48 +08:00
|
|
|
|
|
|
|
quoted_address(message.reply_to_name, address)
|
2014-05-22 06:06:13 +08:00
|
|
|
end
|
2011-02-01 09:57:29 +08:00
|
|
|
end
|