Jump to content

Format Data using Tabs in Email


doubledee

Recommended Posts

My website sends me an e-mail when there is an error, and I would like it to look like this...

Date:		2012-03-17 12:36:46pm
Results Code:	EMAIL_USER_NOT_LOGGED_IN_2127
Error Page:	/members/change_email.php
Member ID:	0
IP Address:	127.0.0.1
Host Name:	localhost

 

However my PHP code isn't giving me that...

$body = "A website error has occurred...\n\n";
$body .= "Date: \t\t" . date('Y-m-d g:i:sa', time()) ."\n";
$body .= "Results Code: \t\t" . $resultsCode . "\n";
$body .= "Error Page: \t\t" . $errorPage . "\n";
$body .= "Member ID: \t\t" . $memberID . "\n";
$body .= "IP Address: \t\t" . $ip . "\n";
$body .= "Host Name: \t\t" . $hostName . "\n";

 

What is wrong?

 

Thanks,

 

 

Debbie

 

Link to comment
Share on other sites

So what is it giving you?

 

Well, unfortunately I can't post a screenshot here, but the left edges of the data values (versus the labels) looks jagged like I tried using spaces instead of tabs to create a two-column effect?!  :shrug:

 

It's not a big deal, but annoying nonetheless!

 

 

Debbie

 

Link to comment
Share on other sites

Because the tab lengths are based off of the last letters in the left column, so they won't line up nice unless the left labels are all the same length.

You will have to do what scootstah suggested and use HTML to make it how you want if it's a big deal.

Link to comment
Share on other sites

Because the tab lengths are based off of the last letters in the left column, so they won't line up nice unless the left labels are all the same length.

You will have to do what scootstah suggested and use HTML to make it how you want if it's a big deal.

 

Why is that so?

 

When I did the same thing in TextEdit the tabs behaved...

 

 

Debbie

 

Link to comment
Share on other sites

Different render engines may use different widths for what they consider a tab.  This is why you can tab something out so it looks really nice in say notepad, then copy it into a web form or other editor and it will not line up quite right.

 

You can use spaces instead of tabs to make sure things line up correctly as a space width is constant.  You could also use HTML and throw the data into a <table> with some styling to make it real nice.

 

Link to comment
Share on other sites

Different render engines may use different widths for what they consider a tab.  This is why you can tab something out so it looks really nice in say notepad, then copy it into a web form or other editor and it will not line up quite right.

 

You can use spaces instead of tabs to make sure things line up correctly as a space width is constant.  You could also use HTML and throw the data into a <table> with some styling to make it real nice.

 

I've always heard that creating HTML-generated e-mails is a nightmare and also a security risk.

 

Obviously I trust MY system e-mail ME, but still.

 

How would I do what you are saying?

 

Especially since I don't see how you could do it without CSS.

 

 

Debbie

 

Link to comment
Share on other sites

Different render engines may use different widths for what they consider a tab.  This is why you can tab something out so it looks really nice in say notepad, then copy it into a web form or other editor and it will not line up quite right.

 

You can use spaces instead of tabs to make sure things line up correctly as a space width is constant.  You could also use HTML and throw the data into a <table> with some styling to make it real nice.

 

I've always heard that creating HTML-generated e-mails is a nightmare and also a security risk.

 

Then you've heard wrong. It's no harder than a regular email, you just have to specify the mime-type as HTML. Done.

Link to comment
Share on other sites

To re-cap on my previous post..

(was from my iPhone, very tedious to post code from!)

 

$body = "<table>\n"
$body .= "<tr><td colspan=\"2\">A website error has occurred...</td></tr>\n";
$body .= "<tr><td>Date:</td><td>" . date('Y-m-d g:i:sa', time()) ."</td></tr>\n";
$body .= "<tr><td>Results Code:</td><td>" . $resultsCode . "</td></tr>\n";
$body .= "<tr><td>Error Page:</td><td>" . $errorPage . "</td></tr>\n";
$body .= "<tr><td>Member ID:</td><td>" . $memberID . "</td></tr>\n";
$body .= "<tr><td>IP Address:</td><td>" . $ip . "</td></tr>\n";
$body .= "<tr><td>Host Name:</td><td>" . $hostName . "</td></tr>\n";
$body .= "</table>\n"

Link to comment
Share on other sites

Then you've heard wrong. It's no harder than a regular email, you just have to specify the mime-type as HTML. Done.

 

I can find 100 Web Designers that disagree...

 

 

Debbie

 

 

Then you've found 100 people that don't know what they're talking about.

Link to comment
Share on other sites

l0gic,

 

I tried incorporating your code into mine...

$body = "<table>\n";
$body .= "<tr><td colspan=\"2\">A website error has occurred...</td></tr>\n";
$body .= "<tr><td>Date:</td><td>" . date('Y-m-d g:i:sa', time()) ."</td></tr>\n";
$body .= "<tr><td>Results Code:</td><td>" . $resultsCode . "</td></tr>\n";
$body .= "<tr><td>Error Page:</td><td>" . $errorPage . "</td></tr>\n";
$body .= "<tr><td>Member ID:</td><td>" . $memberID . "</td></tr>\n";
$body .= "<tr><td>IP Address:</td><td>" . $ip . "</td></tr>\n";
$body .= "<tr><td>Host Name:</td><td>" . $hostName . "</td></tr>\n";
$body .= "</table>\n";

mail('debbie@gmail.com', 'Re: Website Error  (' . date('Y-m-d g:i:sa', time()) . ')', $body, 'From: admin@MySite.com <admin@MySite.com>');

 

 

And when I open the e-mail I see...

admin@MySite.com admin@mysite.com to me

 

<table>

<tr><td colspan="2">A website error has occurred...</td></tr>

<tr><td>Date:</td><td>2012-03-18 7:44:58pm</td></tr>

<tr><td>Results Code:</td><td>COMMENT_MEMBER_COMMENT_ADDED_2052</td></tr>

<tr><td>Error Page:</td><td>/articles/add_comment.php</td></tr>

<tr><td>Member ID:</td><td>19</td></tr>

<tr><td>IP Address:</td><td>127.0.0.1</td></tr>

<tr><td>Host Name:</td><td>localhost</td></tr>

</table>

 

Not exactly what I was looking for...  ;)

 

 

Debbie

 

Link to comment
Share on other sites

Because you're still telling it to send as plaintext. Try this:

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

mail('debbie@gmail.com', 'Re: Website Error  (' . date('Y-m-d g:i:sa', time()) . ')', $body, $headers);

Link to comment
Share on other sites

Then you've heard wrong. It's no harder than a regular email, you just have to specify the mime-type as HTML. Done.

 

I can find 100 Web Designers that disagree...

 

 

Debbie

 

 

Then you've found 100 people that don't know what they're talking about.

 

For someone who is usually pretty well based, that is obnoxious.

 

I know Web Designers that could do circles around anyone on this board, so to say they they don't know anything is crazy.

 

Back on topic...

 

Is making HTML-based e-mails that use simple tags like <p> and <table> doable?  Probably.

 

But is making a professional-looking HTML, that looks like it was created in MS Word or a Desktop Publishing App, and which is supported across ALL browsers, and that withstands all filtering that exists out there, then NO, that isn't easy!!!

 

 

Debbie

 

 

Link to comment
Share on other sites

Then you've heard wrong. It's no harder than a regular email, you just have to specify the mime-type as HTML. Done.

 

I can find 100 Web Designers that disagree...

 

 

Debbie

 

 

Then you've found 100 people that don't know what they're talking about.

 

For someone who is usually pretty well based, that is obnoxious.

 

I know Web Designers that could do circles around anyone on this board, so to say they they don't know anything is crazy.

 

But they can't send an HTML email? Boy, they sure seem qualified.

 

Is making HTML-based e-mails that use simple tags like <p> and <table> doable?  Probably.

 

But is making a professional-looking HTML, that looks like it was created in MS Word or a Desktop Publishing App, and which is supported across ALL browsers, and that withstands all filtering that exists out there, then NO, that isn't easy!!!

 

I don't see how that is relevant at all. The ability to send an HTML email, like I said, is trivial. Whether it looks good or not is an entirely different matter.

Link to comment
Share on other sites

But they can't send an HTML email? Boy, they sure seem qualified.

 

I don't see how that is relevant at all. The ability to send an HTML email, like I said, is trivial. Whether it looks good or not is an entirely different matter.

 

The purpose of an HTML Email is something that is pretty and formatted.  So when I use that term that is what I am talking about, and no, it isn't "trivial'...

 

 

Debbie

 

Link to comment
Share on other sites

But they can't send an HTML email? Boy, they sure seem qualified.

 

I don't see how that is relevant at all. The ability to send an HTML email, like I said, is trivial. Whether it looks good or not is an entirely different matter.

 

The purpose of an HTML Email is something that is pretty and formatted.  So when I use that term that is what I am talking about, and no, it isn't "trivial'...

 

 

Debbie

 

 

I'm pretty sure any browser or email client can handle a simple HTML table.

 

It is trivial. All you have to do is add two headers. I already showed you how to do that above.

 

I'm not going to continue to argue. You have been given code, you have been given answers.

Link to comment
Share on other sites

I'm pretty sure any browser or email client can handle a simple HTML table.

 

Right, I said that earlier and am agreeing with you.

 

 

It is trivial. All you have to do is add two headers. I already showed you how to do that above.

 

And that is NOT what I said...

 

 

I'm not going to continue to argue. You have been given code, you have been given answers.

 

You started it, and added in "trivial" and "Boy, they sure seem qualified."

 

(Everything always seems to have to be *personal* on PHPFreaks.  I guess that is how Developers think.)

 

 

Debbie

 

Link to comment
Share on other sites

This thread has been locked because it is starting to get derailed due to more personal attacks rather than trying to solve the problem. Let's try to keep things under control for future threads, please. If anybody has any concerns or would like to discuss more about this thread/why it was closed, please feel free to PM me.

 

OP, to summarize the advice above:

[*] If you really want your email to look that way like you showed, you will need to switch to HTML emails. Most email clients do not used a fixed-width font-face.

[*] The format you are looking for in HTML is not as complicated as you might think. Yes, as you get more and more involved and have complex designs across multiple email clients it does get quite challenging. I can say that from personal experience, and tbh it isn't that much fun. But a simple data table should be a piece of cake for your skill level.

[*]l0gic's code just needs the content type that scootstah provided for it to work.

Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.