【发布时间】:2014-01-04 07:33:23
【问题描述】:
我需要向多个收件人发送电子邮件。接收者的数量将根据数据库中的数据而有所不同。
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'不是有效的php,没有问题
-
嗨 Dagon,这是我从 mandrill 文档中挑选的脚本。我需要根据 $emailArray 的长度生成“收件人”值。
-
$emailArray 不是有效的 php,你不能用它做任何事情
-
好的,你能帮我纠正一下吗?应该是 $array = array("foo" => "bar", "bar" => "foo", );
-
@william 是的,威廉,解决方案很简单。在 for 循环中运行它:/。但是您需要在 for 循环中初始化 mandrill,以便它适用于每次迭代。
标签: php arrays dynamic-arrays mandrill