SitePoint Sponsor

User Tag List

Results 1 to 22 of 22
  1. #1
    SitePoint Wizard DoubleDee's Avatar
    Join Date
    Aug 2010
    Location
    Arizona
    Posts
    3,931
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    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...
    PHP Code:
            $emailTo 'Debbie@mail.com';

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

            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 ."\n\n";
                
    $body_FailedInsert .= "Dear Admin,\n\n";
                
    $body_FailedInsert .= "A website error has occurred, but could not be logged in the database.\n\n";
                
    $body_FailedInsert .= "Please take immediate action!!\n\n";

                
    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

  2. #2
    PHP Guru lampcms.com's Avatar
    Join Date
    Jan 2009
    Posts
    921
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try replacing "\n\n" with "\r\n\r\n"
    This may help.
    My project: Open source Q&A
    (similar to StackOverflow)
    powered by php+MongoDB
    Source on github, collaborators welcome!

  3. #3
    SitePoint Wizard DoubleDee's Avatar
    Join Date
    Aug 2010
    Location
    Arizona
    Posts
    3,931
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by lampcms.com View Post
    Try replacing "\n\n" with "\r\n\r\n"
    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...

  4. #4
    Non-Member
    Join Date
    Oct 2007
    Posts
    363
    Mentioned
    11 Post(s)
    Tagged
    0 Thread(s)
    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 \n character? Also, are you sure \n should be used for line breaks? Perhaps try using \r or \r\n 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.

  5. #5
    PHP Guru lampcms.com's Avatar
    Join Date
    Jan 2009
    Posts
    921
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Mac has different new line characters that Linux.
    Try to replace the "\n\n" 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";
    My project: Open source Q&A
    (similar to StackOverflow)
    powered by php+MongoDB
    Source on github, collaborators welcome!

  6. #6
    SitePoint Wizard DoubleDee's Avatar
    Join Date
    Aug 2010
    Location
    Arizona
    Posts
    3,931
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by lampcms.com View Post
    Mac has different new line characters that Linux.
    Try to replace the "\n\n" 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...

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

    PHP Code:
        $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...


    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...
    PHP Code:
            $body_Error =        "<table>\n";
            
    $body_Error .=    "    <tr><td colspan=\"2\">A website error has occurred...<br /><br /></td></tr>\n";
            
    $body_Error .=    "    <tr><td>Date:</td><td>" $currTime ."</td></tr>\n";
            
    $body_Error .=    "    <tr><td>Results Code:</td><td>" $resultsCode "</td></tr>\n";
            
    $body_Error .=    "    <tr><td>Error Page:</td><td>" $sourcePage "</td></tr>\n";
            
    $body_Error .=    "    <tr><td>Member ID:</td><td>" $sessMemberID "</td></tr>\n";
            
    $body_Error .=    "    <tr><td>IP Address:</td><td>" $userIP "</td></tr>\n";
            
    $body_Error .=    "    <tr><td>Host Name:</td><td>" $userHostName "</td></tr>\n";
            
    $body_Error .=    "</table>\n"
    Go figure?!


    Debbie

  7. #7
    Non-Member
    Join Date
    Oct 2007
    Posts
    363
    Mentioned
    11 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by DoubleDee View Post
    I tried both of these with no luck...

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

    PHP Code:
        $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...


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

  8. #8
    SitePoint Wizard DoubleDee's Avatar
    Join Date
    Aug 2010
    Location
    Arizona
    Posts
    3,931
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Does anyone else have any ideas?

    I have tried playing around with hidden characters like \n 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

  9. #9
    It's all Geek to me silver trophybronze trophy
    ralph.m's Avatar
    Join Date
    Mar 2009
    Location
    Melbourne, AU
    Posts
    24,319
    Mentioned
    462 Post(s)
    Tagged
    8 Thread(s)
    What about this:

    PHP Code:
    $body_FailedInsert $currTime ."\n\n" .
    "Dear Admin,\n\n" .
    "A website error has occurred, but could not be logged in the database.\n\n" .
    "Please take immediate action!!\n\n"
    That always works for me. It creates <br>s between the lines, though, rather than separate paragraphs.

  10. #10
    SitePoint Wizard DoubleDee's Avatar
    Join Date
    Aug 2010
    Location
    Arizona
    Posts
    3,931
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ralph.m View Post
    What about this:

    PHP Code:
    $body_FailedInsert $currTime ."\n\n" .
    "Dear Admin,\n\n" .
    "A website error has occurred, but could not be logged in the database.\n\n" .
    "Please take immediate action!!\n\n"
    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

  11. #11
    It's all Geek to me silver trophybronze trophy
    ralph.m's Avatar
    Join Date
    Mar 2009
    Location
    Melbourne, AU
    Posts
    24,319
    Mentioned
    462 Post(s)
    Tagged
    8 Thread(s)
    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 ...

  12. #12
    SitePoint Wizard DoubleDee's Avatar
    Join Date
    Aug 2010
    Location
    Arizona
    Posts
    3,931
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ralph.m View Post
    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...

    Thanks,


    Debbie

  13. #13
    It's all Geek to me silver trophybronze trophy
    ralph.m's Avatar
    Join Date
    Mar 2009
    Location
    Melbourne, AU
    Posts
    24,319
    Mentioned
    462 Post(s)
    Tagged
    8 Thread(s)
    Are you putting it into some kind of element like a <p>?

  14. #14
    SitePoint Wizard DoubleDee's Avatar
    Join Date
    Aug 2010
    Location
    Arizona
    Posts
    3,931
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ralph.m View Post
    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

  15. #15
    It's all Geek to me silver trophybronze trophy
    ralph.m's Avatar
    Join Date
    Mar 2009
    Location
    Melbourne, AU
    Posts
    24,319
    Mentioned
    462 Post(s)
    Tagged
    8 Thread(s)
    Tricky little rotter, isn't it!

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

    PHP Code:
    $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!"

  16. #16
    SitePoint Wizard DoubleDee's Avatar
    Join Date
    Aug 2010
    Location
    Arizona
    Posts
    3,931
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    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...
    PHP Code:
        <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 />\n\n";
                
    ?>

    In the middle of this same script, I have this code that sends the Admin notification of the complaint...
    PHP Code:
        $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...
    Code:
    Line1
    Line2
    
    Line4
    ...and submit it, in the Admin e-mail I see this...
    Code:
    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...

    PHP Code:
        $headers  "From: Admin <admin@MySite.com>\r\n";
        
    $headers .= "MIME-Version: 1.0\r\n";
        
    $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n"
    What do you think?

    Sincerely,


    Debbie

  17. #17
    It's all Geek to me silver trophybronze trophy
    ralph.m's Avatar
    Join Date
    Mar 2009
    Location
    Melbourne, AU
    Posts
    24,319
    Mentioned
    462 Post(s)
    Tagged
    8 Thread(s)
    Quote Originally Posted by DoubleDee View Post
    I'm not sure why I am losing the carriage returns from my Form to my Gmail e-mail
    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.

  18. #18
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,097
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    Quote Originally Posted by DoubleDee View Post
    Here is the Header code I have been using...

    PHP Code:
        $headers  "From: Admin <admin@MySite.com>\r\n";
        
    $headers .= "MIME-Version: 1.0\r\n";
        
    $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n"
    You're sending the email as HTML, so you must use HTML line breaks, i.e., <br />, and not plain line breaks, i.e., \n

    PHP has a simple function to do this for you: nl2br

    PHP Code:
        $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 strip_tags and htmlentities your $issue.
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  19. #19
    SitePoint Wizard DoubleDee's Avatar
    Join Date
    Aug 2010
    Location
    Arizona
    Posts
    3,931
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ScallioXTX View Post
    You're sending the email as HTML, so you must use HTML line breaks, i.e., <br />, and not plain line breaks, i.e., \n

    PHP has a simple function to do this for you: nl2br

    PHP Code:
        $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 strip_tags and htmlentities 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\n. And if you use 'text/html' then you should use <p> or <br />.


    Debbie

  20. #20
    SitePoint Wizard DoubleDee's Avatar
    Join Date
    Aug 2010
    Location
    Arizona
    Posts
    3,931
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ScallioXTX View Post
    Also, you may want to strip_tags and htmlentities your $issue.
    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

  21. #21
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,097
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    #1. Yes, use htmlentities
    #2. Normally I'd say you shouldn't htmlentities stuff in your database, but since you're using PhpMyAdmin, which is a web interface, I'm not 100% sure. If there's a leak in PhpMyAdmin you're vulnerable to that too if you don't htmlentities stuff in your database. On the other hand, you normally don't want to htmlentities stuff in your database for reasons you already stated. So I'm not sure.
    #3. Yes, I would use it there too, to avoid XSS and CSFR via email.
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  22. #22
    SitePoint Wizard DoubleDee's Avatar
    Join Date
    Aug 2010
    Location
    Arizona
    Posts
    3,931
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ScallioXTX View Post
    #1. Yes, use htmlentities
    Right.


    #2. Normally I'd say you shouldn't htmlentities stuff in your database, but since you're using PhpMyAdmin, which is a web interface, I'm not 100% sure. If there's a leak in PhpMyAdmin you're vulnerable to that too if you don't htmlentities stuff in your database. On the other hand, you normally don't want to htmlentities stuff in your database for reasons you already stated. So I'm not sure.
    Well, what would be the worst that could happen if I was viewing my database in phpMyAdmin and ran in to an issue? (I think that is less of a concern on my computer than if a visitor to my live site encountered the same thing, right?)

    Plus, I wouldn't want to see stuff like this in my database...
    Code:
    &lt;i&gt;This is in Italics&lt;/i&gt;
    
    &lt;h1&gt;This is a Heading&lt;/h1&gt;
    If someone did add HTML tags, this looks much better...
    Code:
    This person has a rude profile!!
    
    Take a look at that nasty picture they have.  Look at what they are doing?!  That is obscene!!!
    
    And take a look at those <b>NAUGHTY WORDS</b> in their Interests section...  :)

    For my database, I decided to go with this...
    PHP Code:
        $issue $trimmed['issue'];
    //    $issue = str2htmlentities($trimmed['issue']); 

    #3. Yes, I would use it there too, to avoid XSS and CSFR via email.
    For my Admin e-mail, I decided to go with this...
    PHP Code:
            // str2htmlentities: Converts all applicable characters to HTML-entities.
            // nl2p: Converts Form submission to include proper HTML tags.
            
    $formattedIssue nl2p(str2htmlentities($issue));
    //        $formattedIssue = nl2p($issue); 
    Off Topic:

    Those two Functions above are ones I wrote last summer which are similar to the ones you'd expect, but slightly better!



    Well, I think that covers everything, unless you have any more advice?

    Thanks,


    Debbie


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •