text and html version of a message

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
User avatar
yacahuma
Forum Regular
Posts: 870
Joined: Sun Jul 01, 2007 7:11 am

text and html version of a message

Post 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;
 
User avatar
yacahuma
Forum Regular
Posts: 870
Joined: Sun Jul 01, 2007 7:11 am

Re: text and html version of a message

Post 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();
 
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: text and html version of a message

Post by Chris Corbyn »

You don't need to build the message no :)
User avatar
yacahuma
Forum Regular
Posts: 870
Joined: Sun Jul 01, 2007 7:11 am

Re: text and html version of a message

Post 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));
 
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: text and html version of a message

Post 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.
Post Reply