2013-06-25 6 views
6

私はMandrillでテンプレートを作成しましたが、メールの送信方法はわかりません。テンプレートを使ってメールを送信する(Mandrill PHP)

<?php 

include_once "swift_required.php"; 

$subject = 'Hello from Mandrill, PHP!'; 
$from = array('[email protected]' =>'Your Name'); 
$to = array(
'[email protected]' => 'Recipient1 Name', 
'[email protected]' => 'Recipient2 Name' 
); 

$text = "Mandrill speaks plaintext"; 
$html = "<em>Mandrill speaks <strong>HTML</strong></em>"; 

$transport = Swift_SmtpTransport::newInstance('smtp.mandrillapp.com', 587); 
$transport->setUsername('MANDRILL_USERNAME'); 
$transport->setPassword('MANDRILL_PASSWORD'); 
$swift = Swift_Mailer::newInstance($transport); 

$message = new Swift_Message($subject); 
$message->setFrom($from); 
$message->setBody($html, 'text/html'); 
$message->setTo($to); 
$message->addPart($text, 'text/plain'); 

if ($recipients = $swift->send($message, $failures)) 
{ 
echo 'Message successfully sent!'; 
} else { 
echo "There was an error:\n"; 
print_r($failures); 
} 

?> 

答えて

15

あなたは電子メールを送信してMandrill PHP API wrapperを使用して、テンプレートを使用することができます。ここでは

は、単純なHTMLでそれを使用する方法の例です。あなたはではSwiftMailer経由でSMTPを使用する場合は

require 'Mandrill.php'; 

$mandrill = new Mandrill('YOUR_API_KEY'); 

$message = array(
    'subject' => 'My subject', 
    'from_email' => '[email protected]', 
    'to' => array(array('email' => '[email protected]', 'name' => 'Marc')), 
    '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 = 'YOUR-TEMPLATE-NAME'; 

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

); 

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

、あなたはあなたがではSwiftMailerに渡すことができる完全なHTMLを与えるテンプレートを、レンダリングするためにAPIのRenderメソッドを呼び出すことができますが、それは少しですPHP APIラッパーと比較して、それをやり遂げるのに長年慣れてきました。

+0

ありがとう、働いているだけです。$ mandrill = new Mandrill( 'YOUR_API_KEY'); – Marckaraujo

+0

もう1つの情報、私は* htmlテンプレートの中に* pspReference | *を設定しました。そしてmerge_varsには 'name' => 'pspReference'、 'content' => 'Hello world'価値、あなたはなぜ知っていますか?ありがとう – Marckaraujo

+4

@Marckaraujoはあなたのテンプレートがどのように見えるかを表示できますか? – MrCode

関連する問題