...

View Full Version : Maintaining html styles in php mail form



hothousegraphix
12-21-2006, 02:20 AM
Please be gentle. I have very little experience with php.

That said. I've created a simple mail form with associated script that validates three fields (name, e-mail, comments) and sends out two html formatted e-mails; one to the form owner, and one to the form user.

All pretty straight forward. The form works just fine. All validations and error messages execute as expected. The trouble I'm running into is with maintaining the html formatting (more specifically, styles) spelled out in the server-side generated e-mails.

What seems to be happening is that the escapes I'm placing prior to my double quotes (in my php) are eliminating two characters from the defined style which screws up the formatting. Below is an example of the resulting source from the generated e-mail; e.g.

<font size=" face=elvetica, Geneva, Arial, SunSans-Regular, sans-serif">Michael, thank you for your e-mail<br />
We will respond as soon as possible.<br /></font>As you can see the size definition is eliminated (size="3") and the H from Helvetica is also not being written. Below is the part of my PHP function that writes the e-mail; e.g.
$confEmailBody = "--BOUNDRY\n"
. "Content-Type: text/plain; charset:\"ISO-8859-1\"\n"
. "Content-Transfer-Encoding: quoted-printable\n"
. "\n"
. "name: $name\n"
. "email: $email\n"
. "comments: $comments\n"
. ""
. "\n"
. "--BOUNDRY\n"
. "Content-Type: text/html; charset:\"ISO-8859-1\"\n"
. "Content-Transfer-Encoding: quoted-printable\n"
. "\n"
. "<HTML>\n"
. "<head>\n"
. "<title>Inquiry/Comments Received</title>\n"
. "</head>\n"
. "<body>\n"
. "<p><font size=\"3\" face=\"Helvetica, Geneva, Arial, SunSans-Regular, sans-serif\">$name, thank you for your e-mail<br />\n"
. "We will respond as soon as possible.<br /></font>\n"
. "<br /> \n"
. "Regards,<br />\n"
. "<br />\n"
. "<font size=\"3\" color=\"#566f88\" face=\"Helvetica, Geneva, Arial, SunSans-Regular, sans-serif\"><b>The company name team</b><br /><br /></font>\n"
. "\n"
. "<font size=\"3\"><br /></font><font size=\"2\" color=\"#666666\" face=\"Helvetica, Geneva, Arial, SunSans-Regular, sans-serif\">==================================================================== <br />\n"
. "This e-mail is in response to a submission to www.thecompanyname.com<b>.</b><br />\n"
. "Sent from: $email, at IP: " . $_SERVER['REMOTE_ADDR'] . "<br />\n"
. "<br />\n"
. "If you feel you've received this e-mail in error, please contact us at <b>www.thecompanyname.com</b><br /></font></p>\n"
. "</body>\n"
. "</html>"
. "\n"
. "--BOUNDRY--";Every other aspect functions properly. All the formatting writes as intended; i.e. line breaks, p tags, etc.

I'm loosing those few characters in the style definitions which is causing the generated e-mail to look different than intended.

Any advice on how to address???????????

Thanks

mlseim
12-21-2006, 10:07 AM
Instead of escaping all of those double-quotes, you could use
single quotes like this example line:

"<font size='3' color='#566f88' face='Helvetica, Geneva, Arial, SunSans-Regular, sans-serif'><b>The company name team</b><br /><br /></font>\n"

In fact, you could do this:

$confEmailBody = "
--BOUNDRY\n
Content-Type: text/plain; charset:'ISO-8859-1'\n
Content-Transfer-Encoding: quoted-printable\n
\n
name: $name\n
email: $email\n
comments: $comments\n
\n
--BOUNDRY\n
Content-Type: text/html; charset:'ISO-8859-1'\n
Content-Transfer-Encoding: quoted-printable\n
\n
<HTML>\n
<head>\n
<title>Inquiry/Comments Received</title>\n
</head>\n
<body>\n
<p><font size='3' face='Helvetica, Geneva, Arial, SunSans-Regular, sans-serif'>$name, thank you for your e-mail<br />\n
We will respond as soon as possible.<br /></font>\n
<br />\n

