Skip to content

Instantly share code, notes, and snippets.

@pedrokoblitz
Created July 31, 2015 21:30
Show Gist options
  • Save pedrokoblitz/2761f3b99fbb546196a1 to your computer and use it in GitHub Desktop.
Save pedrokoblitz/2761f3b99fbb546196a1 to your computer and use it in GitHub Desktop.
<?php
// template
// Usually you want to replace the following static array
// with a dynamic one built retrieving users from the database
$users = array(
array(
"fullname" => "Aurelio De Rosa",
"operations" => 100,
"email" => "aurelioderosa@gmail.com"
),
array(
"fullname" => "Audero",
"operations" => 50,
"email" => "info@audero.it"
)
);
// Create the replacements array
$replacements = array();
foreach ($users as $user) {
$replacements[$user["email"]] = array(
"{fullname}" => $user["fullname"],
"{transactions}" => $user["operations"]
);
}
// Create the mail transport configuration
$transport = Swift_MailTransport::newInstance();
// Create an instance of the plugin and register it
$mailer = Swift_Mailer::newInstance($transport);
// register decorator plugin
$plugin = new Swift_Plugins_DecoratorPlugin($replacements);
$mailer->registerPlugin($plugin);
// Create the message
$message = Swift_Message::newInstance();
$message->setSubject("This email is sent using Swift Mailer");
$message->setBody("You {fullname}, are our best client ever thanks " .
" to the {transactions} transactions you made with us.");
$message->setFrom("account@bank.com", "Your bank");
// Send the email
foreach($users as $user) {
$message->setTo($user["email"], $user["fullname"]);
$mailer->send($message);
}
// batch
$mailer = Swift_Mailer::newInstance( ... );
$message = Swift_Message::newInstance( ... )
->setFrom( ... )
->setTo(array(
'receiver@bad-domain.org' => 'Receiver Name',
'other@domain.org' => 'A name',
'other-receiver@bad-domain.org' => 'Other Name'
))
->setBody( ... )
;
// Pass a variable name to the send() method
if (!$mailer->send($message, $failures))
{
echo "Failures:";
print_r($failures);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment