使用mandrill在php中发送电子邮件

时间:2014-09-17 08:08:58

标签: php email mandrill

我是mandrill的新手,我正在使用mandrill在我的php应用程序中发送邮件。我也在mandril注册了我的域名。我得到了代码在php中发送mandrill邮件。我尝试了它的工作。但我不明白如何在不指定代码中的来自邮件地址的情况下发送邮件。这就是'来自'邮件地址应该是我设置的默认值。

我搜索了很多,甚至在mandrill但是我找不到任何答案。任何人都可以帮我这个.. ??

我使用了这段代码:

<?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', //this address should be my default one
    'from_name' => 'Example Name',
    'to' => array(
        array(
            'email' => 'recipient.email@example.com',
            'name' => 'Recipient Name',
            'type' => 'to'
        )
    ),
    '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);
/*
Array
(
    [0] => Array
        (
            [email] => recipient.email@example.com
            [status] => sent
            [reject_reason] => hard-bounce
            [_id] => abc123abc123abc123abc123abc123
        )

)
*/
} catch(Mandrill_Error $e) {
// Mandrill errors are thrown as exceptions
echo 'A mandrill error occurred: ' . get_class($e) . ' - ' . $e->getMessage();
// A mandrill error occurred: Mandrill_Unknown_Subaccount - No subaccount exists with the id 'customer-123'
throw $e;
}
?>

1 个答案:

答案 0 :(得分:0)

您无法设置默认的from_address。每次通过API调用都必须包含它。

您的域名是其他内容,通过添加域名,您可以将其添加到可以通过API与您的Mandrill帐户对话的域名白名单中。