Skip to content

Instantly share code, notes, and snippets.

@andreabedini
Last active November 18, 2015 02:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andreabedini/3e5526e60b186cce3f23 to your computer and use it in GitHub Desktop.
Save andreabedini/3e5526e60b186cce3f23 to your computer and use it in GitHub Desktop.
http_email_receiver
# name: http_email_receiver
# about: Receives emails through HTTP post request
# version: 0.0.0
# authors: Andrea Bedini <andrea@andreabedini.com>
after_initialize do
require_dependency 'email/receiver'
module ::HttpEmailPlugin
class Engine < ::Rails::Engine
isolate_namespace HttpEmailPlugin
end
class IncomingEmailController < ActionController::Base
def handle_mail
begin
mail_string = params["body-mime"]
Email::Receiver.new(mail_string).process
rescue => e
handle_failure(mail_string, e)
end
# think about this
head :ok
end
# copied from app/jobs/scheduled/poll_mailbox.rb
def handle_failure(mail_string, e)
Rails.logger.warn("Email can not be processed: #{e}\n\n#{mail_string}") if SiteSetting.log_mail_processing_failures
template_args = {}
case e
when Email::Receiver::UserNotSufficientTrustLevelError
message_template = :email_reject_trust_level
when Email::Receiver::UserNotFoundError
message_template = :email_reject_no_account
when Email::Receiver::EmptyEmailError
message_template = :email_reject_empty
when Email::Receiver::EmailUnparsableError
message_template = :email_reject_parsing
when Email::Receiver::EmailLogNotFound
message_template = :email_reject_reply_key
when Email::Receiver::BadDestinationAddress
message_template = :email_reject_destination
when Email::Receiver::TopicNotFoundError
message_template = :email_reject_topic_not_found
when Email::Receiver::TopicClosedError
message_template = :email_reject_topic_closed
when Email::Receiver::AutoGeneratedEmailError
message_template = :email_reject_auto_generated
when Discourse::InvalidAccess
message_template = :email_reject_invalid_access
when ActiveRecord::Rollback
message_template = :email_reject_post_error
when Email::Receiver::InvalidPost
if e.message.length < 6
message_template = :email_reject_post_error
else
message_template = :email_reject_post_error_specified
template_args[:post_error] = e.message
end
else
message_template = nil
end
if message_template
# inform the user about the rejection
message = Mail::Message.new(mail_string)
template_args[:former_title] = message.subject
template_args[:destination] = message.to
template_args[:site_name] = SiteSetting.title
client_message = RejectionMailer.send_rejection(message_template, message.from, template_args)
Email::Sender.new(client_message, message_template).send
else
Discourse.handle_job_exception(e, error_context(@args, "Unrecognized error type when processing incoming email", mail: mail_string))
end
end
end
HttpEmailPlugin::Engine.routes.draw do
post '/mime', to: 'incoming_email#handle_mail'
end
Discourse::Application.routes.append do
mount ::HttpEmailPlugin::Engine, at: '/plugins/incoming_email'
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment