renz45 / mandrill_mailer

A small gem for sending Mandrill template emails
260 stars 84 forks source link

Proper format to including multiple addresses via CC? #114

Closed Aerlinger closed 8 years ago

Aerlinger commented 8 years ago

Hi, I'm trying to send to multiple addresses using the format documented in the Mandrill API here: https://mandrillapp.com/api/docs/messages.JSON.html#method-send-template

My request

mandrill_mail(template: "template_name",
                  to: [
                           { email: "recipient@example.com", name: "User" },
                           { email: "cc1_recipient@example.com", name: "CC User 1", type: "cc" },
                           { email: "cc2_recipient@example.com", name: "CC User 2", type: "cc" }
                  ]

However, it seems as though only recipient@example.com actually gets the email. Is there something wrong with this request format?

renz45 commented 8 years ago

Odd, that should work. If you remove the type from the last 2 do they get it?

I don't see anything in the gem that removes type, and it looks like arrays are handled in the format helpers.

You can get the object that's sent to the mandrill api by doing:

mailer = mandrill_mail(template: "template_name",
  to: [
    { email: "recipient@example.com", name: "User" },
    { email: "cc1_recipient@example.com", name: "CC User 1", type: "cc" },
    { email: "cc2_recipient@example.com", name: "CC User 2", type: "cc" }
  ]
)

mailer.message

The message is the hash that gets passed directly to the official mandrill_api gem, so if that format looks good then it's likely something outside the gem. :sweat_smile:

Aerlinger commented 8 years ago

Yup, what's weird is it looks like all the emails get sent separately to each individual user.

mailer.message["to"]

[{:email=>"primary@example.com", :name=>"Testemail", "type"=>"to"}, {:email=>"user1@example.com", :name=>"Annie", :type=>"cc"}, {:email=>"email@example.com", :name=>"Dan", :type=>"cc"}, {:email=>"user3@example.com", :name=>"Amy", :type=>"cc"}]

mailer.deliver

[{"email"=>"primary@example.com", "status"=>"sent", "_id"=>"4365b369fa1b4e938fde141fcf235242", "reject_reason"=>nil}, {"user1@example.com", "status"=>"sent", "_id"=>"b002a2feba0740f7a73434dd698e3efc", "reject_reason"=>nil}, {"email"=>"user2@example.com", "status"=>"sent", "_id"=>"920429f434c64b4c8992562eede91082", "reject_reason"=>nil}, {"email"=>"user3@example.com", "status"=>"sent", "_id"=>"8e1dd7c62f5246a5a5b557351f54a0df", "reject_reason"=>nil}]
renz45 commented 8 years ago

I think even as CC, they would still return a status of being sent. The cc part is handled differently in the headers that get sent with the email, so judging by the response, the gem is working as intended.

It sort of looks like this has been an issue before with the mandrill api just ignoring BCC and CC emails, granted its a few months old:

https://github.com/laravel/framework/issues/7276

I wonder if it's still an issue?

I also found this: http://stackoverflow.com/questions/18211868/send-email-via-the-mandrill-api-containing-multiple-bcc-addresses

It seems like you might have to set preserve_recipients to true for it to work.

Aerlinger commented 8 years ago

Thanks, had to stab around in the dark for a while, but it seem as though the the to: parameter works as expected if the preserve_recipients option is also set to true. I had preserve recipients set under my Mandrill settings, however, it was being overridden by the gem/api call.

Also, sorry if I missed this in the documentation but is there a way to specify default mailer options, perhaps in .configure?

renz45 commented 8 years ago

I think people have been using the interceptor api for setting defaults: https://github.com/renz45/mandrill_mailer#using-an-interceptor

Conversely, you might find it easer to make a base mailer class and set defaults using the default keyword like seen here: https://github.com/renz45/mandrill_mailer#creating-a-new-mailer

Then inherit from that for your mailers. This would allow you to sort of namespace default configs to groups of mailers without requiring them all to share the same.

renz45 commented 8 years ago

Closing :) Feel free to reopen if there are any other related things.