Search code examples
phphtmlemail

Link within my email is adding a '%20' within my URL


I currently have a project that is based around approving and denying documents that have been changed or need to be uploaded to my database. I have the code set up where whenever a document is created or needs revision, a email will be sent. This email will have a breakdown of the document as well as a link to direct the user to the page they need to access. Currently that link is returning a 404 Not Found error. This is how my code is set up below:

$email_subject = "A Previous Document is Pending Removal";
$heading = "<a href='http://rfp.wabtec.com/production/apps/documentControl'>$dept_string-$ctrl_string-$num: $name $dev</a>";
$body_one = "A document is pending removal and will need your approval. Below is a brief description of the document:";
$body_two = "Click on the link below to be redirected to the page. Once you have logged in, check under the tab labeled <b>Pending Action</b>. Click on the name and you will be able to approve or deny the document.";

//Email
$email_to = $email;

//define the headers we want passed. Note that they are separated with \r\n
$email_headers = "MIME-Version: 1.0\r\n" .
                    "Content-type: text/html; charset=iso-8859-1\r\n" .
                    "From: RFPC Document System<[email protected]>\r\n" .
                    "Reply-To: RFPC Document System<[email protected]>";
  
//Body
$email_body = "<style type='text/css'>" .
                "BODY {FONT-FAMILY: arial,helvetica,sans-serif,verdana; FONT-SIZE: 14px;}" .
                ".heading {FONT-FAMILY: arial,helvetica,sans-serif,verdana; FONT-SIZE: 16px; FONT-WEIGHT: bold;}" .
                "th {text-align: left; padding-right: 10px;padding-bottom:5px}" .
                "td {padding-bottom:5px}" .
                "</style>" .
                "<div>Hello</div><br />" .
                "<div>$body_one</div><br />" .
                "<table>" .
                "<tr><th>File:</th><td><a href='http://rfp.wabtec.com/docs/documentSystem/docs/$file'>$name</a></td></tr>" .
                "<tr><th>Type:</th><td>$type</td></tr>" .
                "<tr><th>Description:</th><td>$description</td></tr>" .
                "</table><br />" .
                "<div>$body_two</div><br />" .
                "<div class='heading'>$heading</div><br />";
  
  $email_send = @mail( $email_to, $email_subject, $email_body, $email_headers );

Whenever the link is clicked on this is my URL:

http://rfp.wabtec.com/production/apps/documen%20tControl

I know that '%20' is HTML URL encode for space, but as you see above. There is no space within my url and was wondering why my code is putting it in the URL when I have no space there at all


Solution

  • Gmail and probably other email providers will add spaces if your HTML lines are too long. Any line longer than ~1024 characters (IIRC) will be broken.

    Break up lines with \n.

    Here, I just removed the string-gluing marks instead inserting a literal newline:

    $email_body = "<style type='text/css'>
    BODY {FONT-FAMILY: arial,helvetica,sans-serif,verdana; FONT-SIZE: 14px;}
    .heading {FONT-FAMILY: arial,helvetica,sans-serif,verdana; FONT-SIZE: 16px; FONT-WEIGHT: bold;}
    th {text-align: left; padding-right: 10px;padding-bottom:5px}
    td {padding-bottom:5px}
    </style>
    <div>Hello</div><br />
    <div>$body_one</div><br />
    <table>
    <tr><th>File:</th><td><a href='http://rfp.wabtec.com/docs/documentSystem/docs/$file'>$name</a></td></tr>
    <tr><th>Type:</th><td>$type</td></tr>
    <tr><th>Description:</th><td>$description</td></tr>
    </table><br />
    <div>$body_two</div><br />
    <div class='heading'>$heading</div><br />";
    

    You can also probably base64-encode the email body by setting the Content-Transfer-Encoding header for the piece to base64, e.g.:

    A base64-encoded email part will look something like this:

    Content-Type: text/html; charset="utf-8"
    MIME-Version: 1.0
    Content-Transfer-Encoding: base64
    
    CgoKCgo8IURPQ1RZUEUgaHRtbCBQVUJMSUMgIi0vL1czQy8vRFREIFhIVE1MIDEuMCBTdHJpY3Qv
    Q2dvS0NnbzhJVVJQUTFSWlVFVWdhSFJ0YkNCUVZVSk1TVU1nSWkwdkwxY3pReTh2UkZSRUlGaElW
    UTJkdlMwTm5iemhKVlZKUVVURlNXbFZGVldkaFNGSjBZa05DVVZaVlNrMVRWVTFuU1drd2Rrd3hZ
    <more base64-encoded lines wrapped to 76 characters>