This repository has been archived by the owner on Nov 27, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 143
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #122 from mailman/v0.8.0
Aggregated release v0.8.0
- Loading branch information
Showing
22 changed files
with
557 additions
and
20 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
--require spec_helper | ||
--format progress | ||
-cfd |
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 |
---|---|---|
|
@@ -18,4 +18,5 @@ matrix: | |
- rvm: ruby-head | ||
- rvm: jruby-19mode | ||
- rvm: jruby-head | ||
- rvm: rbx-2 | ||
- rvm: rbx-19mode |
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,26 @@ | ||
#!/usr/bin/env ruby | ||
$LOAD_PATH.unshift(File.join(__dir__, '../lib')) | ||
require 'mailman' | ||
|
||
# This example will process emails sent via Sendgrid and create | ||
# a text file for every email sent to 'message-<number>@yourdomain.com' containing | ||
# the email address that last sent a message to that number. | ||
# | ||
# 1. Set up a Sendgrid account | ||
# 2. Point your domain's MX record at `mx.sendgrid.net` | ||
# 3. Configure Sendgrid to point to wherever you're running this example (port 6245) | ||
# 4. Profit! | ||
|
||
Mailman.config.http = { | ||
host: "0.0.0.0", | ||
port: 6245, | ||
parser: :sendgrid | ||
} | ||
|
||
Mailman::Application.run do | ||
to %r{^message-(\d+)@} do | ||
open("#{params['captures'].first}.txt", 'w') do |f| | ||
f.write message.from.join(', ') | ||
end | ||
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
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,131 @@ | ||
require 'rack' | ||
require 'uri' | ||
|
||
module Mailman | ||
module Receiver | ||
# Receives messages over HTTP, and passes them to a {MessageProcessor}. | ||
# | ||
# If using CloudMailIn (Raw format) you would make your target "http://yourserver:6245/" and use this code: | ||
# | ||
# Mailman.config.http = { | ||
# host: '0.0.0.0', | ||
# port: 6245, | ||
# path: '/', | ||
# parser: :raw_post, | ||
# parser_opts: { | ||
# part_name: 'message' | ||
# } | ||
# } | ||
# | ||
# Mailman::Application.run do | ||
# # ... etc | ||
# end | ||
# | ||
# However those are all the defaults, so you could also just use: | ||
# | ||
# Mailman.config.http = {} | ||
# | ||
# Mailman::Application.run do | ||
# # ... etc | ||
# end | ||
# | ||
# Sendgrid format is also available, which is simply: | ||
# | ||
# Mailman.config.http = { parser: :sendgrid } | ||
# | ||
# Mailman::Application.run do | ||
# # ... etc | ||
# end | ||
class HTTP | ||
|
||
# @param [Hash] options the receiver options | ||
# @option options [MessageProcessor] :processor the processor to pass new | ||
# messages to | ||
# @option options [String] :host ('0.0.0.0') The host the server should listen on | ||
# @option options [Integer] :port (6245) The port the server should listen on | ||
# @option options [String] :path ('/') The path that should trigger email delivery | ||
# @option options [Symbol] :handler (:thin, :webrick) The rack server to use. Falls back on thin, then webrick. | ||
# @option options [Symbol] :parser (:raw_post) The parser which should be used to extract the email content from the HTTP request. | ||
# @option options [Symbol] :parser_opts ({}) Options to be passed to the parser. | ||
def initialize(options) | ||
@processor = options[:processor] | ||
@listen = URI::HTTP.build( | ||
host: options[:host] || "0.0.0.0", | ||
port: options[:port] || 6245, | ||
path: options[:path] || "/" | ||
) | ||
|
||
options[:parser] ||= :raw_post | ||
parser_klass = "#{options[:parser].to_s.camelize}Parser" | ||
begin | ||
@parser = Mailman::Receiver::HTTP.const_get(parser_klass).new(options[:parser_opts] || {}) | ||
rescue NameError | ||
raise "The Mailman::Receiver::HTTP::#{parser_klass} parser isn't defined." | ||
end | ||
|
||
@handler = Rack::Handler.pick([options[:handler], :thin, :webrick].compact) | ||
end | ||
|
||
# Starts the HTTP server | ||
def start_and_block | ||
@handler.run(self, {:Host => @listen.host, :Port => @listen.port}) do |server| | ||
Mailman.logger.info "Listening for emails at #{@listen} using #{@parser.class.name.demodulize} processing" | ||
end | ||
end | ||
|
||
## Web server components | ||
|
||
def call(env) | ||
return [404, {}, []] if env['REQUEST_PATH'] != @listen.path | ||
begin | ||
@processor.process(@parser.parse(env)) | ||
return [200, {}, []] | ||
rescue Exception => e | ||
Mailman.logger.error(e.message + "\n#{e.backtrace}") | ||
return [500, {}, ["Email processing failed"]] | ||
end | ||
end | ||
|
||
# A class which abstracts the processing of CloudMailIn style 'Raw' emails over HTTP. To use: | ||
# | ||
# Mailman.config.http = { | ||
# parser: :raw_post, | ||
# parser_opts: { | ||
# part_name: 'message' | ||
# } | ||
# } | ||
class RawPostParser | ||
# @param [Hash] opts The parser's options | ||
# @option opts [String] :part_name ('message') The name of the mulipart segment which will contain the email | ||
def initialize(opts = {}) | ||
@opts = opts | ||
@opts['part_name'] ||= 'message' | ||
end | ||
|
||
# Parses a Rack `env` variable and creates a +Mail::Message+ from the email contents found. | ||
def parse(env) | ||
multipart = Rack::Multipart.parse_multipart(env) | ||
Mail.new(multipart[@opts['part_name']]) | ||
end | ||
end | ||
|
||
# A class which abstracts the processing of SendGrid style emails over HTTP. To use: | ||
# | ||
# Mailman.config.http = { parser: :sendgrid } | ||
class SendgridParser | ||
# @param [Hash] opts The parser's options - not used for this parser. | ||
def initialize(opts = {}); end | ||
|
||
# Parses a Rack `env` variable and creates a +Mail::Message+ from the email contents found. | ||
def parse(env) | ||
parts = Rack::Multipart.parse_multipart(env) | ||
Mail.new do | ||
header parts['headers'] | ||
text_part { parts['text'] } | ||
html_part { parts['html'] } | ||
end | ||
end | ||
end | ||
end | ||
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
Oops, something went wrong.