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