使用 Mandrill 发送电子邮件的简单 php 函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14473592/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 07:21:13  来源:igfitidea点击:

Simple php function to send an email with Mandrill

phpmailchimpmandrill

提问by Jeremy

What is the easiest way to send an email via Mailchimp's Mandrill service (using the API).

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

Here's the send method: https://mandrillapp.com/api/docs/messages.html#method=send

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

Here's the API wrapper: https://bitbucket.org/mailchimp/mandrill-api-php/src/fe07e22a703314a51f1ab0804018ed32286a9504/src?at=master

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

But I can't figure out how to make an PHP function that will send and email via Mandrill.

但我不知道如何制作一个通过 Mandrill 发送和发送电子邮件的 PHP 函数。

Can anyone help?

任何人都可以帮忙吗?

回答by Kaitlin

We also have an official API wrapper for PHP, which is available on Bitbucketor via Packagist, which wraps the Mandrill API for you.

我们还有一个官方的 PHP API 包装器,可在 Bitbucket或通过Packagist 获得,它为您包装了 Mandrill API。

If your Mandrill API key is stored as an environment variable, here's a simple example of sending using a template, with some merge variables and metadata:

如果您的 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' => '[email protected]',
    'html' => '<p>this is a test message with Mandrill\'s PHP wrapper!.</p>',
    'to' => array(array('email' => '[email protected]', 'name' => 'Recipient 1')),
    'merge_vars' => array(array(
        'rcpt' => '[email protected]',
        '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));

?>

回答by MrCode

Mandrill take HTTP POSTrequests for all of their API methods, and they take your input as a JSON string. Here's a basic example of sending an email. It uses cURLto do the HTTP request:

MandrillPOST为所有 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": "[email protected]",
    "from_name": "John",
    "to": [
        {
            "email": "[email protected]",
            "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;

回答by Muddasir Abbas

// 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 = "[email protected]";
$message->from_name  = "From Name";
$message->to = array(array("email" => "[email protected]"));
$message->track_opens = true;

$response = $mandrill->messages->send($message);

回答by 0x1ad2

This is the most basic piece of code I could give you, I just craft it seconds ago for a client and it's working smooth.

这是我可以给你的最基本的一段代码,我只是在几秒钟前为一个客户制作了它,它运行得很顺利。

require_once 'path/to/your/mandrill/file/Mandrill.php';
 try {
    $mandrill = new Mandrill('your-API-key');
    $message = array(
        'html' => $htmlMessage,
        'subject' => $subject,
        'from_email' => $fromEmail,
        'from_name' => $fromName,
        'to' => array(
            array(
                'email' => $toEmail,
                'name' =>  $toName,
                'type' => 'to'
            )
        )
    );
    $result = $mandrill->messages->send($message);
    print_r($result);
} catch(Mandrill_Error $e) {
    echo 'A mandrill error occurred: ' . get_class($e) . ' - ' . $e->getMessage();
    throw $e;
}

Also check their send method for more options like headers, meta-data, attachments etc. https://mandrillapp.com/api/docs/messages.php.html#method-send

还要检查他们的发送方法以获取更多选项,如标题、元数据、附件等。https://mandrillapp.com/api/docs/messages.php.html#method-send

回答by octavian