使用Mandrill(php)发送电子邮件时出错

时间:2013-10-07 10:34:49

标签: php email mandrill

我第一次使用mandrill api。我正在使用以下代码。 我有Mandrill API密钥。

 <?php
    try {
        $mandrill = new Mandrill('YOUR_API_KEY');
        $message = array(
            'html' => '<p>Example HTML content</p>',
            'text' => 'Example text content',
            'subject' => 'example subject',
            'from_email' => 'message.from_email@example.com',
            'from_name' => 'Example Name',
            'to' => array(
                array(
                    'email' => 'recipient.email@example.com',
                    'name' => 'Recipient Name'
                )
            ),
            'headers' => array('Reply-To' => 'message.reply@example.com'),
            'important' => false,
            'track_opens' => null,
            'track_clicks' => null,
            'auto_text' => null,
            'auto_html' => null,
            'inline_css' => null,
            'url_strip_qs' => null,
            'preserve_recipients' => null,
            'view_content_link' => null,
            'bcc_address' => 'message.bcc_address@example.com',
            'tracking_domain' => null,
            'signing_domain' => null,
            'return_path_domain' => null,
            'merge' => true,
            'global_merge_vars' => array(
                array(
                    'name' => 'merge1',
                    'content' => 'merge1 content'
                )
            ),
            'merge_vars' => array(
                array(
                    'rcpt' => 'recipient.email@example.com',
                    'vars' => array(
                        array(
                            'name' => 'merge2',
                            'content' => 'merge2 content'
                        )
                    )
                )
            ),
            'tags' => array('password-resets'),
            'subaccount' => 'customer-123',
            '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' => 'recipient.email@example.com',
                    'values' => array('user_id' => 123456)
                )
            ),
            'attachments' => array(
                array(
                    'type' => 'text/plain',
                    'name' => 'myfile.txt',
                    'content' => 'ZXhhbXBsZSBmaWxl'
                )
            ),
            'images' => array(
                array(
                    'type' => 'image/png',
                    'name' => 'IMAGECID',
                    'content' => 'ZXhhbXBsZSBmaWxl'
                )
            )
        );
        $async = false;
        $ip_pool = 'Main Pool';
        $send_at = 'example send_at';
        $result = $mandrill->messages->send($message, $async, $ip_pool, $send_at);
        print_r($result);

    } catch(Mandrill_Error $e) {
        echo 'A mandrill error occurred: ' . get_class($e) . ' - ' . $e->getMessage();

        throw $e;
    }
    ?>

通过使用此代码,我收到错误:

发生了mandrill错误:Mandrill_HttpError - 对消息/发送的API调用失败:错误设置证书验证位置:CAfile:/usr/local/share/certs/ca-root-nss.crt CApath:none

为什么我收到此错误?

3 个答案:

答案 0 :(得分:27)

在此文件中:  山魈-API的PHP中\ SRC \ Mandrill.php

在第58行初始化curl:

$this->ch = curl_init();

您需要添加这两个选项来解决问题:

curl_setopt($this->ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($this->ch, CURLOPT_SSL_VERIFYPEER, 0);

或者你有这个选项: HTTPS and SSL3_GET_SERVER_CERTIFICATE:certificate verify failed, CA is OK

答案 1 :(得分:7)

错误表明您没有在本地安装所需的SSL证书来验证与Mandrill API的SSL连接。您可以通过操作系统的软件包管理器获取一组证书,或者您可以下载随Mozilla分发的软件包:http://curl.haxx.se/docs/caextract.html,然后将它们存储在本地。

答案 2 :(得分:4)

http://curl.haxx.se/docs/caextract.html下载cacert.pem并将其放在我的服务器上后,我能够解决此问题(同时保证一切安全),并使用以下内容:

$mandrill = new Mandrill(MANDRILL_API_KEY);

// Fix CA issue
curl_setopt($mandrill->ch, CURLOPT_SSL_VERIFYHOST, true);
curl_setopt($mandrill->ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($mandrill->ch, CURLOPT_CAINFO, 'PATH_TO/cacert.pem');

Mandrill类中的curl属性是公共的,因此无需向库本身添加hack。