PHP : mailto function gets breaking when populating text having lines and url

Go To


Hi I am creating a task assignment page, in which after creating the task admin can send a mail to assignee to know the task. I cant use PHP mail function since I cant configure office SMTP mail address. So I am using mailto function.

Issue happens when my Commenst got symbols like " &, ", ' . It will stop executing the remaining part of code

In my mail it just populates the first line. So my comments is getting broken.

Here is my code to mailto function :

<a href="mailto:'.mysql_result($result, $i, 'assignee').' Task Assignment &body= %0D%0AHello%20'.mysql_result($result, $i, 'assignee'). '%0D%0A%0D%0ADescription%20%3A%20'.mysql_result($result, $i, 'comments'). .'%20%0D%0A%0D%0A%0D%0AFor%20more%20details%20Log%20in%20to%20%3A%20%0D%0A " ><img src="images/mail.png"  width="32" height="32" p = "center"></a>';
2012-04-04 07:43
by wanderors
Not sure what the standard dictates exactly, but I suspect you should be urlencode()ing the values in the URL. (which you're already doing by hand on part of it - Corbin 2012-04-04 07:48
Mailto is not a function but a URI-scheme, The mailto URL scheme. Build it according to the specs is the best you can do. Things might still not work however - hakre 2012-04-04 07:48
Try using javascript escape() function to urlencode the mail body - mim 2012-04-04 08:06


You put everything into one line which brings you into devils kitchen. Instead put the functionality apart (e.g. first load the data from the database), then process the data (e.g. build the mailbox name, the subject and the body) and then finally create the mail URL.


# load data from database
$assignee = mysql_result($result, $i, 'assignee');
$comments = mysql_result($result, $i, 'comments');

# process data
$mailbox = sprintf('', rawurlencode($assignee));
$headers = array(
    'subject' => 'Task Assignment',
    'body' => "Hello $assignee,\n\nDescription : $comments ...\n\nFor more details log in to ...",

# generate the mailto URL
$url = sprintf('mailto:%s?%s', $mailbox, http_build_query($headers));

You can then just output the $url:

printf('<a href="%s"><img src="images/mail.png" width="32" height="32" p="center"></a>',
2012-04-04 08:12
by hakre
But the issue happens when the comments contain the symbols " ' & - wanderors 2012-04-04 10:57
Why "but"? That's exactly why http_build_query is used - hakre 2012-04-04 12:44


You need to explicity urlencode everything that is non alphanumeric.

Note, urlencode (the php command) doesn't urlencode everything such as underscore and letters and perhaps other things.


    $test="this: is it's dog/+_";
    echo "<br>";
    echo urlencode($test);
    echo "<br>";
    echo $test;


this: is it's dog/+_
2014-03-27 21:23
by ssaltman