5 * -------------------------------------------------------------
10 * Author: Monte Ohrt <monte@ispi.net>
11 * Credits: Jason Sweat (added cc, bcc and subject functionality)
12 * Purpose: automate mailto address link creation, and optionally
14 * Input: address = e-mail address
15 * text = (optional) text to display, default is address
16 * encode = (optional) can be one of:
17 * none : no encoding (default)
18 * javascript : encode with javascript
19 * hex : encode with hexidecimal (no javascript)
20 * cc = (optional) address(es) to carbon copy
21 * bcc = (optional) address(es) to blind carbon copy
22 * subject = (optional) e-mail subject
23 * newsgroups = (optional) newsgroup(s) to post to
24 * followupto = (optional) address(es) to follow up to
25 * extra = (optional) extra tags for the href link
27 * Examples: {mailto address="me@domain.com"}
28 * {mailto address="me@domain.com" encode="javascript"}
29 * {mailto address="me@domain.com" encode="hex"}
30 * {mailto address="me@domain.com" subject="Hello to you!"}
31 * {mailto address="me@domain.com" cc="you@domain.com,they@domain.com"}
32 * {mailto address="me@domain.com" extra='class="mailto"'}
33 * Taken from the original Smarty
34 * http://smarty.php.net
35 * -------------------------------------------------------------
37 function tpl_function_mailto($params, &$template_object)
43 throw new Template_Exception("mailto: missing 'address' parameter", $template_object);
57 // netscape and mozilla do not decode %40 (@) in BCC field (bug?)
58 // so, don't encode it.
60 $mail_parms = array();
63 $mail_parms[] = 'cc='.str_replace('%40','@',rawurlencode($cc));
68 $mail_parms[] = 'bcc='.str_replace('%40','@',rawurlencode($bcc));
73 $mail_parms[] = 'subject='.rawurlencode($subject);
76 if (!empty($newsgroups))
78 $mail_parms[] = 'newsgroups='.rawurlencode($newsgroups);
81 if (!empty($followupto))
83 $mail_parms[] = 'followupto='.str_replace('%40','@',rawurlencode($followupto));
87 for ($i=0; $i<count($mail_parms); $i++
)
89 $mail_parm_vals .= (0==$i) ?
'?' : '&';
90 $mail_parm_vals .= $mail_parms[$i];
92 $address .= $mail_parm_vals;
98 elseif (!in_array($encode,array('javascript','hex','none')) )
100 throw new Template_Exception("mailto: 'encode' parameter must be none, javascript or hex", $template_object);
104 if ($encode == 'javascript' )
106 $string = 'document.write(\'<a href="mailto:'.$address.'" '.$extra.'>'.$text.'</a>\');';
108 for ($x=0; $x < strlen($string); $x++
)
110 $js_encode .= '%' . bin2hex($string[$x]);
112 return '<script type="text/javascript" language="javascript">eval(unescape(\''.$js_encode.'\'))</script>';
114 elseif ($encode == 'hex')
116 preg_match('!^(.*)(\?.*)$!',$address,$match);
117 if(!empty($match[2]))
119 throw new Template_Exception("mailto: hex encoding does not work with extra attributes. Try javascript.", $template_object);
122 $address_encode = "";
123 for ($x=0; $x < strlen($address); $x++
)
125 if(preg_match('!\w!',$address[$x]))
127 $address_encode .= '%' . bin2hex($address[$x]);
131 $address_encode .= $address[$x];
135 for ($x=0; $x < strlen($text); $x++
)
137 $text_encode .= '&#x' . bin2hex($text[$x]).';';
139 return '<a href="mailto:'.$address_encode.'" '.$extra.'>'.$text_encode.'</a>';
144 return '<a href="mailto:'.$address.'" '.$extra.'>'.$text.'</a>';