best way to encode whitespace in URL passed via 'mailto' HREF link

Posted on 2007-11-19
Last Modified: 2009-12-16
I wrote a PHP based web front end for file upload & download a few years ago.

Outside our firewall, people connect to it via a browser or via WebDAV.  They can download files, navigate thru nested directories, and upload files as well.  Authentication is required to browse client directories, auth is tied to an Open LDAP directory.

Inside of our firewall, people connect to via SMB/CIFS or AFP file sharing.

Our external user population like the convenience of having a direct link to a file for download.

Our internal user population lacks the technical savvy to construct such direct links for delivery via email.

I am trying to code up a link/form that does the following -

1. shows up only for internal web connections (using HTTP_PC_REMOTE_ADDR in $_SERVER allows for this it appears)
2. addresses an email message with a URL to the file in the body of the message

Early tests showed this was quite possible using PHP/HTML.

Further coding to allow for files/folders with whitespace in the name have proved more challenging.

I've tried a variety of methods to accomplish this...

- php eregreplace " " with "%20"
- php urlencode
- now javascript document.write with either escape() or encodeURI()

When the source code for the web page generates, the URL is formatted properly - " " prints out as "%20".  As soon as the mail client launches though, the URL loses the proper encoding.

My next attempt will involve setting the MIME type specifically I think.

The other option is to 'tell' my local users *NOT* to use spaces in file/folder names.  Thing is I've been trying to get cooperation with such a policy for years...sigh.

if (ereg('^192.168.', $_SERVER['HTTP_PC_REMOTE_ADDR']) || $_SERVER['HTTP_PC_REMOTE_ADDR'] == '') {
     print '<tr>';
     print '<td valign="top" align="right" bgcolor="#999999" colspan="3" class="mail_text">';
     print '<script language="JavaScript" type="text/javascript">';
     print 'document.write("<a href=\"mailto:?subject=file download from vitrorobertson?body=" + encodeURI("'.$_GET['dir_to_list'].'/'.$item.'") + "\">Email direct link to client</a>")';
     print '</script>';
     print '</td>';
     print '</tr>';
$_GET['dir_to_list'] is, obviously, passed from the last posting of the self-posting page/form.  I am obviously not using CSS for table generation.

Open in new window

Question by:pteeter
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
LVL 11

Accepted Solution

siliconbrit earned 63 total points
ID: 20316957

I have approached this problem myself.  When fixed in one area something else in the chain fell apart.

I used four different solutions:

1) Generate tiny URL's for the documents to be downloaded.  So you either use filesystem links or keep a hash table relating a small url to the actual document.

2) Zip the files and generate a name with no spaces.

3) Provide instructions on how to login and view the document through the browser rather than sending links in email.

4) Company wide policy to slaughter staff who put spaces in web-distributed documents.

Clearly the last one may be the least attractive (?!), but this is one of those issues that even Microsoft fails to solve for all recipients of it's Windows Live service marketing emails, including MS Outlook 2003 users.  My vote is to workaround.
LVL 27

Expert Comment

ID: 20856942
pteeter, please show an example of what a generated html table code looks like for your filenames with spaces.

Author Comment

ID: 20868634
The code is attached.

You'll see the inclusion of '%20' strings, substituting for white spaces in the body portion of the mailto URL.

Problem is that when the mail client is launched, the %20 strings disappear.

Hope this is useful.

Thanks for the help.
<td valign="top" class="list_text" align="left">
<td valign="top" class="list_text" align="left">
<a href="/pickup/94 Shot List Folder.sitx">94 Shot List Folder.sitx</a>
<td valign="top" class="list_text" align="center">(64.16 mb, 02/08/08)</td>
<td valign="top" align="right" bgcolor="#999999" colspan="3" class="mail_text">
<a href="mailto:?subject=file download from COMPANY_NAME&body=" type="text/html">Email direct link to client</a>

Open in new window

Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

LVL 27

Expert Comment

ID: 20869309
This seemed to work for me:
<a href="mailto:?subject=file download from COMPANY_NAME&body=<?php echo urlencode(" Shot List Folder.sitx"); ?>" type="text/html">

I would recommend not creating HTML entities in the string and then pass it to urlencode as shown.

Author Comment

ID: 20869415
i'll try it again, urlencode that is.

It didn't seem to cooperate before.

Question - which email client are you passing the URL to as body?

I'm using Outlook 2003 and Entourage 2004/2008.

I feel like neither has cooperated thus far.

LVL 27

Assisted Solution

ddrudik earned 62 total points
ID: 20869675
Outlook 2003, but I celebrated success too soon, Outlook 2003 doesn't translates the +'s into %20's and %20's are converted into spaces which break the HREF in the e-mail.  It would seem you are left with not including spaces in these e-mails, one method to accomplish this is to use a script that references the filename with spaces as +'s:
<a href="mailto:?subject=file download from COMPANY_NAME&body=<?php echo urlencode(" Shot List Folder.sitx"); ?>" type="text/html">

Then in send.php you would urldecode() the file value and send the browser to that location.

Author Comment

ID: 21947605
I hope to revisit this soon and try the '+' as spaces encoding method.



Author Comment

ID: 23614394
no problem, i havent been able to work on the project in some time.

Featured Post

What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Finding original email is quite difficult due to their duplicates. From this article, you will come to know why multiple duplicates of same emails appear and how to delete duplicate emails from Outlook securely and instantly while vital emails remai…
In this tutorial viewers will learn how to embed videos in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <video> tag to insert a video. Define the src as the URL of your video; this is similar to …
The viewer will learn how to dynamically set the form action using jQuery.

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question