.
.the rest of it here with all single quotes
.

</body>\n
</html>
\n
--BOUNDRY--
";

hothousegraphix
12-21-2006, 02:19 PM
Thanks for the suggestion.

I actually tried using single quotes; however, I was not doing it in the manner you suggested. I did the following; e.g.
'--FTG_BOUNDRY\n'
. 'Content-Type: text/html; charset:"ISO-8859-1"\n'
. 'Content-Transfer-Encoding: quoted-printable\n'
. '\n'
. '<HTML>\n'
. '<head>\n'
. '<title>soundcake contact form</title>\n'
. '</head>\n'
. '<body>\n'
. '<p style="font-family:Arial; font-size:12px">An e-mail has been sent via thesoundcake.com:<br /><br />\n'
This did write the document but all formatting was lost and each variable name was written not the variable value.

One question, should your suggestion (use of single quotes) be only applied to the portion of the script that writes the e-mail?

Thanks for taking the time to assist!

Regards.

Tyree
12-21-2006, 02:40 PM
I personally use single quote whenever doing large html strings. It's just 100 times easier than escaping a ton of double-quotes. Just use that as your rule of thumb. It's really just whatever is easiest and cleanest.

I would use single-quotes the way you have though, not the way mlseim suggested. While vars will not parse within single-quotes, it's still far less work to wrap the string in single-quotes and just break out any vars within the string. Like this:


$mystring = '<p class="someclass">This is my string text with a ' . $variable . ' in it.</p>';


Eezy-Peezy :)

mlseim
12-21-2006, 04:54 PM
Tyree ...

QUOTE: "While vars will not parse within single-quotes"

I didn't really think of that. Glad you brought it up.

hothousegraphix
12-26-2006, 06:51 AM
Update and question:

After testing a number of suggestions from using the single quote approach, to defining a here doc I found a solution to my situation. For what ever reason, none of the suggested solutions resolved my situation.

After much tinkering, I discovered that if I used an escape plus a space after the initial "=" in my html formatting definition (along with single quotes within the definition); the formatting printed as intended. e.g.
"<font size=\ '3' color=\ '#7397AE' face=\ 'Helvetica, Geneva, Arial, SunSans-Regular, sans-serif'><b>The xyz team</b><br /></font>\n"

I'm wondering if anyone has an idea why this is the case?

Thanks for the assistance!

Tyree
12-26-2006, 01:27 PM
There's absolutely no reason to escape single-quotes within a string wrapped in double-quotes.

There's also no reason why this:


'<font size="3" color="#7397AE" face="Helvetica, Geneva, Arial, SunSans-Regular, sans-serif"><b>The xyz team</b><br /></font>' . "\n";


can't replace what you just posted.
Here's an explanation:
http://www.tyreeonline.com/tests/single-double.php

mlseim
12-26-2006, 06:30 PM
Tyree ...

Great Explanation. It's hard to explain with words alone.

I just don't think I understand what the problem really is ...
as though I'm missing something in the posts.

Tyree
12-27-2006, 01:38 AM
I guess I don't quite understand the problem myself. This should be one of the easiest parts of php...simple string defining.

Why his original string didn't work, I don't know for sure. But, I know the subsequent ideas, including heredoc, should work. They eliminate the need for the escapes and hopefully that erases his original problem too.

hothousegraphix
12-27-2006, 01:44 AM
Tyree,

Thanks for the explanation. I do understand what you are saying and I've, in fact, attempted the suggested steps to resolve and recover the lost characters from my formatting. Unfortunately, the results print a continues string of the text and it's formatting to my e-mail.

To address mlseims point this script is firing off an html formated e-mail. The results do not appear within a browser. The results print to an e-mail viewed from any number of agents each of which handle formatting differently.

Tyree
12-27-2006, 01:57 AM
Yeah, I tested your original message in a browser...works fine using multiple methods of defining the string.
http://www.tyreeonline.com/tests/single-double.php

Would you mind posting the rest of your email code? It's definitely not a problem with the way you're defining the string. Has to be something else...especially if you've tested it with multiple email clients as well.

hothousegraphix
12-27-2006, 02:02 AM
Tyree,

Below is the code I'm using, stripped of the specific addresses and paths to image references.

The script is working fine and produces the desired results. Both as plian text and html.

Thanks for hanging in there with me and my inexperience with PHP.

<?PHP

#----------
# Validate: String

function check_string($value, $low, $high, $mode, $optional)
{
if ( (strlen($value) == 0) && ($optional === true) ) {
return true;
} elseif ( (strlen($value) >= $low) && ($mode == 1) ) {
return true;
} elseif ( (strlen($value) <= $high) && ($mode == 2) ) {
return true;
} elseif ( (strlen($value) >= $low) && (strlen($value) <= $high) && ($mode == 3) ) {
return true;
} else {
return false;
}
}
#----------
# Validate: Email

function check_email($email, $optional)
{
if ( (strlen($email) == 0) && ($optional === true) ) {
return true;
} elseif ( ereg("[A-Za-z0-9_-]+\@[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+", $email) ) {
return true;
} else {
return false;
}
}

# RegisterGlobals OFF

$USERcmd = $_POST['cmd'];
$USERname = $_POST['name'];
$USERemail = $_POST['email'];
$USERcomments = $_POST['comments'];

# Fields Validations

$validationFailed = false;

if ( (! check_string($USERname, 4, 0, 1, false))) {
$validationFailed = true;
$errorList .= 'Please enter a contact name' . '<BR>';
}

if ( (! check_email($USERemail, false))) {
$validationFailed = true;
$errorList .= 'Please enter a valid e-mail address' . '<BR>';
}

if ( (! check_string($USERcomments, 10, 0, 1, false))) {
$validationFailed = true;
$errorList .= 'Please complete the comments field' . '<BR>';
}

# Embed error page and dump it to the browser

$fileErrorPage = 'contact_Error.html';

if ($validationFailed == true) {

if (file_exists($fileErrorPage) === false) {
echo "The error page: <b>contact_Error.html</b> cannot be found on the server.";
exit;
}

$fileHandle = fopen ($fileErrorPage, "r");
$errorPage = fread ($fileHandle, filesize($fileErrorPage));
fclose ($fileHandle);

$errorPage = str_replace('<!--VALIDATIONERROR-->', $errorList, $errorPage);

$errorPage = str_replace('<!--FIELDVALUE:cmd-->', $USERcmd, $errorPage);
$errorPage = str_replace('<!--FIELDVALUE:name-->', $USERname, $errorPage);
$errorPage = str_replace('<!--FIELDVALUE:email-->', $USERemail, $errorPage);
$errorPage = str_replace('<!--FIELDVALUE:comments-->', $USERcomments, $errorPage);
$errorPage = str_replace('<!--ERRORMSG:name-->', 'Please enter a contact name', $errorPage);
$errorPage = str_replace('<!--ERRORMSG:email-->', 'Please enter a valid e-mail address', $errorPage);
$errorPage = str_replace('<!--ERRORMSG:comments-->', 'Please complete the comments field', $errorPage);

echo $errorPage;
exit;

}


# Email to Form Owner

$emailTo = '"soundcake" <FormOwner@theCompanyName.com.com>';

$emailSubject = "Inquiry Received";

$emailHeader = "From: $USERemail\n"
. "Reply-To: $USERemail\n"
. "MIME-Version: 1.0\n"
. "Content-Type: multipart/alternative; boundary=\"USER_BOUNDRY\"\n"
. "X-Sender: $USERemail\n"
. "X-Mailer: PHP\n"
. "X-Priority: 3\n"
. "Return-Path: $USERemail\n"
. "\n";

$emailBody = "--USER_BOUNDRY\n"
. "Content-Type: text/plain; charset:\"ISO-8859-1\"\n"
. "Content-Transfer-Encoding: quoted-printable\n"
. "\n"
. "An inquiry/comment was made to www.theCompanyName.com by:\n"
. "Name: $USERname\n"
. "E-mail: $USERemail\n"
. "\n"
. "Message details:\n"
. "Date: " . date('m/d/Y') . "\n"
. "Time: " . date('h:i:s A') . "\n"
. "IP: " . $_SERVER['REMOTE_ADDR'] . " \n"
. "\n"
. "Information:\n"
. "Comments: $USERcomments\n"
. "\n"
. ""
. "\n"
. "--USER_BOUNDRY\n"
. "Content-Type: text/html; charset:\"ISO-8859-1\"\n"
. "Content-Transfer-Encoding: quoted-printable\n"
. "\n"
. "<HTML>\n"
. "<head>\n"
. "<title>Inquiry Received</title>\n"
. "</head>\n"
. "<body>\n"
. "<img src=\ 'http://LinkToImage/Logo.png' alt=\ '' height=\ '77' width=\ '116' border=\ '0'>\n"
. "<p><font size=\ '3' face=\ 'Helvetica, Geneva, Arial, SunSans-Regular, sans-serif'>An inquiry/comment was made to www.theCompanyName.com by:<br />\n"
. "<strong>Name:</strong> $USERname<br />\n"
. "<strong>E-mail:</strong> $USERemail<br />\n"
. "<br />\n"
. "<strong>Message details:</strong> <br />\n"
. "<hr align=\ 'left' noshade size=\ '1' width=\ '420' color=\ '000'>\n"
. "<strong>Date:</strong> " . date('m/d/Y') . " <br />\n"
. "<strong>Time:</strong> " . date('h:i:s A') . " <br />\n"
. "<strong>IP:</strong> " . $_SERVER['REMOTE_ADDR'] . " <br />\n"
. "<br />\n"
. "<strong>Comments:</strong> <br />\n"
. "$USERcomments</font></p>\n"
. "</body>\n"
. "</html>\n"
. "\n"
. ""
. "\n"
. "--USER_BOUNDRY--";

mail($emailTo, $emailSubject, $emailBody, $emailHeader);

# Confirmation Email to User

$confEmailTo = $USERemail;

$confEmailSubject = "Received";

$confEmailHeader = "From: info@theCompanyName.com\n"
. "Reply-To: info@theCompanyName.com\n"
. "MIME-Version: 1.0\n"
. "Content-Type: multipart/alternative; boundary=\"USER_BOUNDRY\"\n"
. "X-Sender: info@theCompanyName.com\n"
. "X-Mailer: PHP\n"
. "X-Priority: 3\n"
. "Return-Path: info@theCompanyName.com\n"
. "\n";

