class ApplicationMailer < ActionMailer::Base default from: 'info@helpbuild.co' helper MailerStyleHelper layout 'mailer' def try_delivering(_options = {}) yield true rescue EOFError, IOError, Errno::ECONNRESET, Errno::ECONNABORTED, Errno::EPIPE, Errno::ETIMEDOUT, Net::SMTPAuthenticationError, Net::SMTPServerBusy, Net::SMTPSyntaxError, Net::SMTPUnknownError, OpenSSL::SSL::SSLError => e ExceptionHub.notify(e) false end private def deliver_mail(email, subject, reply_to = nil, to_name = '') addresses = set_addresses_and_user(email: email, to_name: to_name) reply_to = addresses[:from] if reply_to.nil? try_delivering do I18n.with_locale('en') do mail(reply_to: reply_to, from: addresses[:from], to: addresses[:to], subject: "[Helpbuild] | #{subject}") end end end def set_addresses_and_user(email: '', from_name: 'Helpbuild', to_name: '', from_email: nil) addresses = {} from_email ||= Rails.configuration.action_mailer[:default_options][:from] addresses[:from] = %(#{from_name} <#{from_email}>) addresses[:reply_to] = %(#{from_name} <#{from_email}>) addresses[:to] = %("#{to_name}" <#{email}>) addresses end end