简单的PHP函数发送一封电子邮件,山魈(Simple php function to send an

2019-07-18 14:01发布

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

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

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

但我无法弄清楚如何使一个PHP函数,将通过山魈发送和电子邮件。

任何人都可以帮忙吗?

Answer 1:

我们也有PHP官方API包装,这是可以的到位桶或通过Packagist ,它包装的山魈API为您服务。

如果您的山魈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));

?>


Answer 2:

山魈取HTTP POST他们所有的API方法的要求,他们把你的输入作为一个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;


Answer 3:

// 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);


Answer 4:

这是最基本的一段代码,我可以给你,我只是手艺它秒前​​一个客户端,它的工作顺利。

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;
}

同时检查他们的发送方法为更多选项,如标题,元数据,附件等https://mandrillapp.com/api/docs/messages.php.html#method-send



Answer 5:

包括PHP API: https://bitbucket.org/mailchimp/mandrill-api-php

代码: https://mandrillapp.com/api/docs/messages.php.html#method-send

您可以使用ZF的自动装填了包括包装类或作曲: https://mandrillapp.com/api/docs/index.php.html



文章来源: Simple php function to send an email with Mandrill