Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Handle incoming mails. Closes #4016.
* Uses Mailman gem * Can be configured for POP3, Maildir, or stdin (push from mailserver) * Maildir can be chained with fetchmail or similar to support IMAP * Can be run as part of the job server, or as a separate process Change-Id: I000000000000000000000000000001 Reviewed-on: https://gerrit.instructure.com/2971 Tested-by: Hudson <hudson@instructure.com> Reviewed-by: Brian Whitmer <brian@instructure.com>
- Loading branch information
Showing
13 changed files
with
159 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# See http://rubydoc.info/github/titanous/mailman/master/file/USER_GUIDE.md for | ||
# available options. rails_root will be configured automatically. | ||
# | ||
# Currently, there are three ways to process incoming mail: | ||
# * Fetch directly from POP3, controlled by the Canvas job server | ||
# * Make sure to configure pop3, ignore stdin, and set the poll_interval to 0 | ||
# * Process messages by piping them to script/process_incoming_messages | ||
# * Make sure to not ignore stdin | ||
# * Monitor a maildir | ||
# * Make sure to ignore stdin | ||
# * If poll_interval is 0, it will be run periodically by the Canvas job | ||
# server | ||
# * If poll_interval is non-0, script/process_incoming_messages will never | ||
# return, continually monitoring the directory | ||
|
||
development: | ||
# defaults are to allow reading from stdin | ||
|
||
test: | ||
maildir: "maildir" | ||
ignore_stdin: true | ||
poll_interval: 0 | ||
|
||
production: | ||
poll_interval: 0 | ||
ignore_stdin: true | ||
pop3: | ||
server: "pop.example.com" | ||
port: 110 | ||
username: "user" | ||
password: "password" | ||
ssl: true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# Initialize incoming email configuration. See config/incoming_mail.yml.example. | ||
|
||
config = Setting.from_config("incoming_mail") || {} | ||
|
||
Rails.configuration.to_prepare do | ||
config.each do |key, value| | ||
value = value.symbolize_keys if value.respond_to? :symbolize_keys | ||
Mailman.config.send(key + '=', value) | ||
end | ||
# yes, this is lame, but setting this to real nil makes mailman assume '.', | ||
# which then reloads the rails configuration (and gets an error because we | ||
# try to remove a method that's already there | ||
Mailman.config.rails_root = 'nil' | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
# | ||
# Copyright (C) 2011 Instructure, Inc. | ||
# | ||
# 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/>. | ||
# | ||
|
||
class IncomingMessageProcessor | ||
def self.process | ||
addr, domain = HostUrl.outgoing_email_address.split(/@/) | ||
regex = Regexp.new("#{Regexp.escape(addr)}\\+([0-9a-f]+)-(\\d+)@#{Regexp.escape(domain)}") | ||
Mailman::Application.run do | ||
to regex do | ||
html_body = (message.parts.find { |p| p.content_type = 'text/html' }).body.decoded if message.multipart? | ||
html_body = message.body.decoded if !message.multipart? && message.content_type = 'text/html' | ||
body = (message.parts.find { |p| p.content_type = 'text/plain' }).body.decoded if message.multipart? | ||
body ||= message.body.decoded | ||
if !html_body | ||
self.extend TextHelper | ||
html_body = format_message(body).first | ||
end | ||
|
||
msg = Message.find_by_id(params['captures'][1].to_i) | ||
context = msg.context if msg && params['captures'][0] == msg.reply_to_secure_id | ||
cc = CommunicationChannel.find_all_by_path_and_path_type(message.from, 'email') | ||
user = cc.first.user if !cc.empty? | ||
begin | ||
if user && context && context.respond_to?(:reply_from) | ||
context.reply_from({ | ||
:purpose => 'general', | ||
:user => user, | ||
:subject => message.subject, | ||
:html => html_body, | ||
:text => body | ||
}) | ||
else | ||
IncomingMessageProcessor.ndr(message.from.first, message.subject) | ||
end | ||
rescue => e | ||
ErrorLogging.log_exception(:default, e, :message => "Incoming Message Failed", :params => {:from => message.from.first, :to => message.to} ) | ||
end | ||
end | ||
default do | ||
IncomingMessageProcessor.ndr(message.from.first, message.subject) | ||
end | ||
end | ||
end | ||
|
||
def self.ndr(from, subject) | ||
message = Message.create!( | ||
:to => from, | ||
:from => HostUrl.outgoing_email_address, | ||
:subject => "Message Reply Failed: #{subject}", | ||
:body => %{The message titled "#{subject}" could not be delivered. The message was sent to an unknown mailbox address. If you are trying to contact someone through Canvas you can try logging in to your account and sending them a message using the Inbox tool. | ||
Thank you, | ||
Canvas Support | ||
}, | ||
:delay_for => 0, | ||
:context => nil, | ||
:path_type => 'email', | ||
:from_name => "Instructure" | ||
) | ||
message.deliver | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
#!/usr/bin/env ruby | ||
|
||
require File.expand_path(File.join(File.dirname(__FILE__), '..', 'config', 'environment')) | ||
require 'incoming_message_processor' | ||
|
||
IncomingMessageProcessor::process |