Mandrill向多个阵列发送电子邮件

时间:2015-03-07 23:54:51

标签: ruby-on-rails arrays email mandrill

我正在使用Mandrill API发送电子邮件。我有一堆@request.agents(一组代理ID' s),我想发送电子邮件中阵列中的所有代理。

这是我目前的方法,但它似乎没有通过a(我知道这可能是完全错误的方法)

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

有更好的(和工作)方式吗?

更新

我的API日志显示了这个:

{
"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"

}

1 个答案:

答案 0 :(得分:0)

您应该从rcpt删除merge_vars,因为有多个收件人。

并确保message确实包含您的所有电子邮件:message.inspect

另一个好的做法是查看mandrill仪表板中的API日志。总是有用的信息。

更新

试试这个

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