adLDAP library upgraded to 3.3.1
[dokuwiki.git] / inc / mail.php
blob3b0592b8bdb5103f21bd5b12ef4798181a1379e6
1 <?php
2 /**
3 * Mail functions
5 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author Andreas Gohr <andi@splitbrain.org>
7 */
9 if(!defined('DOKU_INC')) die('meh.');
10 require_once(DOKU_INC.'inc/utf8.php');
11 require_once(DOKU_INC.'inc/EmailAddressValidator.php');
13 // end of line for mail lines - RFC822 says CRLF but postfix (and other MTAs?)
14 // think different
15 if(!defined('MAILHEADER_EOL')) define('MAILHEADER_EOL',"\n");
16 #define('MAILHEADER_ASCIIONLY',1);
18 /**
19 * Patterns for use in email detection and validation
21 * NOTE: there is an unquoted '/' in RFC2822_ATEXT, it must remain unquoted to be used in the parser
22 * the pattern uses non-capturing groups as captured groups aren't allowed in the parser
23 * select pattern delimiters with care!
25 * May not be completly RFC conform!
26 * @link http://www.faqs.org/rfcs/rfc2822.html (paras 3.4.1 & 3.2.4)
28 * @author Chris Smith <chris@jalakai.co.uk>
29 * Check if a given mail address is valid
31 if (!defined('RFC2822_ATEXT')) define('RFC2822_ATEXT',"0-9a-zA-Z!#$%&'*+/=?^_`{|}~-");
32 if (!defined('PREG_PATTERN_VALID_EMAIL')) define('PREG_PATTERN_VALID_EMAIL', '['.RFC2822_ATEXT.']+(?:\.['.RFC2822_ATEXT.']+)*@(?i:[0-9a-z][0-9a-z-]*\.)+(?i:[a-z]{2,4}|museum|travel)');
36 /**
37 * UTF-8 autoencoding replacement for PHPs mail function
39 * Email address fields (To, From, Cc, Bcc can contain a textpart and an address
40 * like this: 'Andreas Gohr <andi@splitbrain.org>' - the text part is encoded
41 * automatically. You can seperate receivers by commas.
43 * @param string $to Receiver of the mail (multiple seperated by commas)
44 * @param string $subject Mailsubject
45 * @param string $body Messagebody
46 * @param string $from Sender address
47 * @param string $cc CarbonCopy receiver (multiple seperated by commas)
48 * @param string $bcc BlindCarbonCopy receiver (multiple seperated by commas)
49 * @param string $headers Additional Headers (seperated by MAILHEADER_EOL
50 * @param string $params Additonal Sendmail params (passed to mail())
52 * @author Andreas Gohr <andi@splitbrain.org>
53 * @see mail()
55 function mail_send($to, $subject, $body, $from='', $cc='', $bcc='', $headers=null, $params=null){
57 $message = compact('to','subject','body','from','cc','bcc','headers','params');
58 return trigger_event('MAIL_MESSAGE_SEND',$message,'_mail_send_action');
61 function _mail_send_action($data) {
63 // retrieve parameters from event data, $to, $subject, $body, $from, $cc, $bcc, $headers, $params
64 $to = $data['to'];
65 $subject = $data['subject'];
66 $body = $data['body'];
68 // add robustness in case plugin removes any of these optional values
69 $from = isset($data['from']) ? $data['from'] : '';
70 $cc = isset($data['cc']) ? $data['cc'] : '';
71 $bcc = isset($data['bcc']) ? $data['bcc'] : '';
72 $headers = isset($data['headers']) ? $data['headers'] : null;
73 $params = isset($data['params']) ? $data['params'] : null;
75 // end additional code to support event ... original mail_send() code from here
77 if(defined('MAILHEADER_ASCIIONLY')){
78 $subject = utf8_deaccent($subject);
79 $subject = utf8_strip($subject);
82 if(!utf8_isASCII($subject)) {
83 $subject = '=?UTF-8?Q?'.mail_quotedprintable_encode($subject,0).'?=';
84 // Spaces must be encoded according to rfc2047. Use the "_" shorthand
85 $subject = preg_replace('/ /', '_', $subject);
88 $header = '';
90 // No named recipients for To: in Windows (see FS#652)
91 $usenames = (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') ? false : true;
93 $to = mail_encode_address($to,'',$usenames);
94 $header .= mail_encode_address($from,'From');
95 $header .= mail_encode_address($cc,'Cc');
96 $header .= mail_encode_address($bcc,'Bcc');
97 $header .= 'MIME-Version: 1.0'.MAILHEADER_EOL;
98 $header .= 'Content-Type: text/plain; charset=UTF-8'.MAILHEADER_EOL;
99 $header .= 'Content-Transfer-Encoding: quoted-printable'.MAILHEADER_EOL;
100 $header .= $headers;
101 $header = trim($header);
103 $body = mail_quotedprintable_encode($body);
105 if($params == null){
106 return @mail($to,$subject,$body,$header);
107 }else{
108 return @mail($to,$subject,$body,$header,$params);
113 * Encodes an email address header
115 * Unicode characters will be deaccented and encoded
116 * quoted_printable for headers.
117 * Addresses may not contain Non-ASCII data!
119 * Example:
120 * mail_encode_address("föö <foo@bar.com>, me@somewhere.com","TBcc");
122 * @param string $string Multiple adresses separated by commas
123 * @param string $header Name of the header (To,Bcc,Cc,...)
124 * @param boolean $names Allow named Recipients?
126 function mail_encode_address($string,$header='',$names=true){
127 $headers = '';
128 $parts = explode(',',$string);
129 foreach ($parts as $part){
130 $part = trim($part);
132 // parse address
133 if(preg_match('#(.*?)<(.*?)>#',$part,$matches)){
134 $text = trim($matches[1]);
135 $addr = $matches[2];
136 }else{
137 $addr = $part;
140 // skip empty ones
141 if(empty($addr)){
142 continue;
145 // FIXME: is there a way to encode the localpart of a emailaddress?
146 if(!utf8_isASCII($addr)){
147 msg(htmlspecialchars("E-Mail address <$addr> is not ASCII"),-1);
148 continue;
151 if(!mail_isvalid($addr)){
152 msg(htmlspecialchars("E-Mail address <$addr> is not valid"),-1);
153 continue;
156 // text was given
157 if(!empty($text) && $names){
158 // add address quotes
159 $addr = "<$addr>";
161 if(defined('MAILHEADER_ASCIIONLY')){
162 $text = utf8_deaccent($text);
163 $text = utf8_strip($text);
166 if(!utf8_isASCII($text)){
167 $text = '=?UTF-8?Q?'.mail_quotedprintable_encode($text,0).'?=';
169 }else{
170 $text = '';
173 // add to header comma seperated
174 if($headers != ''){
175 $headers .= ',';
176 if($header) $headers .= MAILHEADER_EOL.' '; // avoid overlong mail headers
178 $headers .= $text.' '.$addr;
181 if(empty($headers)) return null;
183 //if headername was given add it and close correctly
184 if($header) $headers = $header.': '.$headers.MAILHEADER_EOL;
186 return $headers;
190 * Check if a given mail address is valid
192 * @param string $email the address to check
193 * @return bool true if address is valid
195 function mail_isvalid($email){
196 $validator = new EmailAddressValidator;
197 return $validator->check_email_address($email);
201 * Quoted printable encoding
203 * @author umu <umuAThrz.tu-chemnitz.de>
204 * @link http://www.php.net/manual/en/function.imap-8bit.php#61216
206 function mail_quotedprintable_encode($sText,$maxlen=74,$bEmulate_imap_8bit=true) {
207 // split text into lines
208 $aLines= preg_split("/(?:\r\n|\r|\n)/", $sText);
209 $cnt = count($aLines);
211 for ($i=0;$i<$cnt;$i++) {
212 $sLine =& $aLines[$i];
213 if (strlen($sLine)===0) continue; // do nothing, if empty
215 $sRegExp = '/[^\x09\x20\x21-\x3C\x3E-\x7E]/e';
217 // imap_8bit encodes x09 everywhere, not only at lineends,
218 // for EBCDIC safeness encode !"#$@[\]^`{|}~,
219 // for complete safeness encode every character :)
220 if ($bEmulate_imap_8bit)
221 $sRegExp = '/[^\x20\x21-\x3C\x3E-\x7E]/e';
223 $sReplmt = 'sprintf( "=%02X", ord ( "$0" ) ) ;';
224 $sLine = preg_replace( $sRegExp, $sReplmt, $sLine );
226 // encode x09,x20 at lineends
228 $iLength = strlen($sLine);
229 $iLastChar = ord($sLine{$iLength-1});
231 // !!!!!!!!
232 // imap_8_bit does not encode x20 at the very end of a text,
233 // here is, where I don't agree with imap_8_bit,
234 // please correct me, if I'm wrong,
235 // or comment next line for RFC2045 conformance, if you like
236 if (!($bEmulate_imap_8bit && ($i==count($aLines)-1))){
237 if (($iLastChar==0x09)||($iLastChar==0x20)) {
238 $sLine{$iLength-1}='=';
239 $sLine .= ($iLastChar==0x09)?'09':'20';
242 } // imap_8bit encodes x20 before chr(13), too
243 // although IMHO not requested by RFC2045, why not do it safer :)
244 // and why not encode any x20 around chr(10) or chr(13)
245 if ($bEmulate_imap_8bit) {
246 $sLine=str_replace(' =0D','=20=0D',$sLine);
247 //$sLine=str_replace(' =0A','=20=0A',$sLine);
248 //$sLine=str_replace('=0D ','=0D=20',$sLine);
249 //$sLine=str_replace('=0A ','=0A=20',$sLine);
252 // finally split into softlines no longer than $maxlen chars,
253 // for even more safeness one could encode x09,x20
254 // at the very first character of the line
255 // and after soft linebreaks, as well,
256 // but this wouldn't be caught by such an easy RegExp
257 if($maxlen){
258 preg_match_all( '/.{1,'.($maxlen - 2).'}([^=]{0,2})?/', $sLine, $aMatch );
259 $sLine = implode( '=' . MAILHEADER_EOL, $aMatch[0] ); // add soft crlf's
263 // join lines into text
264 return implode(MAILHEADER_EOL,$aLines);