Page 1 of 1

text and html version of a message

Posted: Fri Mar 07, 2008 9:05 pm
by yacahuma
Hello,
In phpmailer you can set both a text version and an html version of a message. How can I do this in swift?

Code: Select all

 
  // HTML body
    $body  = "Hello <font size=\"4\">" . $row["full_name"] . "</font>, <p>";
    $body .= "<i>Your</i> personal photograph to this message.<p>";
    $body .= "Sincerely, <br>";
    $body .= "PHPMailer List manager";
 
    // Plain text body (for mail clients that cannot read HTML)
    $text_body  = "Hello " . $row["full_name"] . ", \n\n";
    $text_body .= "Your personal photograph to this message.\n\n";
    $text_body .= "Sincerely, \n";
    $text_body .= "PHPMailer List manager";
 
    $mail->Body    = $body;
    $mail->AltBody = $text_body;
 

Re: text and html version of a message

Posted: Fri Mar 07, 2008 10:05 pm
by yacahuma
I found the answer. sorry for asking before looking to much

Code: Select all

 
$message =& new Swift_Message($newsletter->subject);
$message->attach(new Swift_Message_Part($newsletter->body_txt));
$message->attach(new Swift_Message_Part($newsletter->body_html, "text/html"));
 
I dont need this , right??

Code: Select all

 
$stream =& $message->build();
 

Re: text and html version of a message

Posted: Fri Mar 07, 2008 10:56 pm
by Chris Corbyn
You don't need to build the message no :)

Re: text and html version of a message

Posted: Sat Mar 08, 2008 5:52 am
by yacahuma
is there a difference between

Code: Select all

 
$message =& new Swift_Message($newsletter->body_txt, $newsletter->subject);
 
and this

Code: Select all

 
$message =& new Swift_Message($newsletter->subject);
$message->attach(new Swift_Message_Part($newsletter->body_txt));
 

Re: text and html version of a message

Posted: Sat Mar 08, 2008 6:07 am
by Chris Corbyn
The parameters are in the wrong order in your first snippet ;) There is a difference yes. The first one creates a basic message without any boundaries for additional parts. The second one creates a multipart message with just the one part (between some multipart boundaries).

In v4 the requirement that multipart messages be created in such a different way is no longer an issue, you'll be able to do this:

Code: Select all

$message = $mimeDi->create('message')
  ->setSubject('Some Subject')
  ->setBody('Your preferred body')
  ->attach($mimeDi->create('part')->setBody('Another body'))
  ;
Don't worry about the massively changing interface, it's a side-effect of the way v4 is written, there are also wrapper functions to do this:

Code: Select all

$message = $mimeDi->createMessage('Some Subject', 'Your preferred body')
  ->attach($mimeDi->createPart('Another body'))
  ;
And there are also the original class APIs so you don't have to rewrite old code if you don't like the newer API.