Skip to content

Instantly share code, notes, and snippets.

@tadman
Created September 26, 2014 18:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tadman/041339b8d60e7c5c3eec to your computer and use it in GitHub Desktop.
Save tadman/041339b8d60e7c5c3eec to your computer and use it in GitHub Desktop.
Cc: and Bcc: support patches
class PostageApp::Mailer
def mail(headers = nil, &block)
# Guard flag to prevent both the old and the new API from firing
# Should be removed when old API is removed
@_mail_was_called = true
m = @_message
# Call all the procs (if any)
class_default = self.class.default
default_values = class_default.merge(self.class.default) do |k,v|
v.respond_to?(:call) ? v.bind(self).call : v
end
# Handle defaults
headers = default_values.merge(headers || { })
# At the beginning, do not consider class default for parts order neither content_type
content_type = headers[:content_type]
# Apply charset at the beginning so all fields are properly quoted
charset = headers[:charset]
# Set configure delivery behavior
wrap_delivery_behavior!(
headers.delete(:delivery_method),
headers.delete(:delivery_method_options)
)
# Assigning recipients
targets = Hash[
[ :to, :cc, :bcc ].collect do |header|
[
header,
case (value = headers.delete(header))
when Hash
value
when Array
Hash[value.collect { |v| [ v, { } ] }]
else
if (value.present?)
{ value => { } }
else
{ }
end
end
]
end
]
m.arguments['recipients'] = targets[:bcc].merge(targets[:cc]).merge(targets[:to])
[ :to, :cc ].each do |header|
next if (targets[header].empty?)
headers[header] = targets[header].keys.join(',')
end
# Assign all headers except parts_order, content_type and body
assignable = headers.except(
:parts_order, :content_type, :body, :template_name, :template_path
)
m.headers.merge!(assignable)
# Render the templates and blocks
responses = collect_responses(headers, &block)
create_parts_from_responses(m, responses)
m
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment