aboutsummaryrefslogtreecommitdiffstats
path: root/lib/tasks
diff options
context:
space:
mode:
authorGeorge Claghorn <george.claghorn@gmail.com>2018-11-05 14:21:27 -0500
committerGitHub <noreply@github.com>2018-11-05 14:21:27 -0500
commit152a442b1902050265ddcef56f5506b3bfbb12e4 (patch)
treebd365d76299e978a6e974a64f5ff3d47f0614514 /lib/tasks
parentc474daefb18e9bab96f6f0bb0bb30dfc00058cb3 (diff)
parentac7fd0e56886eb134554789e014d2736b95d7042 (diff)
downloadrails-152a442b1902050265ddcef56f5506b3bfbb12e4.tar.gz
rails-152a442b1902050265ddcef56f5506b3bfbb12e4.tar.bz2
rails-152a442b1902050265ddcef56f5506b3bfbb12e4.zip
Merge pull request #1 from basecamp/ingresses
Accept inbound emails from a variety of ingresses
Diffstat (limited to 'lib/tasks')
-rw-r--r--lib/tasks/ingress.rake41
1 files changed, 41 insertions, 0 deletions
diff --git a/lib/tasks/ingress.rake b/lib/tasks/ingress.rake
new file mode 100644
index 0000000000..510951aa2a
--- /dev/null
+++ b/lib/tasks/ingress.rake
@@ -0,0 +1,41 @@
+# frozen_string_literal: true
+
+namespace :action_mailbox do
+ namespace :ingress do
+ desc "Pipe an inbound email from STDIN to the Postfix ingress at the given URL"
+ task :postfix do
+ require "active_support"
+ require "active_support/core_ext/object/blank"
+ require "http"
+
+ unless url = ENV["URL"].presence
+ abort "5.3.5 URL is required"
+ end
+
+ unless password = ENV["INGRESS_PASSWORD"].presence
+ abort "5.3.5 INGRESS_PASSWORD is required"
+ end
+
+ begin
+ response = HTTP.basic_auth(user: "actionmailbox", pass: password)
+ .timeout(connect: 1, write: 10, read: 10)
+ .post(url, headers: { "Content-Type" => "message/rfc822", "User-Agent" => ENV.fetch("USER_AGENT", "Postfix") }, body: STDIN)
+
+ case
+ when response.status.success?
+ puts "2.0.0 HTTP #{response.status}"
+ when response.status.unauthorized?
+ abort "4.7.0 HTTP #{response.status}"
+ when response.status.unsupported_media_type?
+ abort "5.6.1 HTTP #{response.status}"
+ else
+ abort "4.0.0 HTTP #{response.status}"
+ end
+ rescue HTTP::ConnectionError => error
+ abort "4.4.2 Error connecting to the Postfix ingress: #{error.message}"
+ rescue HTTP::TimeoutError
+ abort "4.4.7 Timed out piping to the Postfix ingress"
+ end
+ end
+ end
+end