Class: MailSafe::AddressReplacer
- Inherits:
-
Object
- Object
- MailSafe::AddressReplacer
- Defined in:
- lib/mail_safe-sendgrid.rb
Constant Summary collapse
- @@replaced_addresses =
{}
- @@address_types =
ADDRESS_TYPES.dup
Class Method Summary collapse
- .add_body_postscript_with_mail_header(part, replaced_addresses, call_super = false) ⇒ Object
-
.add_xsmtpapi_to_address_types ⇒ Object
ADDRESS_TYPES is frozen but we want to add :X_SMTPAPI to it so the original add__postscript functions have the SendGrid specific addresses that MailSafe removed as well when generating their message.
-
.remove_xsmtpapi_from_address_types ⇒ Object
We then need to reset ADDRESS_TYPES back to the frozen value for MailSafe to be able to process the next message properly.
- .replace_external_addresses_with_mail_header(mail) ⇒ Object
Class Method Details
.add_body_postscript_with_mail_header(part, replaced_addresses, call_super = false) ⇒ Object
9 10 11 12 13 14 15 16 17 18 |
# File 'lib/mail_safe-sendgrid.rb', line 9 def add_body_postscript_with_mail_header(part, replaced_addresses, call_super = false) # we don't want to call add_body_postscript until after we've added the SendGrid SMTPAPI # replaced addresses to the list of all addresses replaced by Mail Safe @@replaced_addresses.reverse_merge! replaced_addresses if call_super or replaced_addresses[:X_SMTPAPI] add_xsmtpapi_to_address_types add_body_postscript_without_mail_header(part, @@replaced_addresses) remove_xsmtpapi_from_address_types end end |
.add_xsmtpapi_to_address_types ⇒ Object
ADDRESS_TYPES is frozen but we want to add :X_SMTPAPI to it so the original add__postscript functions have the SendGrid specific addresses that MailSafe removed as well when generating their message. Otherwise we would have to override these functions entirely.
59 60 61 62 63 64 65 66 |
# File 'lib/mail_safe-sendgrid.rb', line 59 def add_xsmtpapi_to_address_types modified_address_types = @@address_types.dup modified_address_types << :X_SMTPAPI # silence already initialized constant ADDRESS_TYPES silence_warnings do MailSafe::AddressReplacer.singleton_class.const_set(:ADDRESS_TYPES,modified_address_types.uniq) end end |
.remove_xsmtpapi_from_address_types ⇒ Object
We then need to reset ADDRESS_TYPES back to the frozen value for MailSafe to be able to process the next message properly
70 71 72 73 74 75 76 |
# File 'lib/mail_safe-sendgrid.rb', line 70 def remove_xsmtpapi_from_address_types # silence already initialized constant ADDRESS_TYPES silence_warnings do MailSafe::AddressReplacer.singleton_class.const_set(:ADDRESS_TYPES,@@address_types) end end |
.replace_external_addresses_with_mail_header(mail) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/mail_safe-sendgrid.rb', line 22 def replace_external_addresses_with_mail_header(mail) replace_external_addresses_without_mail_header(mail) # load X-SMTPAPI headers (json) x_smtpapi = JSON.parse mail.header['X-SMTPAPI'].to_s external_indices = [] # get a list of indices for external addresses x_smtpapi['to'].each_with_index do |address, index| external_indices << index if !MailSafe::Config.is_internal_address?(address) end deleted_cnt = 0 deleted_addresses = [] external_indices.each do |i| # remove any external address from :to deleted_addresses << x_smtpapi['to'].delete_at(i - deleted_cnt) # remove the related tokens for every array in :sub x_smtpapi['sub'].each do |k, v| v.delete_at(i-deleted_cnt) x_smtpapi['sub'][k] = v end # the indices decrement as we remove elements, deleted_cnt accounts for this deleted_cnt += 1 end # save the headers back to X-SMTPAPI mail.header['X-SMTPAPI'].value = x_smtpapi.to_json.gsub(/(["\]}])([,:])(["\[{])/, '\\1\\2 \\3') add_body_postscript(mail,{:X_SMTPAPI=>deleted_addresses}, true) # we need to override add_text_postscript to include sendgrid x-smtpapi and not ADDRESS_TYPES only end |