Personalized batch, but with html formatting

Swift Mailer is a fantastic library for sending email with php. Discuss this library or ask any questions about it here.

Moderators: Chris Corbyn, General Moderators

Post Reply
michiel
Forum Newbie
Posts: 3
Joined: Tue Feb 05, 2008 8:27 am

Personalized batch, but with html formatting

Post by michiel »

Hi, I'm new to Swiftmailer but the library looks very promissing!

I'm trying now to combine two different turorials, but somehwhere in the middle I get stuck :S

I'd like to send a batch of personalized e-mail messages, but I also want the message to be in html format.

This is what I got so far. It sends the messages perfectly, but doesn't personalize them.

Code: Select all

 
require_once "lib/Swift.php";
require_once "lib/Swift/Connection/SMTP.php";
require_once "lib/Swift/Plugin/Decorator.php";
 
$log =& Swift_LogContainer::getLog();
$log->setLogLevel(4);  
 
//Instantiate Swift as usual
$smtp =& new Swift_Connection_SMTP("domain.tld");
$smtp->setUsername("****");
$smtp->setPassword("****");
 
$swift =& new Swift($smtp);
        
$message =& new Swift_Message($_POST['subject']);
 
$template = '<html><body><img src="'.$message->attach(new Swift_Message_Image(new Swift_File("./images/header.jpg"))).'" />';
$template .= $_POST['content'];
$template .= '</body></html>';
        
$message->attach(new Swift_Message_Part($template, "text/html"));       
 
//Specify the list of replacements as a 2-d array
$replacements = array(
    "foo@bar.com" => array("{firstname}" => "Foo", "{lastname}" => "Bar"),
    "bar@foo.com" => array("{firstname}" => "Bar", "{lastname}" => "Foo"),
);
 
//Load the plugin with these replacements
 $swift->attachPlugin(new Swift_Plugin_Decorator($replacements), "decorator");
 
//Send messages
$recipients =& new Swift_RecipientList();
$recipients->addTo("foo@bar.com");
$recipients->addTo("bar@foo.com");
//Send with batchSend()
$swift->batchSend($message, $recipients, "my@domain.com");
 
$swift->disconnect();
 
echo $log->dump(true);
 
Any help or pointers would be greatly appriciated!

CheerMichiel
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: Personalized batch, but with html formatting

Post by Chris Corbyn »

What you have doesn't work? Looks right to me, although I don't see the content where the replacements will be made, so I assume that's in $_POST['content']?
michiel
Forum Newbie
Posts: 3
Joined: Tue Feb 05, 2008 8:27 am

Re: Personalized batch, but with html formatting

Post by michiel »

Hi,

I've got it working. I made a stupid error, using capital letters for the parts to be replaced in $_POST, while defining them lower case in the script.

So I can now confirm the script is working properly, if anybody else needs something like this.

Thanks for the quick reply (and writing this great library!)

Michiel
Post Reply