$confEmailBody = "--USER_BOUNDRY\n"
. "Content-Type: text/plain; charset:\"ISO-8859-1\"\n"
. "Content-Transfer-Encoding: quoted-printable\n"
. "\n"
. "$USERname, thanks for visiting theCompanyName.com.\n"
. "We've received your e-mail and will respond,\n"
. "as soon as possible.\n"
. "\n"
. "This e-mail is in response to a submission to www.theCompanyName.com<b>.\n"
. "Sent by: $USERemail\n"
. "from: IP " . $_SERVER['REMOTE_ADDR'] . "\n"
. "\n"
. "If you feel you've received this e-mail in error, please contact us at www.theCompanyName.com\n"
. "\n"
. ""
. "\n"
. "--USER_BOUNDRY\n"
. "Content-Type: text/html; charset:\"ISO-8859-1\"\n"
. "Content-Transfer-Encoding: quoted-printable\n"
. "\n"
. "<HTML>\n"
. "<head>\n"
. "<title>Inquiry/Comments Received</title>\n"
. "</head>\n"
. "<body>\n"
. "<img src=\ 'http://LinkToImage/Logo.png' alt=\ '' height=\ '77' width=\ '116' border=\ '0'>\n"
. "<p><font size=\ '3' face=\ 'Helvetica, Geneva, Arial, SunSans-Regular, sans-serif'>Mr./Mrs, $USERname,<br />\n"
. "<br />\n"
. "Thanks for visiting theCompanyName.com.<br />\n"
. "<br />\n"
. "We've received your submission and appriciate your interest in theCompanyName.<br />\n"
. "After reviewing your comments we will respond as soon as possible.<br />\n"
. "<br />\n"
. "Regards,<br /></font>\n"
. "<br />\n"
. "<font size=\ '3' color=\ '#7397AE' face=\ 'Helvetica, Geneva, Arial, SunSans-Regular, sans-serif'><b>The CompanyName team</b><br /></font>\n"
. "<hr align=\ 'left' noshade size=\ '1' width=\ '550' color=\ '7397AE'>\n"
. "\n"
. "<font size=\ '2' color=\ '#c0c0c0' face=\ 'Helvetica, Geneva, Arial, SunSans-Regular, sans-serif'>This e-mail is in response to a submission at www.theCompanyName.com<b>.</b><br />\n"
. "Sent by: $USERemail<br />\n"
. "from: IP " . $_SERVER['REMOTE_ADDR'] . "<br />\n"
. "<br />\n"
. "If you feel you've received this e-mail in error, please contact us at <b><a href=\ 'mailto:info@theCompanyName.com' style=\ 'color:#c0c0c0'>www.theCompanyName.com</a></b><br /></font></p>\n"
. "</body>\n"
. "</html>\n"
. "\n"
. ""
. "\n"
. "--USER_BOUNDRY--";

mail($confEmailTo, $confEmailSubject, $confEmailBody, $confEmailHeader);

# Embed success page and dump it to the browser

$fileSuccessPage = 'contact_Success.html';

if (file_exists($fileSuccessPage) === false) {
echo 'The success page: <b>contact_Success.html</b> cannot be found on the server.';
exit;
}

$fileHandle = fopen ($fileSuccessPage, "r");
$successPage = fread ($fileHandle, filesize($fileSuccessPage));
fclose ($fileHandle);

$successPage = str_replace('<!--FIELDVALUE:cmd-->', $USERcmd, $successPage);
$successPage = str_replace('<!--FIELDVALUE:name-->', $USERname, $successPage);
$successPage = str_replace('<!--FIELDVALUE:email-->', $USERemail, $successPage);
$successPage = str_replace('<!--FIELDVALUE:comments-->', $USERcomments, $successPage);

echo $successPage;
exit;

# End of PHP script
?>

hothousegraphix
12-27-2006, 02:07 AM
I think the problem is the fact this is not being viewed by a browser, but rather, e-mail applications.

hothousegraphix
12-28-2006, 01:28 AM
Because of the assistance of someone from another sites Users Fourm I was provided an explaination as to why I've been experiencing trouble; dropped formatting characters in the resulting e-mail document printed from my PHP script.


Content-Transfer-Encoding: quoted-printable

This is the source of my problem. Quoted-printable encoding uses the '=' character to escape values in exactly the same way as the '%' is used to escape values in URLs. Basically, what this means is that if I want the '=' character to appear in your output, and I want to use quoted-printable as my CTE, then I need to replace every '=' with '=3D'. Alternatively, I can use "Content-Transfer-Encoding: 7bit" instead, as all printable ASCII characters can be represented in this encoding and I then don't have to worry about escaping any characters.

http://en.wikipedia.org/wiki/MIME#Co...nsfer-Encoding



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum