XpressEngine Core  1.11.2
 All Classes Namespaces Files Functions Variables Pages
Mail.class.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) NAVER <http://www.navercorp.com> */
3 
4 require_once _XE_PATH_ . "libs/phpmailer/phpmailer.php";
5 
11 class Mail extends PHPMailer
12 {
13 
18  var $sender_name = '';
19 
24  var $sender_email = '';
25 
30  var $receiptor_name = '';
31 
36  var $receiptor_email = '';
37 
42  var $title = '';
43 
48  var $content = '';
49 
54  var $content_type = 'html';
55 
60  var $messageId = NULL;
61 
66  var $replyTo = NULL;
67 
72  var $bcc = NULL;
73 
78  var $attachments = array();
79 
84  var $cidAttachments = array();
85 
90  var $mainMailPart = NULL;
91 
96  var $body = '';
97 
102  var $header = '';
103 
108  var $eol = '';
109 
114  var $references = '';
115 
120  var $additional_params = NULL;
121 
126  var $use_smtp = FALSE;
127 
133  function __construct()
134  {
135 
136  }
137 
145  function useGmailAccount($account_name, $account_passwd)
146  {
147  $this->SMTPAuth = TRUE;
148  $this->SMTPSecure = "tls";
149  $this->Host = 'smtp.gmail.com';
150  $this->Port = '587';
151  if($this->isVaildMailAddress($account_name))
152  {
153  $this->Username = $account_name;
154  }
155  else
156  {
157  $this->Username = $account_name . '@gmail.com';
158  }
159  $this->Password = $account_passwd;
160  $this->IsSMTP();
161  }
162 
175  function useSMTP($auth = NULL, $host = NULL, $user = NULL, $pass = NULL, $secure = NULL, $port = 25)
176  {
177  $this->SMTPAuth = $auth;
178  $this->Host = $host;
179  $this->Username = $user;
180  $this->Password = $pass;
181  $this->Port = $port;
182 
183  if($secure == 'ssl' || $secure == 'tls')
184  {
185  $this->SMTPSecure = $secure;
186  }
187 
188  if(($this->SMTPAuth !== NULL && $this->Host !== NULL && $this->Username !== NULL && $this->Password !== NULL) || ($this->SMTPAuth === NULL && $this->Host !== NULL))
189  {
190  $this->IsSMTP();
191  $this->AltBody = "To view the message, please use an HTML compatible email viewer!";
192  return TRUE;
193  }
194  else
195  {
196  $this->IsMail();
197  return FALSE;
198  }
199  }
200 
208  {
209  $this->additional_params = $additional_params;
210  }
211 
219  function addAttachment($filename, $orgfilename)
220  {
221  $this->attachments[$orgfilename] = $filename;
222  }
223 
231  function addCidAttachment($filename, $cid)
232  {
233  $this->cidAttachments[$cid] = $filename;
234  }
235 
243  function setSender($name, $email)
244  {
245  if($this->Mailer == "mail")
246  {
247  $this->sender_name = $name;
248  $this->sender_email = $email;
249  }
250  else
251  {
252  $this->SetFrom($email, $name);
253  }
254  }
255 
261  function getSender()
262  {
263  if(!stristr(PHP_OS, 'win') && $this->sender_name)
264  {
265  return sprintf("%s <%s>", '=?utf-8?b?' . base64_encode($this->sender_name) . '?=', $this->sender_email);
266  }
267  return $this->sender_email;
268  }
269 
277  function setReceiptor($name, $email)
278  {
279  if($this->Mailer == "mail")
280  {
281  $this->receiptor_name = $name;
282  $this->receiptor_email = $email;
283  }
284  else
285  {
286  $this->AddAddress($email, $name);
287  }
288  }
289 
295  function getReceiptor()
296  {
297  if(!stristr(PHP_OS, 'win') && $this->receiptor_name && $this->receiptor_name != $this->receiptor_email)
298  {
299  return sprintf("%s <%s>", '=?utf-8?b?' . base64_encode($this->receiptor_name) . '?=', $this->receiptor_email);
300  }
301  return $this->receiptor_email;
302  }
303 
310  function setTitle($title)
311  {
312  if($this->Mailer == "mail")
313  {
314  $this->title = $title;
315  }
316  else
317  {
318  $this->Subject = $title;
319  }
320  }
321 
327  function getTitle()
328  {
329  return '=?utf-8?b?' . base64_encode($this->title) . '?=';
330  }
331 
338  function setBCC($bcc)
339  {
340  if($this->Mailer == "mail")
341  {
342  $this->bcc = $bcc;
343  }
344  else
345  {
346  $this->AddBCC($bcc);
347  }
348  }
349 
357  {
358  $this->messageId = $messageId;
359  }
360 
368  {
369  $this->references = $references;
370  }
371 
379  {
380  if($this->Mailer == "mail")
381  {
382  $this->replyTo = $replyTo;
383  }
384  else
385  {
386  $this->AddReplyTo($replyTo);
387  }
388  }
389 
397  {
398  $content = preg_replace_callback('/<img([^>]+)>/i', array($this, 'replaceResourceRealPath'), $content);
399  if($this->Mailer == "mail")
400  {
401  $this->content = $content;
402  }
403  else
404  {
405  $this->MsgHTML($content);
406  }
407  }
408 
416  function replaceResourceRealPath($matches)
417  {
418  return preg_replace('/src=(["\']?)files/i', 'src=$1' . Context::getRequestUri() . 'files', $matches[0]);
419  }
420 
426  function getPlainContent()
427  {
428  return chunk_split(base64_encode(str_replace(array("<", ">", "&"), array("&lt;", "&gt;", "&amp;"), $this->content)));
429  }
430 
436  function getHTMLContent()
437  {
438  return chunk_split(base64_encode($this->content_type != 'html' ? nl2br($this->content) : $this->content));
439  }
440 
447  function setContentType($mode = 'html')
448  {
449  $this->content_type = $mode == 'html' ? 'html' : '';
450  }
451 
457  function procAttachments()
458  {
459  if($this->Mailer == "mail")
460  {
461  if(count($this->attachments) > 0)
462  {
463  $this->body = $this->header . $this->body;
464  $boundary = '----==' . uniqid(rand(), TRUE);
465  $this->header = "Content-Type: multipart/mixed;" . $this->eol . "\tboundary=\"" . $boundary . "\"" . $this->eol . $this->eol;
466  $this->body = "--" . $boundary . $this->eol . $this->body . $this->eol . $this->eol;
467  $res = array();
468  $res[] = $this->body;
469  foreach($this->attachments as $filename => $attachment)
470  {
471  $type = $this->returnMIMEType($filename);
472  $file_handler = new FileHandler();
473  $file_str = $file_handler->readFile($attachment);
474  $chunks = chunk_split(base64_encode($file_str));
475  $tempBody = sprintf(
476  "--" . $boundary . $this->eol .
477  "Content-Type: %s;" . $this->eol .
478  "\tname=\"%s\"" . $this->eol .
479  "Content-Transfer-Encoding: base64" . $this->eol .
480  "Content-Description: %s" . $this->eol .
481  "Content-Disposition: attachment;" . $this->eol .
482  "\tfilename=\"%s\"" . $this->eol . $this->eol .
483  "%s" . $this->eol . $this->eol, $type, $filename, $filename, $filename, $chunks);
484  $res[] = $tempBody;
485  }
486  $this->body = implode("", $res);
487  $this->body .= "--" . $boundary . "--";
488  }
489  }
490  else
491  {
492  if(count($this->attachments) > 0)
493  {
494  foreach($this->attachments as $filename => $attachment)
495  {
496  parent::AddAttachment($attachment);
497  }
498  }
499  }
500  }
501 
508  {
509  if(count($this->cidAttachments) > 0)
510  {
511  $this->body = $this->header . $this->body;
512  $boundary = '----==' . uniqid(rand(), TRUE);
513  $this->header = "Content-Type: multipart/relative;" . $this->eol . "\ttype=\"multipart/alternative\";" . $this->eol . "\tboundary=\"" . $boundary . "\"" . $this->eol . $this->eol;
514  $this->body = "--" . $boundary . $this->eol . $this->body . $this->eol . $this->eol;
515  $res = array();
516  $res[] = $this->body;
517  foreach($this->cidAttachments as $cid => $attachment)
518  {
519  $filename = basename($attachment);
520  $type = $this->returnMIMEType(FileHandler::getRealPath($attachment));
521  $file_str = FileHandler::readFile($attachment);
522  $chunks = chunk_split(base64_encode($file_str));
523  $tempBody = sprintf(
524  "--" . $boundary . $this->eol .
525  "Content-Type: %s;" . $this->eol .
526  "\tname=\"%s\"" . $this->eol .
527  "Content-Transfer-Encoding: base64" . $this->eol .
528  "Content-ID: <%s>" . $this->eol .
529  "Content-Description: %s" . $this->eol .
530  "Content-Location: %s" . $this->eol . $this->eol .
531  "%s" . $this->eol . $this->eol, $type, $filename, $cid, $filename, $filename, $chunks);
532  $res[] = $tempBody;
533  }
534  $this->body = implode("", $res);
535  $this->body .= "--" . $boundary . "--";
536  }
537  }
538 
544  function send()
545  {
546  if($this->Mailer == "mail")
547  {
548  $boundary = '----==' . uniqid(rand(), TRUE);
549  $this->eol = $GLOBALS['_qmail_compatibility'] == "Y" ? "\n" : "\r\n";
550  $this->header = "Content-Type: multipart/alternative;" . $this->eol . "\tboundary=\"" . $boundary . "\"" . $this->eol . $this->eol;
551  $this->body = sprintf(
552  "--%s" . $this->eol .
553  "Content-Type: text/plain; charset=utf-8; format=flowed" . $this->eol .
554  "Content-Transfer-Encoding: base64" . $this->eol .
555  "Content-Disposition: inline" . $this->eol . $this->eol .
556  "%s" .
557  "--%s" . $this->eol .
558  "Content-Type: text/html; charset=utf-8" . $this->eol .
559  "Content-Transfer-Encoding: base64" . $this->eol .
560  "Content-Disposition: inline" . $this->eol . $this->eol .
561  "%s" .
562  "--%s--" .
563  "", $boundary, $this->getPlainContent(), $boundary, $this->getHTMLContent(), $boundary
564  );
565  $this->procCidAttachments();
566  $this->procAttachments();
567  $headers = sprintf(
568  "From: %s" . $this->eol .
569  "%s" .
570  "%s" .
571  "%s" .
572  "%s" .
573  "MIME-Version: 1.0" . $this->eol . "", $this->getSender(), $this->messageId ? ("Message-ID: <" . $this->messageId . ">" . $this->eol) : "", $this->replyTo ? ("Reply-To: <" . $this->replyTo . ">" . $this->eol) : "", $this->bcc ? ("Bcc: " . $this->bcc . $this->eol) : "", $this->references ? ("References: <" . $this->references . ">" . $this->eol . "In-Reply-To: <" . $this->references . ">" . $this->eol) : ""
574  );
575  $headers .= $this->header;
576  if($this->additional_params)
577  {
578  return mail($this->getReceiptor(), $this->getTitle(), $this->body, $headers, $this->additional_params);
579  }
580  return mail($this->getReceiptor(), $this->getTitle(), $this->body, $headers);
581  }
582  else
583  {
584  $this->procAttachments();
585  return parent::Send();
586  }
587  }
588 
595  function checkMailMX($email_address)
596  {
597  if(!Mail::isVaildMailAddress($email_address))
598  {
599  return FALSE;
600  }
601  list($user, $host) = explode("@", $email_address);
602  if(function_exists('checkdnsrr'))
603  {
604  if(checkdnsrr($host, "MX") || checkdnsrr($host, "A"))
605  {
606  return TRUE;
607  }
608  else
609  {
610  return FALSE;
611  }
612  }
613  return TRUE;
614  }
615 
622  function isVaildMailAddress($email_address)
623  {
624  if(preg_match("/([a-z0-9\_\-\.]+)@([a-z0-9\_\-\.]+)/i", $email_address))
625  {
626  return $email_address;
627  }
628  else
629  {
630  return '';
631  }
632  }
633 
640  function returnMIMEType($filename)
641  {
642  preg_match("|\.([a-z0-9]{2,4})$|i", $filename, $fileSuffix);
643  switch(strtolower($fileSuffix[1]))
644  {
645  case "js" :
646  return "application/x-javascript";
647  case "json" :
648  return "application/json";
649  case "jpg" :
650  case "jpeg" :
651  case "jpe" :
652  return "image/jpg";
653  case "png" :
654  case "gif" :
655  case "bmp" :
656  case "tiff" :
657  return "image/" . strtolower($fileSuffix[1]);
658  case "css" :
659  return "text/css";
660  case "xml" :
661  return "application/xml";
662  case "doc" :
663  case "docx" :
664  return "application/msword";
665  case "xls" :
666  case "xlt" :
667  case "xlm" :
668  case "xld" :
669  case "xla" :
670  case "xlc" :
671  case "xlw" :
672  case "xll" :
673  return "application/vnd.ms-excel";
674  case "ppt" :
675  case "pps" :
676  return "application/vnd.ms-powerpoint";
677  case "rtf" :
678  return "application/rtf";
679  case "pdf" :
680  return "application/pdf";
681  case "html" :
682  case "htm" :
683  case "php" :
684  return "text/html";
685  case "txt" :
686  return "text/plain";
687  case "mpeg" :
688  case "mpg" :
689  case "mpe" :
690  return "video/mpeg";
691  case "mp3" :
692  return "audio/mpeg3";
693  case "wav" :
694  return "audio/wav";
695  case "aiff" :
696  case "aif" :
697  return "audio/aiff";
698  case "avi" :
699  return "video/msvideo";
700  case "wmv" :
701  return "video/x-ms-wmv";
702  case "mov" :
703  return "video/quicktime";
704  case "zip" :
705  return "application/zip";
706  case "tar" :
707  return "application/x-tar";
708  case "swf" :
709  return "application/x-shockwave-flash";
710  default :
711  if(function_exists("mime_content_type"))
712  {
713  $fileSuffix = mime_content_type($filename);
714  }
715  return "unknown/" . trim($fileSuffix[0], ".");
716  }
717  }
718 
719 }
720 /* End of file Mail.class.php */
721 /* Location: ./classes/mail/Mail.class.php */
addAttachment($filename, $orgfilename)
Definition: Mail.class.php:219
getReceiptor()
Definition: Mail.class.php:295
$args title
Definition: ko.install.php:189
setBCC($bcc)
Definition: Mail.class.php:338
$mainMailPart
Definition: Mail.class.php:90
$content
Definition: Mail.class.php:48
if(file_exists(_XE_PATH_. 'config/config.user.inc.php')) if(!defined('__DEBUG__')) if(!defined('__DEBUG_OUTPUT__')) if(!defined('__DEBUG_PROTECT__')) if(!defined('__DEBUG_PROTECT_IP__')) if(!defined('__DEBUG_DB_OUTPUT__')) if(!defined('__LOG_SLOW_QUERY__')) if(!defined('__LOG_SLOW_TRIGGER__')) if(!defined('__LOG_SLOW_ADDON__')) if(!defined('__LOG_SLOW_WIDGET__')) if(!defined('__DEBUG_QUERY__')) if(!defined('__OB_GZHANDLER_ENABLE__')) if(!defined('__ENABLE_PHPUNIT_TEST__')) if(!defined('__PROXY_SERVER__')) if(!defined('__ERROR_LOG__')) if(!defined('__DISABLE_DEFAULT_CSS__')) if(!defined('__AUTO_OPCACHE_INVALIDATE__')) if((__DEBUG_OUTPUT__==2)&&version_compare(PHP_VERSION, '6.0.0')===-1) if(version_compare(PHP_VERSION, '5.3.0') >=0) $GLOBALS['__xe_autoload_file_map']
Definition: config.inc.php:324
checkMailMX($email_address)
Definition: Mail.class.php:595
setReplyTo($replyTo)
Definition: Mail.class.php:378
$receiptor_name
Definition: Mail.class.php:30
setReceiptor($name, $email)
Definition: Mail.class.php:277
getHTMLContent()
Definition: Mail.class.php:436
procAttachments()
Definition: Mail.class.php:457
$receiptor_email
Definition: Mail.class.php:36
$references
Definition: Mail.class.php:114
setMessageID($messageId)
Definition: Mail.class.php:356
isVaildMailAddress($email_address)
Definition: Mail.class.php:622
$cidAttachments
Definition: Mail.class.php:84
$messageId
Definition: Mail.class.php:60
setTitle($title)
Definition: Mail.class.php:310
procCidAttachments()
Definition: Mail.class.php:507
setReferences($references)
Definition: Mail.class.php:367
$attachments
Definition: Mail.class.php:78
__construct()
Definition: Mail.class.php:133
widget to display content
$sender_email
Definition: Mail.class.php:24
returnMIMEType($filename)
Definition: Mail.class.php:640
getRealPath($source)
const _XE_PATH_
Definition: config.inc.php:49
setContent($content)
Definition: Mail.class.php:396
useSMTP($auth=NULL, $host=NULL, $user=NULL, $pass=NULL, $secure=NULL, $port=25)
Definition: Mail.class.php:175
setSender($name, $email)
Definition: Mail.class.php:243
readFile($filename)
useGmailAccount($account_name, $account_passwd)
Definition: Mail.class.php:145
getRequestUri($ssl_mode=FOLLOW_REQUEST_SSL, $domain=null)
addCidAttachment($filename, $cid)
Definition: Mail.class.php:231
$additional_params
Definition: Mail.class.php:120
$sender_name
Definition: Mail.class.php:18
getPlainContent()
Definition: Mail.class.php:426
replaceResourceRealPath($matches)
Definition: Mail.class.php:416
setAdditionalParams($additional_params)
Definition: Mail.class.php:207
getTitle()
Definition: Mail.class.php:327
send()
Definition: Mail.class.php:544
setContentType($mode= 'html')
Definition: Mail.class.php:447
$content_type
Definition: Mail.class.php:54
getSender()
Definition: Mail.class.php:261
$replyTo
Definition: Mail.class.php:66