我在Mandrill中创建了一个模板,但我不知道如何使用它来发送电子邮件。
以下是如何将其与简单的html一起使用的示例:
<?php
include_once "swift_required.php";
$subject = 'Hello from Mandrill, PHP!';
$from = array('you@yourdomain.com' =>'Your Name');
$to = array(
'recipient1@example.com' => 'Recipient1 Name',
'recipient2@example2.com' => '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);
}
?>
答案 0 :(得分:16)
您可以使用Mandrill PHP API wrapper发送电子邮件并使用模板。
require 'Mandrill.php';
$mandrill = new Mandrill('YOUR_API_KEY');
$message = array(
'subject' => 'My subject',
'from_email' => 'marc@example.com',
'to' => array(array('email' => 'recipient1@example.com', 'name' => 'Marc')),
'merge_vars' => array(array(
'rcpt' => 'recipient1@example.com',
'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使用SMTP,你可以调用渲染API方法渲染一个模板,它会给你完整的HTML,你可以传递给SwiftMailer,但这似乎是一个漫长的方式与PHP API包装器相比,这样做。