【问题标题】:Build Mandrill 'to' array with PHP使用 PHP 构建 Mandrill 'to' 数组
【发布时间】:2015-04-23 22:58:18
【问题描述】:

我想用 Mandrill 发送多封电子邮件,但不知道如何构建 'to' 数组。从他们的docs 来看,它看起来像这样(并且正在工作):

'from_name' => 'My Name',
'to' => array(
    array(
        'email' => 'first@example.com',
        'name' => 'First Recipient',
        'type' => 'to'
    ),
    array(
        'email' => 'second@example.com',
        'name' => 'Second Recipient',
        'type' => 'to'
    )
),
'headers' => array('Reply-To' => 'me@example.org'),

我的 PHP 代码是:

foreach ($users as $user) {
    $to[] = array(
        array(
            'email' => 'first@example.com',
            'name' => 'First Recipient',
            'type' => 'to'
        ),
        array(
            'email' => 'second@example.com',
            'name' => 'Second Recipient',
            'type' => 'to'
        )
}

但是当我用$to插入收件人时,肯定有问题。

'from_name' => 'My Name',
'to' => $to,
'headers' => array('Reply-To' => 'me@example.org'),

Mandrill 不发送任何内容,结果是一个空数组(这意味着没有发送任何电子邮件)。两个数组对我来说看起来都一样。是吗?

供参考,完整代码如下:

public function notifySubscribers() {

    // Get page
    $scheduledPage = $from_db_query;

    // $users = wire('users')->find('!login_start>=today, !login_end<=today, limit=999');
    // foreach($users as $u) {
    //  if ( $u->hasPermission('page-view', $scheduledPage) ) {
    //      $recipients[] = $u;
    //  }
    // }
    $to = array(
                array(
                    'email' => 'first@example.com',
                    'name' => 'First Recipient',
                    'type' => 'to'
                ),
                array(
                    'email' => 'second@example.com',
                    'name' => 'Second Recipient',
                    'type' => 'to'
                )
            );

    // Returns array with stats from mailing
    $dispatched = $this->dispatchMail($scheduledPage->title, $scheduledPage->body, $scheduledPage->files->first(), $to);

}



/**
 * Dispatches the email to be sent
 *
 * Create email and send one email to each subscriber
 */
function dispatchMail($subject, $pageBody, $file, $from, $recipients) {

    require_once 'lib/mandrill-api-php/src/Mandrill.php';

    try {
        $mandrill = new Mandrill('xxxxxxxxx');
        $template_name = 'e-mail-notification';
        $template_content = array(
            array(
                'name' => 'PREHADER',
                'content' => 'Neue Ausgabe'
            )
        );
        $message = array(
            // 'html' => 'HTML Content',
            // 'text' => 'Text Content',
            'subject' => $subject,
            'from_email' => 'john@example.com',
            'from_name' => 'John',
            'to' => $to,
            'headers' => array('Reply-To' => 'reply@example.com'),
            'important' => false,
            'track_opens' => null,
            'track_clicks' => null,
            'auto_text' => null,
            'auto_html' => null,
            'inline_css' => true,
            'url_strip_qs' => null,
            'preserve_recipients' => false,
            'view_content_link' => null,
            // 'bcc_address' => '',
            'tracking_domain' => null,
            'signing_domain' => null,
            'return_path_domain' => null,
            'merge' => true,
            'merge_language' => 'mailchimp',
            'global_merge_vars' => array(
                array(
                    'name' => 'SUBJECT',
                    'content' => 'News'
                ),
                array(
                    'name' => 'PREHEADER',
                    'content' => 'Message'
                ),
                array(
                    'name' => 'CURRENT_YEAR',
                    'content' => date('Y')
                )
            ),
            'merge_vars' => array(
                array(
                    'rcpt' => 'test1@example.com',
                    'vars' => array(
                        array(
                            'name' => 'merge2',
                            'content' => 'merge2 content'
                        )
                    )
                )
            ),
            //'tags' => array('password-resets'),
            'subaccount' => 'xxxxx',
            'google_analytics_domains' => array('example.com'),
            'google_analytics_campaign' => 'message.from_email@example.com',
            'metadata' => array('website' => 'www.example.com'),
            'recipient_metadata' => array(
                array(
                    'rcpt' => 'test@example.com',
                    'values' => array('user_id' => 123456)
                )
            ),
            'attachments' => array(
                array(
                    'type' => 'text/plain',
                    'name' => 'dateiname.txt',
                    'content' => 'ZXhhbXBsZSBmaWxl'
                )
            )/*,
            'images' => array(
                array(
                    'type' => 'image/png',
                    'name' => 'IMAGECID',
                    'content' => 'iVBORw0KGgoAAAANSUhEUgAAABMAAAANCAIAAADE7sJwAAABrUlEQVQoFQXB0YETMQwE0BlJ9uZyQBv0wAc90QFFc9nYljS8x99//rpdaNCj87xv0Ewt8yrL2vvo5NHauHcjLc+p3GTF8+N63acLfvqgaVCzVYZ20ALIMb+NeXquyl2vVwOhpdjNEQ/MWbu01tFRrRJ3nbZGqQTWKckGunhaVUJYKIsRxuDlnSWrRAaUvfddu4lCj8zj0+PrK8/7oFvK2ClvjavUXUqqZlgxWQ8+9ZAq0TvTkYXphAUiWTOChGW37fu+z40+cHSZw2g0GzTzz4+TvXcqwSfXkgIRwxje1WE04V3Jdqfa3YpHZWD1GFaYLrBfmxC4I8K6RR0Lm3PSR6tf77Xet4sm+Hx67QTd6PQxPLfXYfz7qjCZjpqXe6BX2rDTRFGyIGIatvbpEEH68+Pac/iPn7+Gu4HTY0xdQYfMBkgS1gbputzhO7tW5ekYYUBU4n4XpxnK2e7x/XNeVc+lxthLbRIMxqg63QWt1zJHWCizNZSnHTSXSUHGY0g2J45QO0/OYf5moYomn58xzC/ndIvBBvdR6ZBwWjXfZ++KxzWH+50mNlWM6zHiPyuXXR8ZKkjEAAAAAElFTkSuQmCC'
                )
            )*/
        );
        $async = true;
        $ip_pool = '';
        $send_at = null;
        $result = $mandrill->messages->sendTemplate($template_name, $template_content, $message, $async, $ip_pool, $send_at);
        print_r($result);
        $return = $result;
    } catch(Mandrill_Error $e) {
        // Mandrill errors are thrown as exceptions
        $return = 'A mandrill error occurred: ' . get_class($e) . ' - ' . $e->getMessage();
    }

}

【问题讨论】:

  • 你能展示剩下的代码吗?到目前为止,您粘贴的内容看起来还不错
  • 我投票搁置这个问题,因为原始发帖人说他们会尝试在没有 Stack Overflow 的情况下解决它。

标签: php arrays api mandrill


【解决方案1】:

在你的循环中你正在这样做:

$to[] = array(
    array(
        'email' => 'first@example.com',
        'name' => 'First Recipient',
        'type' => 'to'
    ),
 );

请记住,这意味着您将所有内容都推送到 $to 数组中。但是这里你推送的是一个两级数组,实际上你只想推送一个级别:

$to[] = array(
    'email' => 'first@example.com',
    'name' => 'First Recipient',
    'type' => 'to',
 );

Mandrill 无法理解你在做什么,因为它看到了这个:

array(
    array(
        array(
            'email' => 'first@example.com',
            'name' => 'First Recipient',
            'type' => 'to'
        ),
    ),
    array(
        array(
            'email' => 'second@example.com',
            'name' => 'Second Recipient',
            'type' => 'to'
        )
    ),
)

太多关卡了!

【讨论】:

  • 一半,谢谢!我以前也是这样,但是没用
  • 恐怕“没有/没有工作”没有给我任何回应,@suntrop。 (我建议你不要在这里使用这个短语——我们经常说它是信息量最少的故障报告!)。尝试在循环之后使用print_r() 来显示结构,然后再将其传递给 Mandrill,并将其编辑到您的问题中。
  • 对不起,我会努力解决的。不管怎样,谢谢你的帮助。
  • @suntrop,不,不要放弃你的问题。既然您已经决定寻求帮助,我正在尝试帮助您编写一个包含足够信息的问题来解决它。第一步是向我们展示循环完成后数组的样子。请编辑您的问题。
  • 你的收件人真的是something@example.com吗?它们不起作用,原因很明显(example.com 是一个测试域,可能没有 MX 域记录)。
猜你喜欢
  • 2013-07-26
  • 1970-01-01
  • 2018-12-20
  • 1970-01-01
  • 1970-01-01
  • 2016-04-21
  • 2015-12-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多