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