Send HTML email? no can do!

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
grvulture
Forum Newbie
Posts: 4
Joined: Tue Feb 17, 2009 1:07 pm

Send HTML email? no can do!

Post by grvulture »

Hello!

I am really :banghead: as to why whenever I send an HTML email, it gets delivered as a plain text with html tags, e.g.:
<p>Refer to your Account Setup Information from your hosting company. </p><p>&nbsp;</p><p>If you enter non valid SMTP information, it will be ignored. If you are on a shared hosting, you probably need to setup an SMTP account. </p><p>&nbsp;</p><p><strong>If you dont, some web mail applications like yahoo and gmail will reject your email. Get help by your host!If you dont need to setup SMTP, ignore this! All recipients will receive your email.</strong></p>
delivered like this in various "places" such as MS Outlook, Yahoo, GMail, Hotmail, etc.

while the PHP code I use is valid:

Code: Select all

   $message =& new Swift_Message($title);
    $message->setBody($body, "text/plain");
    $message->attach(new Swift_Message_Part($html_message), "text/html");
please help because I am slowly becoming :crazy: with this
HHahn
Forum Commoner
Posts: 43
Joined: Mon Mar 02, 2009 9:16 am
Location: Veldhoven, Netherlands

Re: Send HTML email? no can do!

Post by HHahn »

I am not so experienced yet in SwiftMailer, but I can tell you how I did it myself, without having the problem you describe. Simplified, it looks like:

Code: Select all

$Msg->setBody($Content, "text/html");
$Msg->addPart(StripHtml ($Content), "text/plain");
Note that for the second "part" I use

Code: Select all

$Msg->addPart();
instead of your

Code: Select all

$message->attach(new Swift_Message_Part($html_message), "text/html");
From your question, it does not seem to be clear whether the message with the "visible" tags as you show it, is actually the HTML part being wrongly displayed, or the TEXT part still containing the tags.

My function StripHtml (), is as follows:

Code: Select all

//------ StripHtml(): -------------------
function StripHtml ($Text)
{
  $T = str_replace  ("&nbsp;",                        " ",  $Text);   // "&nbsp;" to spatie
  $T = str_replace  ("/\r\n/",                        "\n", $T);      // "\r\n" to "\n"
  $T = preg_replace ("/[\x20\x9]*<td[^>]*>[\n]?/",    "\t", $T);      // "<td>" to "\t"
  $T = preg_replace ("/[\x20\x9]*<\/tr[^>]*>[\n]?/",  "\n", $T);      // "</tr>\n" to "\n"
  $T = preg_replace ("/<\/t[drh][^>]*>[\n]?/",        "",   $T);      // remove "</td>", "</tr>", "</th>"
  $T = preg_replace ("/<\/title[^>]*>[\n]?/",         "\n", $T);      // "</title>" to "\n"
  $T = preg_replace ("/<\/table[^>]*>[\n]?/",         "\n", $T);      // "</table>" to "\n"
  $T = preg_replace ("/[\x20\x9]*<table[^>]*>[\n]?/", "",   $T);      // remove "<table>"
  $T = preg_replace ("/[\x20\x9]*<tbody[^>]*>[\n]?/", "",   $T);      // remove "<tbody>"
  $T = preg_replace ("/[\x20\x9]*<thead[^>]*>[\n]?/", "",   $T);      // remove "<thead>"
  $T = preg_replace ("/[\x20\x9]*<tfoot[^>]*>[\n]?/", "",   $T);      // remove "<tfoot>"
  $T = preg_replace ("/<br[^>]*>[\n]?/",              "\n", $T);      // "<br>\n" to "\n"
  $T = preg_replace ("/<\/p[^>]*>[\n]?/",             "\n", $T);      // "</p>\n" to "\n"
  $T = preg_replace ("/<\/h\d[^>]*>[\n]?/",           "\n", $T);      // "</h1>" etc. to "\n"
  $T = preg_replace ("/<\/?b>/",                      "*",  $T);      // "<b>" and "</b>" to "*"
  $T = preg_replace ("/<\/?i>/",                      "/",  $T);      // "<i>" and "</i>" to "/"
  $T = preg_replace ("/<[^>]*>[\n]?/",                "",   $T);      // remove all other HTML-tags
  return ($T);
}    // "StripHtml()"
 
It may not be quite optimised, but I dislike the unreadability of complex regular expressions.
grvulture
Forum Newbie
Posts: 4
Joined: Tue Feb 17, 2009 1:07 pm

Re: Send HTML email? no can do!

Post by grvulture »

It is the html part for sure, because the text part is something totally different (for testing purposes).

I uploaded the new SwiftMailer version, and it works like you say, but I want to use my old version for various reasons, most important ones are:
a: I want to log the mailing process and setLogLevel
b: I want to use disk caching
c: I want to
setTo("undisclosed-recipients:;") and have a variable
$recipients = new Swift_RecipientList(); and add recipients by
$recipients->addTo($email)
and then send my message to these $recipients, while the To: field stays "undisclosed-recipients:"
all these are not possible with the new version. If they are, please somebody explain how, because the new documentation only scratches the surface of what the old documentation used to be...

also the new documentation does not make it clear whether by the end of the script, the SMTP connection gets disconnected or not.
HHahn
Forum Commoner
Posts: 43
Joined: Mon Mar 02, 2009 9:16 am
Location: Veldhoven, Netherlands

Re: Send HTML email? no can do!

Post by HHahn »

Your points a and b are beyond my experience so far.

As for c: As far as I know, the "undisclosed-recipients:;" seems to be a typical MS Outlook construct. I woukd rather use something like "nobody@yourdomain.xy".
SwiftMailer (the 4.0.3 version anyway; I am not sure about the earlier ones) seems to rather strictly check for RFI compliance. So you may have done something that did pass earlier, less strict test but not the newer ones?

Did you try the alternative way to add the second part?

For the remainder, I am afraid you will have to rely on someone else's experience.
Post Reply