2013-06-25 7 views
6

मैंने मंड्रिल में एक टेम्पलेट बनाया है लेकिन मुझे नहीं पता कि ईमेल भेजने के लिए उपयोग कैसे करें।टेम्पलेट के साथ ईमेल भेजें (मैनरिल 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 का उपयोग करके ईमेल टेम्पलेट भेजने के लिए और उपयोग कर सकते हैं:

यहाँ कैसे एक साधारण एचटीएमएल के साथ इसका इस्तेमाल करने की एक उदाहरण है।

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, जो आप SwiftMailer को पारित कर सकते हैं दे देंगे रेंडर करने के लिए, लेकिन यह एक का एक सा लगता है PHP एपीआई रैपर की तुलना में इसे करने का लंबा हवादार तरीका।

+0

धन्यवाद, यह काम कर रहा है, बस एक सुधार: $ mandrill = नया मंड्रिल ('YOUR_API_KEY'); – Marckaraujo

+0

एक और जानकारी, मैंने सेट किया है * | pspReference | * HTML टेम्पलेट के अंदर और merge_vars में मैंने उपयोग किया: 'name' => 'pspReference', 'content' => 'हैलो वर्ल्ड'), लेकिन यह नहीं बदलता मूल्य, क्या आप जानते हैं क्यों? धन्यवाद – Marckaraujo

+4

@Marckaraujo क्या आप दिखा सकते हैं कि आपका टेम्पलेट कैसा दिखता है? – MrCode

संबंधित मुद्दे