From 578e929e0d66782ea85fdd259c8476ce32e3c5f9 Mon Sep 17 00:00:00 2001 From: Patrick Gansterer Date: Sat, 19 Aug 2017 16:24:02 +0200 Subject: [PATCH] Add handling for bounce mails --- app/mail_receivers/bounce_mail_receiver.rb | 17 +++++++++++++++++ app/mailers/mailer.rb | 6 ++++++ config/initializers/mail_receiver.rb | 1 + 3 files changed, 24 insertions(+) create mode 100644 app/mail_receivers/bounce_mail_receiver.rb create mode 100644 config/initializers/mail_receiver.rb diff --git a/app/mail_receivers/bounce_mail_receiver.rb b/app/mail_receivers/bounce_mail_receiver.rb new file mode 100644 index 00000000..73da529b --- /dev/null +++ b/app/mail_receivers/bounce_mail_receiver.rb @@ -0,0 +1,17 @@ +class BounceMailReceiver + + def self.regexp + /bounce\+(?.*)=(?[^=]+)/ + end + + def received(match, data) + address = "#{match[:local]}@#{match[:domain]}" + mail = Mail.new data + subject = mail.subject || 'Unknown bounce error' + MailDeliveryStatus.create email: address, + message: subject, + attachment_mime: 'message/rfc822', + attachment_data: data + end + +end diff --git a/app/mailers/mailer.rb b/app/mailers/mailer.rb index ad5aebcd..0ff7053b 100644 --- a/app/mailers/mailer.rb +++ b/app/mailers/mailer.rb @@ -89,6 +89,12 @@ class Mailer < ActionMailer::Base args[k] = "#{show_user user} <#{user.email}>" if user.is_a? User end + reply_email_domain = FoodsoftConfig[:reply_email_domain] + if reply_email_domain && !args[:return_path] + address = Mail::Parsers::AddressListsParser.new.parse(args[:to]).addresses.first + args[:return_path] = "<#{FoodsoftConfig.scope}.bounce+#{address.local}=#{address.domain}@#{reply_email_domain}>" + end + super end diff --git a/config/initializers/mail_receiver.rb b/config/initializers/mail_receiver.rb new file mode 100644 index 00000000..67288cc1 --- /dev/null +++ b/config/initializers/mail_receiver.rb @@ -0,0 +1 @@ +FoodsoftMailReceiver.register BounceMailReceiver