简体   繁体   中英

Mandrill send email to multiple with array

I am using Mandrill API to send email. I have a bunch of @request.agents (an array of Agent ID's) and I want to send all agents in the array the email.

This is my current method but it doesn't seem to be passing the a (I am aware that this may be completely the wrong approach)

def new_request_agents(request, user)

    @request = request
    @user = user

    template_name = "new-request"
    template_content = []
    message = {
        to: [
                request.agents.each do |a| 
                  if a != ""
                    {
                        email: Agent.find(a.to_i).email
                    }
                  end
                end
            ],
        subject: "New Request: #{user.email}",
        merge_vars: [
            {rcpt: @user.email,
            vars: [
                {name: "MESSAGE", content: request.message},
                {name: "AGENTS", content: request.agents}
                ]
            }
        ]
    }

    mandrill_client.messages.send_template template_name, template_content, message
end

Is there a better (and working) way of doing this?

UPDATE

My API log shows this:

{
"template_name": "new-request",
"template_content": {},
"message": {
    "to": [
        [
            "",
            "4",
            "5"
        ]
    ],
    "subject": "New Request: example@gmail.com",
    "merge_vars": [
        {
            "vars": [
                {
                    "name": "MESSAGE",
                    "content": "Test message"
                },
                {
                    "name": "AGENTS",
                    "content": [
                        "",
                        "4",
                        "5"
                    ]
                }
            ]
        }
    ]
},
"async": false,
"ip_pool": null,
"send_at": null,
"key": "B-B_-yRi1LLLhBrzZEVLLw"

}

You should just remove rcpt from merge_vars because there is more than one recipient.

And make sure message does contain all your emails by doing: message.inspect .

An other good practice is looking at API logs in your mandrill dashboard. There is always useful informations.

Update:

Try this

request.agents.each do |a| 
  {
    email: a.email
  }
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM