简单的PHP功能,用Mandrill发送电子邮件

Jer*_*emy 27 php mailchimp mandrill

通过Mailchimp的Mandrill服务(使用API​​)发送电子邮件的最简单方法是什么?

这是send方法:https://mandrillapp.com/api/docs/messages.html#method=send

这是API包装器:https://bitbucket.org/mailchimp/mandrill-api-php/src/fe07e22a703314a51f1ab0804018ed32286a9504/src? at = master

但我无法弄清楚如何制作一个PHP函数,它将通过Mandrill发送和发送电子邮件.

有人可以帮忙吗?

Kai*_*lin 63

我们还有一个适用于PHP的官方API包装器,它可以在BitbucketPackagist上获得,它包含了Mandrill API.

如果您的Mandrill API密钥存储为环境变量,这里是一个使用模板发送的简单示例,包含一些合并变量和元数据:

<?php
require 'Mandrill.php';

$mandrill = new Mandrill(); 

// If are not using environment variables to specific your API key, use:
// $mandrill = new Mandrill("YOUR_API_KEY")

$message = array(
    'subject' => 'Test message',
    'from_email' => 'you@yourdomain.com',
    'html' => '<p>this is a test message with Mandrill\'s PHP wrapper!.</p>',
    'to' => array(array('email' => 'recipient1@domain.com', 'name' => 'Recipient 1')),
    'merge_vars' => array(array(
        'rcpt' => 'recipient1@domain.com',
        'vars' =>
        array(
            array(
                'name' => 'FIRSTNAME',
                'content' => 'Recipient 1 first name'),
            array(
                'name' => 'LASTNAME',
                'content' => 'Last name')
    ))));

$template_name = 'Stationary';

$template_content = array(
    array(
        'name' => 'main',
        'content' => 'Hi *|FIRSTNAME|* *|LASTNAME|*, thanks for signing up.'),
    array(
        'name' => 'footer',
        'content' => 'Copyright 2012.')

);

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

?>
Run Code Online (Sandbox Code Playgroud)

  • 作为未来用户的注释,您需要在出站 - >模板菜单下的Mandrill中创建一个模板,其中最小的html如下所示:<div mc:edit ="main"> </ div> <div mc:edit ="footer"> </ div>模板slug必须与Stationary匹配.对模板的进一步信息可以在这里找到http://help.mandrill.com/entries/21694286-How-do-I-add-dynamic-content-using-editable-regions-in-my-template- (6认同)

MrC*_*ode 22

Mandrill POST对所有API方法都采用HTTP 请求,并将输入作为JSON字符串.这是发送电子邮件的基本示例.它用于cURL执行HTTP请求:

$uri = 'https://mandrillapp.com/api/1.0/messages/send.json';

$postString = '{
"key": "YOUR KEY HERE",
"message": {
    "html": "this is the emails html content",
    "text": "this is the emails text content",
    "subject": "this is the subject",
    "from_email": "someone@example.com",
    "from_name": "John",
    "to": [
        {
            "email": "blah@example.com",
            "name": "Bob"
        }
    ],
    "headers": {

    },
    "track_opens": true,
    "track_clicks": true,
    "auto_text": true,
    "url_strip_qs": true,
    "preserve_recipients": true,

    "merge": true,
    "global_merge_vars": [

    ],
    "merge_vars": [

    ],
    "tags": [

    ],
    "google_analytics_domains": [

    ],
    "google_analytics_campaign": "...",
    "metadata": [

    ],
    "recipient_metadata": [

    ],
    "attachments": [

    ]
},
"async": false
}';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $uri);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postString);

$result = curl_exec($ch);

echo $result;
Run Code Online (Sandbox Code Playgroud)


Mud*_*bas 8

// Simply Send Email Via Mandrill...

require_once 'Mandrill.php';
$mandrill = new Mandrill($apikey);

$message = new stdClass();
$message->html = "html message";
$message->text = "text body";
$message->subject = "email subject";
$message->from_email = "address@test.com";
$message->from_name  = "From Name";
$message->to = array(array("email" => "recipient@test.com"));
$message->track_opens = true;

$response = $mandrill->messages->send($message);
Run Code Online (Sandbox Code Playgroud)