如何使用mandrill生成多个收件人的数组


How to generate an array of Multiple recipients using mandrill

我需要向多个收件人发送电子邮件。收件人的数量将根据数据库中的数据而有所不同。

Mandrill允许我使用一个数组只添加多个收件人。

以下是适用于多个收件人的

//email array that needs to be added to the 'to' key
$emailArray = ["example@example.com","test@test.com","hello@test.com","world@test.com"];
$mandrill = new Mandrill('xxxxxxxxxxxxxxx');   
$message = array(
          'subject' => 'Thanks for signing up',
          'from_email' => 'support@test.com',
          'to' => array(
           array(
                'email' => 'hello@test.com',
                'name' => 'Hello Test'
               ),
              array(
               'email' => 'goodbye@test.com',
                'name' => 'Goodbye Test',
              )

            ),
            'global_merge_vars' => array(
                array(
                    'name' => 'FIRSTNAME',
                    'content' => 'JOHN'
                    ),
                array(
                    'name' => 'LASTNAME',
                    'content' => 'DOE')
            ));
        //print_r($message);
        $template_name = 'hello-world';

        print_r($mandrill->messages->sendTemplate($template_name, $template_content, $message));

以下是我需要根据emailArray 的长度动态生成的内容

to' => array(
           //the below array should be dynamically generated   
           array(
                'email' => 'hello@test.com',
                'name' => 'Hello Test'
               ),
              array(
               'email' => 'goodbye@test.com',
                'name' => 'Goodbye Test',
              )

            )

这是数组。当前具有固定值。

 //email array that needs to be added to the 'to' key
$emailArray = ["example@example.com","test@test.com","hello@test.com","world@test.com"];

我的问题是如何根据电子邮件数组的长度生成"收件人"值?

有没有一种方法可以使整个数组脚本内爆?

一个有效的方法是使用我在一些代码中添加的array_map,这些代码也使用一个名称数组(我看不出代码中的名称是从哪里获得的)。

$toAddresses = array('example@example.com','test@test.com','hello@test.com','world@test.com');
$names = array('Exmaple', 'Test', 'Hello', 'World');
$mandrillTo = array_map( function ($address, $name) {
    return array(
        'email' => $address,
        'name' => $name
    );
},
$toAddresses,
$names
);

这会将每个数组中的第0个元素传递到函数中,并返回一个由两个值组成的数组,然后传递第1个、第2个等,并在一个新数组($mandrilTo)中返回每个结果