Page 1 of 1

Customizing the very last part of my HTML message. Help!

Posted: Mon Apr 16, 2007 4:09 pm
by bruno_m
Hi,

As indicated on my previous post, I am using swiftmailer to send HTML emails to my list of about 800 emails.
I am already able to send them out no problem, but I'm having issues with the customization of part of the message. I took a look at the Decorator plugin as suggested by d11wtq but I still need a little help.

This is my message as it stands right now. I'm creating the message body outside of the loop as it will be the same to everyone (thanks d11wtq for the hint) except the link at the bottom.

Code: Select all

require_once "../tools/Swift-3.1.2-php5/lib/Swift.php";
require_once "../tools/Swift-3.1.2-php5/lib/Swift/Connection/SMTP.php";
 
//Start Swift
$smtp =& new Swift_Connection_SMTP("mail.myserver.com", 587);
$smtp->setUsername("mylogin");
$smtp->setpassword("mypw");
 
$swift =& new Swift($smtp);

$result = mysql_query("SELECT recipient_email FROM common_recipients WHERE list_id=$list_id");

// build email message
$html_body = "<strong> message here </strong>";
$text_body = "message here";

//Create the message
$message =& new Swift_Message(urldecode($message_subject));
//Add some "parts"
$message->attach(new Swift_Message_Part($html_body, "text/html"));
$message->attach(new Swift_Message_Part($text_body, "text/plain"));

while ($row = mysql_fetch_array ($result)){ 
        //Now check if Swift actually sends it
        if ($swift->send($message, urldecode($row['recipient_email']), urldecode($message_from_email))) echo "Sent";
        else echo "Failed";

}
This works fine, but at the end of the message I need to include a link as:

Code: Select all

// create Unsubscribe link at the end of every message sent
$unsubscribe = '<div align="center"><font size="1" face="Verdana, Arial, Helvetica, sans-serif"> Click <a href="http://www.mysite.com/unsubscribe.php?email='.$row['recipient_email'].'">here </a>to unsubscribe from our mailing list </font></div>';
The $row['recipient_email'] field is retrieved from the same loop that sends each message as indicated above.
I tried using the $message->attach(new Swift_Message_Part($body, "text/html")); to attach another part on my email but it doesn't work

Any ideas?
thanks.
[/b]

Posted: Mon Apr 16, 2007 4:17 pm
by Chris Corbyn
For such a basic replacement I'd probably just by-pass using the decorator and do this:

Code: Select all

require_once "../tools/Swift-3.1.2-php5/lib/Swift.php"; 
require_once "../tools/Swift-3.1.2-php5/lib/Swift/Connection/SMTP.php"; 
  
//Start Swift 
$smtp =& new Swift_Connection_SMTP("mail.myserver.com", 587); 
$smtp->setUsername("mylogin"); 
$smtp->setpassword("mypw"); 
  
$swift =& new Swift($smtp); 

$result = mysql_query("SELECT recipient_email FROM common_recipients WHERE list_id=$list_id"); 

// build email message 
$html_body = "<strong> message here </strong>
{UNSUBSCRIBE}";
 
$text_body = "message here
{UNSUBSCRIBE}"; 

//Store these in objects first
$htmlPart =& new Swift_Message_Part($html_body, "text/html");
$textPart =& new Swift_Message_Part($text_body, "text/plain");

//Create the message 
$message =& new Swift_Message(urldecode($message_subject));
 
//Add the already created parts (this is done by-reference internally)
$message->attach($htmlPart); 
$message->attach($textPart); 

while ($row = mysql_fetch_array ($result)){ 
        //Replace content in the $parts, this will still affect $message due to the references
        $htmlPart->setBody(str_replace("{UNSUBSCRIBE}", $row["whatever"], $html_body));
        $textPart->setBody(str_replace("{UNSUBSCRIBE}", $row["whatever"], $text_body));

        //Now check if Swift actually sends it 
        if ($swift->send($message, urldecode($row['recipient_email']), urldecode($message_from_email))) echo "Sent"; 
        else echo "Failed"; 

}