Mail.class.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. /**
  3. * Utility tool for sending mail to multiple recipient with hmlt format and optionnal attachments
  4. * @author valentin carruesco
  5. * @category Core
  6. * @license MIT
  7. */
  8. class Mail{
  9. public $title,$expeditor,$firm,$recipients,$message,$format,$encoding,$reply,$attachments;
  10. function __construct(){
  11. global $myUser, $myFirm;
  12. $this->encoding = 'utf-8';
  13. $this->firm = utf8_decode($myFirm->label);
  14. $this->expeditor = $myUser->mail;
  15. $this->reply = $myUser->mail;
  16. $this->recipients = array('to'=>array(),'cc'=>array(),'cci'=>array());
  17. $this->attachments = array();
  18. }
  19. //Remplis le corps du message a partir d'un fichier
  20. //template et d'un tableau de données
  21. public function template($path,$data,$mustache = false){
  22. if(!file_exists($path)) return;
  23. $stream = file_get_contents($path);
  24. $this->message = template($stream,$data,$mustache);
  25. }
  26. public function send(){
  27. // clé aléatoire de limite
  28. $boundary = md5(uniqid(microtime(), TRUE));
  29. // Headers
  30. $headers = 'From: '.$this->expeditor."\r\n";
  31. $headers .= "Reply-to: ".$this->reply."\r\n";
  32. $headers .= 'X-Mailer: PHP/' . phpversion(). "\r\n" ;
  33. if(isset($this->recipients['cc']) && count($this->recipients['cc'])>0) $headers .= 'Cc: '.implode(',',$this->recipients['cc']) . "\r\n";
  34. if(isset($this->recipients['cci']) && count($this->recipients['cci'])>0) $headers .= 'Bcc: '.implode(',',$this->recipients['cci']) . "\r\n";
  35. $headers .= "Organization: \"".$this->firm."\"\r\n";
  36. $headers .= 'Mime-Version: 1.0'."\r\n";
  37. $headers .= 'Content-Type: multipart/mixed;boundary='.$boundary."\r\n";
  38. $headers .= "\r\n";
  39. // Message
  40. $stream = '--'.$boundary."\r\n";
  41. $stream .= 'Content-Type: multipart/alternative; boundary="alt-'.$boundary."\"\r\n\r\n";
  42. $stream .= '--alt-'.$boundary."\r\n";
  43. $stream .= 'Content-Type: text/plain; charset=utf-8'."\r\n";
  44. $stream .= 'Content-Transfer-Encoding: 8bit'."\r\n\r\n";
  45. $plainMessage = preg_replace('/<hr[^>]*>/i','-----------------------------------',$this->message);
  46. $plainMessage = preg_replace('/\t/i','',trim(strip_tags($plainMessage)));
  47. $plainMessage = preg_replace('/(\r?\n){2,}/i',"\n\n",$plainMessage);
  48. $stream .= $plainMessage."\r\n\r\n";
  49. $stream .= '--alt-'.$boundary."\r\n";
  50. // Message HTML
  51. $stream .= 'Content-type: text/html; charset='.$this->encoding."\r\n";
  52. $stream .= 'Content-Transfer-Encoding: 8bit'."\r\n\r\n";
  53. $stream .= '<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional //EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">'."\r\n";
  54. $stream .= '<html><head><meta http-equiv="Content-Type" content="text/html; charset='.$this->encoding.'" />'."\r\n";
  55. $stream .= '<style>body{font-family:Calibri,Arial,Verdana;color:#222222;font-size:1em;}</style>'."\r\n";
  56. $stream .= '</head><body>'."\r\n";
  57. $stream .= $this->message.'</body></html>'."\r\n\r\n";
  58. $stream .= '--alt-'.$boundary."--\r\n\r\n";
  59. foreach($this->attachments as $attachment) {
  60. $stream .= '--'.$boundary."\r\n";
  61. if(isset($attachment['cid'])) {
  62. $stream .= 'Content-Disposition: inline; filename="'.utf8_decode($attachment['name']).'"'."\r\n";
  63. $stream .= 'Content-ID: '.$attachment['cid']."\r\n";
  64. $stream .= 'Content-type:'.$attachment['type'].';name="'.utf8_decode($attachment['name']).'"'."\r\n";
  65. $stream .= 'Content-transfer-encoding:base64'."\n\n";
  66. $stream .= chunk_split(base64_encode($attachment['stream']))."\r\n";
  67. } else {
  68. $stream .= 'Content-type:'.$attachment['type'].';name="'.utf8_decode($attachment['name']).'"'."\r\n";
  69. $stream .= 'Content-transfer-encoding:base64'."\r\n";
  70. $stream .= 'Content-Disposition:attachment; filename="'.utf8_decode($attachment['name']).'"'."\n\n";
  71. $stream .= chunk_split(base64_encode($attachment['stream']))."\r\n";
  72. }
  73. }
  74. // Fin
  75. $stream .= '--'.$boundary.'--';
  76. $title = mb_encode_mimeheader($this->title,'UTF-8');
  77. try{
  78. set_error_handler(function($errno, $errstr, $errfile, $errline, array $errcontext) {
  79. throw new Exception("Erreur d'envoi de mail n°".$errno." :".$errstr);
  80. });
  81. mail(implode(',',array_unique($this->recipients['to'])), $title, $stream, $headers);
  82. restore_error_handler();
  83. }catch(Exception $e){
  84. return $e->getMessage();
  85. }
  86. }
  87. }
  88. ?>