2013-01-23 8 views
25

MailchimpのMandrillサービス(APIを使用)経由で電子メールを送信する最も簡単な方法は何ですか?ここでMandrillで電子メールを送信する簡単なPHP関数

はsendメソッドです:https://mandrillapp.com/api/docs/messages.html#method=send

はここでAPIのラッパーです:https://bitbucket.org/mailchimp/mandrill-api-php/src/fe07e22a703314a51f1ab0804018ed32286a9504/src?at=master

しかし、私はマンドリルを経由して送信し、電子メールしますPHP関数を作成する方法を見つけ出すことはできません。

誰でも手助けできますか?

答えて

59

我々はまた、あなたのためのマンドリルのAPIをラップon BitbucketPackagistを介して利用可能であるPHPの公式APIラッパーを、持っている:それは、HTTPリクエストを行うためにcURLを使用しています。

あなたのマンドリル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)); 

?> 
+6

を参照してください。将来のユーザーのためのメモとして、送信 - >テンプレートメニューの下にあるMandrillでテンプレートを作成する必要があります最小のhtmlは次のようになります。

テンプレートのスラッグは、Stationaryと一致する必要があります。テンプレートの詳細については、http://help.mandrill.com/entries/21694286-How-do-I-add-dynamic-content-using-editable-regions-in-my-template- – Treemonkey

21

マンドリルは、すべてのAPIメソッドについてHTTP POSTリクエストを受け取り、入力をJSON文字列として受け取ります。ここでは、電子メールを送信するための基本的な例があります。

$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; 
+1

あなたはZF2とZF1でこれをテストしましたか? FYIは働いていませんでした。 – YumYumYum

+0

複数の電子メールアドレスのCC、BCCが機能していません – YumYumYum

+0

私のために働きました! –

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

これは私があなたに与えることができる最も基本的なコードです。クライアントのために数秒前に手作業を行い、スムーズに動作します。

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

また、ヘッダのようなより多くのオプションのためのメタデータを自分のsendメソッドを確認するなどの添付ファイルhttps://mandrillapp.com/api/docs/messages.php.html#method-send

関連する問題