E-mail Formatting Issue

When an error occurs on my website, I log it into my database. And if the error cannot be logged into MySQL, then I send an e-mail to the Admin (i.e. “me”).

I thought everything was working fine, but during testing, I see that my e-mail is scrunched up on one line like this…

2012-11-23 10:27:57am Dear Admin, A website error has occurred, but could not be logged in the database. Please take immediate action!!

Here is how I build and send my e-mail in PHP…


		$emailTo = 'Debbie@mail.com';

		$headers  = "From: Admin <admin@MySite.com>\\r\
";
		$headers .= "MIME-Version: 1.0\\r\
";
		$headers .= "Content-Type: text/html; charset=ISO-8859-1\\r\
";

		if (mysqli_stmt_affected_rows($stmt1)!==1){
			// Insert Failed.

			// ************************************
			// E-mail Admin about Insert Error.		*
			// ************************************

			// Build E-mail.
			$subject_FailedInsert = 'Re: Unloggable Website Error  (' . $currTime . ')';

			$body_FailedInsert = $currTime ."\
\
";
			$body_FailedInsert .= "Dear Admin,\
\
";
			$body_FailedInsert .= "A website error has occurred, but could not be logged in the database.\
\
";
			$body_FailedInsert .= "Please take immediate action!!\
\
";

			mail($emailTo, $subject_FailedInsert, $body_FailedInsert, $headers);
		}

What is going on here?

BTW, I don’t need anything fancy as far as how the e-mail is formatted (i.e. HTML), but I would like it to have proper line breaks like this…

2012-11-23 9:27:57am

Dear Admin,

A website error has occurred, but could not be logged in the database.

Please take immediate action!!

Thanks,

Debbie

Try replacing "

" with "\r
\r
"
This may help.

No luck.

I swear this was working before for ages…

Could this be something Google is doing to my e-mail?

Should I have HTML in there like <p> or <br /> ?

Debbie

P.S. I am on my MacBook and as mentioned above, use Gmail…

Not exactly sure what the issue is here, as I tend to use components that handle html email when sending out emails.

One thing you may find useful - if you start using exceptions, you can create your own exception class that will handle sending out emails on error automatically. It will save a lot of code duplication, and it means if you get an issue like the one you’re having, you’d only need to fix it in one place in order to fix the issue sitewide.

In terms of the current issue - have you tried using html tags instead of the
character? Also, are you sure
should be used for line breaks? Perhaps try using \r or \r
and see if that makes a difference?

Even if that works, you should still look into the exception idea, as exceptions are an excellent way of handling errors and will make your life easier. If you want a quick example, let me know and I’ll maybe type something up.

Mac has different new line characters that Linux.
Try to replace the "

" with “\r”.PHP_EOL
PHP_EOL is constant for a new line character and its value depends on your OS.

Another thing you can try is to just write your entire email string in one multi-line string, not appending the $body_FailedInsert

$body_FailedInsert = “line 1
line 2
line 3
line 4”;

I tried both of these with no luck…


	$body_FailedInsert = $currTime . "\
\
" . PHP_EOL;
	$body_FailedInsert .= "Dear Admin,\\r\
\\r\
" . PHP_EOL;
	$body_FailedInsert .= "A website error has occurred, but could not be logged in the database.\\r\
\\r\
" . PHP_EOL;
	$body_FailedInsert .= "Please take immediate action!!\\r\
\\r\
" . PHP_EOL;


	$body_FailedInsert = "<p>$currTime</p><br />";
	$body_FailedInsert .= "<p>Dear Admin,<p>";
	$body_FailedInsert .= "<p>A website error has occurred, but could not be logged in the database.</p>";
	$body_FailedInsert .= "<p>Please take immediate action!!</p>";

Very frustrating… :smashy:

By the way, a little farther down in my code I send a 2nd e-mail to the Admin detailing the same info that was supposed to be logged in the database.

This e-mail displays nicely…


		$body_Error =		"<table>\
";
		$body_Error .=	"	<tr><td colspan=\\"2\\">A website error has occurred...<br /><br /></td></tr>\
";
		$body_Error .=	"	<tr><td>Date:</td><td>" . $currTime ."</td></tr>\
";
		$body_Error .=	"	<tr><td>Results Code:</td><td>" . $resultsCode . "</td></tr>\
";
		$body_Error .=	"	<tr><td>Error Page:</td><td>" . $sourcePage . "</td></tr>\
";
		$body_Error .=	"	<tr><td>Member ID:</td><td>" . $sessMemberID . "</td></tr>\
";
		$body_Error .=	"	<tr><td>IP Address:</td><td>" . $userIP . "</td></tr>\
";
		$body_Error .=	"	<tr><td>Host Name:</td><td>" . $userHostName . "</td></tr>\
";
		$body_Error .=	"</table>\
";

Go figure?!

Debbie

The second email is being formatted inside a table, which will account for the rows displaying in the email. Why not do a similar thing for the first email?

Does anyone else have any ideas?

I have tried playing around with hidden characters like
and \r and that isn’t helping.

I also tried the advice about using PHP_EOL in PHP echoes and that didn’t work either.

And putting things in an HTML Table like I did in my Error E-mail really isn’t appropriate, because I want a formatted paragraph instead.

I’m certain that the formatting of this message was okay in the past, but who knows?

It would seem like it should be pretty easy to just add carriage returns - and maybe tabs - to a simple Text-only e-mail?! :-/

Thanks,

Debbie

What about this:

$body_FailedInsert = $currTime ."\
\
" .
"Dear Admin,\
\
" .
"A website error has occurred, but could not be logged in the database.\
\
" .
"Please take immediate action!!\
\
";

That always works for me. It creates <br>s between the lines, though, rather than separate paragraphs.

See the code from my original post…

That is what I have, and I agree that it should work?! :-/

Debbie

It’s not exactly the same as your first post. I modified your code into what I’d do. Almost the same, so may make no difference, though …

I tried your code verbatim and the outcome is still one long wrapping line… :frowning:

Thanks,

Debbie

Are you putting it into some kind of element like a <p>?

Not currently, but see what I did in Post #1 and then in Post #6

Debbie

Tricky little rotter, isn’t it!

It doesn’t look very nice, but I wonder if this world work:

$body_FailedInsert = $currTime . "<br><br>" .
"Dear Admin," . "<br><br>" . 
"A website error has occurred, but could not be logged in the database." . "<br><br>" .
"Please take immediate action!";

Ralph,

I am currently trying apply this conversation to a feature on my website which lets a Member “Report a Profile”…

Your suggestion appears to have helped somewhat, but I am still having issues with my Admin e-mail.

At the bottom of my script, is this HTML Form where a Member can file a complaint…


	<form id="reportProfile" action="" method="post">
		<fieldset>
			<legend>Report a Profile</legend>

			<!-- Issue -->
			<label for="issue">Describe the Issue:</label>
			<?php
				echo '<textarea id="issue" name="issue" cols="50" rows="8">';
				echo (isset($issue) ? $issue : '');
				echo "</textarea><br />\
\
";
			?>

In the middle of this same script, I have this code that sends the Admin notification of the complaint…


	$body = "$currTime<br><br>"
			. "Dear Admin,<br><br>"
			. "'" . $sessUsername . "' has made a complaint about the member profile belonging to '" . $user . "'<br><br>"
			. "Here is the complaint...<br>"
			. "--------------------------<br>"
			.	$issue . "<br>"
			. "--------------------------<br><br>"
			. "Please take immediate action! (LATEST)";

If I type in this complaint to the Form…


Line1
Line2

Line4

…and submit it, in the Admin e-mail I see this…


Dear Admin,

'DoubleDee' has made a complaint about the member profile belonging to 'ralph.m'.

Here is the complaint...
--------------------------
Line1 Line2 Line4
--------------------------

Please take immediate action! (LATEST) 

I’m not sure why I am losing the carriage returns from my Form to my Gmail e-mail, but I am wondering if it has to do with how I have my E-mail Header defined… :-/

Here is the Header code I have been using…


	$headers  = "From: Admin <admin@MySite.com>\\r\
";
	$headers .= "MIME-Version: 1.0\\r\
";
	$headers .= "Content-Type: text/html; charset=ISO-8859-1\\r\
";

What do you think?

Sincerely,

Debbie

Hm, I can only do the most basic PHP form, and when I do carriage returns like that in the textarea, they come out just the same in the email that is sent, with carriage returns and all (to GMail etc.), so I really can’t say what’s causing your formatting to go up the creek like that. :frowning:

You’re sending the email as HTML, so you must use HTML line breaks, i.e., <br />, and not plain line breaks, i.e., \

PHP has a simple function to do this for you: [fphp]nl2br[/fphp]


    $body = "$currTime<br><br>" 
            . "Dear Admin,<br><br>" 
            . "'" . $sessUsername . "' has made a complaint about the member profile belonging to '" . $user . "'<br><br>" 
            . "Here is the complaint...<br>" 
            . "--------------------------<br>" 
            .    nl2br($issue) . "<br>" 
            . "--------------------------<br><br>" 
            . "Please take immediate action! (LATEST)";  

Also, you may want to [fphp]strip_tags[/fphp] and [fphp]htmlentities[/fphp] your $issue.

After some more research last night, I came to that some conclusion…

Apparently if you use ‘text/plain’ for an e-mail, then you correspondingly want to use \r
. And if you use ‘text/html’ then you should use <p> or <br />.

Debbie

Can you help me understand when and where I would want to use htmlentities??

For this current script, the User’s complaint appears in 3 places…

1.) On the Sticky Form if there is an error (e.g. Complaint is too long…).

2.) In the Database which the Admin would see.

3.) In an E-mail to the Admin for a quicker response.

With that being said, where should I use htmlentities??

For #1, I am using htmlentities so that if the Form is re-displayed, there is no risk of a XSS attack.

For #2, I am not sure what to do. I would think that you would want to see any HTML in the database as-is, versus the hard-to-read HTML Entities.

For #3, again, I’m not entirely sure.

Suggestions?

Debbie