4

This is more of a curiosity than anything. The comment system on my site automatically generates an email to me whenever a comment is posted. In it is a link to approve the comment, and a link to deny the comment.

$my_headers = 'MIME-Version: 1.0' . "\n"; 

$my_headers .= 'Content-type: text/html; charset=iso-8859-1' . "\n"; 

$my_headers .= 'From: MYSITE.com <ealert@mysite.com>' . "\n"; 

    $subject = "Comment Received";

    $messagei = "Comment from " . addslashes($_POST['commentName']) ." on ".addslashes($title)."." . "<br /><br />" .addslashes($commentCommment);

    $messagei .= "<br /><br />
        <strong>OPTIONS:</strong>

    <br /><br /><a href='http://www.MYSITE.com/edit/instaprove.php?Approve=Approve&commentid=".$lastID."'>

    <h2>http://www.MYSITE.com/edit/instaprove.php?Approve=Approve&commentid=".$lastID."</h2> (Approve Comment)</a>

    <br /><br /><br /><a href='http://www.MYSITE.com/edit/instaprove.php?Remove=Remove&commentid=".$lastID."'>

    <h2>http://www.MYSITE.com/edit/instaprove.php?Remove=Remove&commentid=".$lastID."</h2> (Delete Comment)</a>";

    mail('comments@mysite.com',$subject,$messagei,$my_headers,"-fealert@mysite.com");

99% of the time it works just fine, but every once in a while, instead of generating the approval link correctly, as in

http://www.mysite.com/edit/instaprove.php?Approve=Approve&commentid=142631,

it generates it with a space in it, so the link I get in the email goes to

http://www.mysite.com/edit/instaprove.ph%20p?Approve=Approve&commentid=142631

Strange, no?

Edit: For clarification - When this happens, the link is written out in the email correctly (.php), but the link it goes to is broken (.ph p).

Edit 8/12: It just happened again. The link text is correct:

http://www.mysite.com/edit/instaprove.php?Approve=Approve&commentid=142858

but the link renders as

http://www.mysite.com/edit/%20instaprove.php?Approve=Approve&commentid=142858

So, when it does show up, the mystery space is showing up at different points in the link.

Dennis
  • 482
  • 7
  • 17
  • 2
    Not strange, but impossible for that code. – zerkms Aug 11 '11 at 00:14
  • I'd agree with you that its impossible, except that it's happened, more than once. – Dennis Aug 11 '11 at 00:23
  • 1
    Take in consideration that mail clients also tend to mess with the email's contents :) try testing on multiple mail clients..also,try loging the email bodies in a text file so you can see what is sent and what is received – Catalin Aug 11 '11 at 07:39
  • I thought that might have been it, since the first time I noticed it I was approving comments from my iPhone. But I double checked on my mail client, and in gmail - same business. I'll try logging and see if that gives me any clues. – Dennis Aug 11 '11 at 07:42
  • also, as far as I remember, the email's body has somekind of max length or something like this... found this on google: http://mailformat.dan.info/body/linelength.html ... it is possible if you don't limit the row length that the server automatically inserts some new line chars..hope this helps – Catalin Aug 11 '11 at 11:08
  • also check this out: http://stackoverflow.com/questions/5602910/receiving-mail-server-inserts-space-before-each-new-line-breaking-multipart-alte – Catalin Aug 11 '11 at 11:22

2 Answers2

6

I had a similar problem with spaces and also strange "!\n" appearing in the source of my email.

This was because of very long lines. Adding some "\n" in the content of the email solve the problem.

Max
  • 76
  • 1
  • 3
  • This is my bet as well. The mail server will wrap lines longer than about 1,000 characters, and the wrap will look like whitespace in HTML etc. Wrap your libes properly, or base64-encode the body part. – tripleee May 26 '12 at 15:27
  • I totally forgot about this question -- in fact, I don't even use this script anymore (we scrapped our old home-brew CMS and switched over to Joomla, and I'm loving it). However, when I was still tinkering with the old script, that was the solution I found, so you sir, get the answer. – Dennis May 30 '12 at 08:22
  • I had this same problem with a link in an HTML email being broken and splitting the line up solved it. – AndyDunn Jan 21 '14 at 12:37
  • I was sending html emails too and got this problem. For me, the email looked fine, as though all the lines were quite short. But looking at the source I found one big unnecessary div in there. I think this div was being considered as a single line and so I got the troublesome %20 in my href. Removing the unnecessary div before sending fixed it for me. NOTE - with html, I could not simply add \n's but
    s seem to work, unless they are in a big div.
    – ryan2johnson9 Jun 16 '15 at 23:31
1

This is pretty strange.. You may want to just do a string replace on the URL itself replace any whitespace with no characters since it apparently sometimes grabs a random whitespace somehow.

str_replace(" ", "", "http://www.MYSITE.com/edit/instaprove.php?Approve=Approve&commentid=12304728")

It is a pretty strange problem.

My only other option in thought would be to change the charset to UTF-8

Atticus
  • 6,585
  • 10
  • 35
  • 57
  • Correct, but I still wonder why this would happen in the first place. – Dennis Aug 11 '11 at 00:24
  • Yeah that is really strange.. Since you're defining it in a string, without passing it as a variable or string building it, I have no idea why that would happen.. Wish I could contribute a more helpful response but it is pretty absurd. Good luck man – Atticus Aug 11 '11 at 00:33
  • Just a thought -- what about changing the charset to utf-8? – Atticus Aug 11 '11 at 00:34
  • That's a good suggestion, I may try it. Truthfully, I'm not as concerned with a solution (like I said, it only happens once a week or so, so on about 1% or less of the comments I receive, and it doesn't really detract from the usability, I just have to delete the `%20` when it happens), but more so curious about the cause. – Dennis Aug 11 '11 at 01:50
  • Yeah, I am too actually. Hey if you find anything out, mind dropping a comment? :) thanks man and good luck – Atticus Aug 11 '11 at 01:52