Posted By

tuffo19 on 10/05/10


Tagged

email php send


Versions (?)

Invio di mail avanzato


 / Published in: PHP
 

URL: send_mail_advanced

  1. /**
  2.  * Funzione di libreria testata e stra-testata per invio di mail.
  3.  *
  4.  * @param $to
  5.  * @param $from
  6.  * @param $from_name
  7.  * @param $subject
  8.  * @param $message
  9.  * @param $reply_to
  10.  * @param $attachments
  11.  * @param $cc
  12.  * @param $bcc
  13.  */
  14. function send_email_advanced($to = null, $from, $from_name, $subject, $message, $reply_to, $attachments = false, $cc = null, $bcc = null) {
  15.  
  16. $eol = "\n";
  17.  
  18. $headers = "From: " . $from_name . "<" . $from . ">".$eol;
  19. $now=date("d-m-Y H:i:s");
  20. $today=date("Y_m_d");
  21. $from_name=str_replace(" ","_",$from_name);
  22. $log_dir="logs/email_log/";
  23. $log_file=$log_dir."email_log_{$today}_{$from_name}.html";
  24. if ($bcc != ''){
  25. $headers .= "BCC: $bcc".$eol;
  26. }
  27. $headers .= "CC: $cc".$eol;
  28. $headers .= "Reply-To: " . $reply_to . "<" . $reply_to . ">".$eol;
  29. $headers .= "Return-Path: " . $from_name . "<" . $from . ">".$eol;
  30. $headers .= "Message-ID: <" . time () . "-" . $from . ">".$eol;
  31. //$headers .= "X-Mailer: PHP v" . phpversion ();
  32. $msg_txt = "";
  33. $email_txt = $message;
  34. $semi_rand = md5 ( time () );
  35. //$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
  36. $mime_boundary = $semi_rand;
  37. $headers .= "MIME-Version: 1.0".$eol."Content-Type: multipart/mixed;".$eol." boundary=\"$mime_boundary\"".$eol.$eol;
  38. $email_txt .= $msg_txt;
  39. $email_message .= "This is a multi-part message in MIME format.".$eol.$eol;
  40. $email_message .= "--$mime_boundary".$eol;
  41. $email_message .= "Content-Type:text/html; charset=\"iso-8859-1\"".$eol.$eol;
  42. $email_message .= $email_txt.$eol.$eol;
  43. if ($attachments != false) {
  44. for($i = 0; $i < count ( $attachments ); $i ++) {
  45. if (true || is_file ( $attachments [$i] ['src'] )) {
  46. $fileatt = $attachments [$i] ['src'];
  47. $fileatt_type = "application/octet-stream";
  48. $start = strrpos ( $attachments [$i] ['src'], '/' ) == - 1 ? strrpos ( $attachments [$i] ['src'], '//' ) : strrpos ( $attachments [$i] ['src'], '/' ) + 1;
  49. $fileatt_name = $attachments [$i] ['name'];
  50. $fileatt_name = str_replace(" ","_",$fileatt_name);
  51. $file = fopen ( $fileatt, 'rb' );
  52. $data = fread ( $file, filesize ( $fileatt ) );
  53. fclose ( $file );
  54. $data = chunk_split ( base64_encode ( $data ) );
  55. $filetype=returnMIMEType($fileatt_name);
  56. // $email_message .= "Content-Type: ".mime_content_type($fileatt_name).";".$eol;
  57. $email_message .= "--$mime_boundary".$eol;
  58. $email_message .= "Content-Type: $filetype; name=\"$fileatt_name\"".$eol;
  59. $email_message .= "Content-Transfer-Encoding: base64".$eol;
  60. $email_message .= "Content-Disposition: attachment;".$eol;
  61. $email_message .= "filename=\"$fileatt_name\"".$eol.$eol;
  62. $email_message .= $data.$eol;
  63. }
  64. }
  65. }
  66. $email_message .= "--$mime_boundary--".$eol;
  67.  
  68. $log="<p align=center>Email sended at $now</p>
  69. <li>to:$to</li>
  70. <li>headers:$headers</li>
  71. <li>oggetto:$subject</li>
  72. <li>testo:$message</li>
  73. <hr>";
  74. if(!file_exists($log_dir)) {
  75. mkdir($log_dir, 0777, true) ;
  76. $flag="w";
  77. } else {
  78. $flag="a";
  79. }
  80.  
  81. // $log.=file_get_contents($log_dir);
  82. $fp=fopen ($log_file, $flag);
  83. fwrite($fp,$log);
  84. fclose($fp);
  85.  
  86. return mail ( $to, $subject, $email_message, $headers );
  87. }
  88.  
  89. /**
  90.  * Funzione di libreria di appoggio alla funzione send_mail_advanced
  91.  *
  92.  * @param unknown_type $filename
  93.  */
  94. function returnMIMEType($filename){
  95. preg_match("|\.([a-z0-9]{2,4})$|i", $filename, $fileSuffix);
  96.  
  97. switch(strtolower($fileSuffix[1])) {
  98. case "js" :
  99. return "application/x-javascript";
  100. case "json" :
  101. return "application/json";
  102. case "jpg" :
  103.  
  104. case "jpeg" :
  105. case "jpe" :
  106. return "image/jpg";
  107.  
  108. case "png" :
  109. case "gif" :
  110. case "bmp" :
  111. case "tiff" :
  112. return "image/".strtolower($fileSuffix[1]);
  113. case "css" :
  114. return "text/css";
  115. case "xml" :
  116. return "application/xml";
  117. case "doc" :
  118. case "docx" :
  119. return "application/msword";
  120.  
  121. case "xls" :
  122. case "xlt" :
  123. case "xlm" :
  124. case "xld" :
  125. case "xla" :
  126. case "xlc" :
  127. case "xlw" :
  128. case "xll" :
  129. return "application/vnd.ms-excel";
  130.  
  131. case "ppt" :
  132. case "pps" :
  133. return "application/vnd.ms-powerpoint";
  134.  
  135. case "rtf" :
  136. return "application/rtf";
  137. case "pdf" :
  138. return "application/pdf";
  139.  
  140. case "html" :
  141. case "htm" :
  142. case "php" :
  143. return "text/html";
  144.  
  145. case "txt" :
  146. return "text/plain";
  147.  
  148. case "mpeg" :
  149. case "mpg" :
  150. case "mpe" :
  151. return "video/mpeg";
  152.  
  153. case "mp3" :
  154. return "audio/mpeg3";
  155.  
  156. case "wav" :
  157. return "audio/wav";
  158. case "aiff" :
  159. case "aif" :
  160. return "audio/aiff";
  161.  
  162. case "avi" :
  163. return "video/msvideo";
  164.  
  165. case "wmv" :
  166. return "video/x-ms-wmv";
  167.  
  168. case "mov" :
  169. return "video/quicktime";
  170.  
  171. case "zip" :
  172. return "application/zip";
  173.  
  174. case "tar" :
  175. return "application/x-tar";
  176.  
  177. case "swf" :
  178. return "application/x-shockwave-flash";
  179.  
  180. default :
  181. if(function_exists("mime_content_type")){
  182. $fileSuffix = mime_content_type($filename);
  183. }
  184.  
  185. return "unknown/" . trim($fileSuffix[0], ".");
  186. }
  187. }

Report this snippet  

You need to login to post a comment.