Posted By

ryarwood on 06/18/11


Tagged

form php mime contact FormMail


Versions (?)

p11creative email class


 / Published in: PHP
 

  1. <?php
  2. # | p11creative mime email class
  3. # | $Id: mime.php,v 1.1.1.1 2004/01/21 00:17:16 ryan Exp $
  4.  
  5. # | define a few MIME types
  6. define('BASE64', 'base64');
  7. define('BIT7', '7bit');
  8. define('QP', 'quoted_printable');
  9. define('NOSUBJECT', '(No Subject)');
  10. define('WARNING', 'This is a MIME encoded message');
  11. define('OCTET', 'application/octet-stream');
  12. define('TEXT', 'text/plain');
  13. define('HTML', 'text/html');
  14. define('PNG', 'image/png');
  15. define('PDF', 'application/pdf');
  16. define('JPEG', 'image/jpg');
  17. define('GIF', 'image/gif');
  18. define('CRLF', "
  19. ");
  20. define('CHARSET', 'us-ascii');
  21. define('INLINE', 'inline');
  22. define('ATTACH', 'attachment');
  23. define('BODY', CRLF.'BODY'.CRLF);
  24.  
  25. class MIME_mail {
  26. var $to;
  27. var $from;
  28. var $subject;
  29. var $body;
  30. var $headers = "";
  31. var $errstr="";
  32.  
  33. var $base64_func= ''; # | use default php base64 function
  34. var $qp_func = ''; # | leave blank for now
  35. var $mailer = ""; # | set to alternate mailer
  36.  
  37. # | private:
  38. var $mimeparts = array();
  39.  
  40. function MIME_mail($from="", $to="", $subject="", $body="", $headers = "") {
  41. $this->to = $to;
  42. $this->from = $from;
  43. $this->subject = $subject;
  44. $this->body = $body;
  45.  
  46. if (is_array($headers)) {
  47. if (sizeof($headers)>1)
  48. $headers=join(CRLF, $headers);
  49. else
  50. $headers=$headers[0];
  51. }
  52.  
  53. if ($from) {
  54. $headers = preg_replace("!(from:\ ?.+?[
  55. ]?\b)!i", '', $headers);
  56. }
  57.  
  58. $this->headers = chop($headers);
  59. $this->mimeparts[] = "";
  60. $this->errstr = "";
  61. return;
  62. }
  63.  
  64. # | Attach a 'file' to e-mail message
  65. # | Pass a file name to attach.
  66. # | This function returns a success/failure code/key of current
  67. # | attachment in array (+1). Read attach() below.
  68. function fattach($path, $description = "", $contenttype = OCTET, $encoding = BASE64, $disp = '') {
  69. $this->errstr = "";
  70. if (!file_exists($path)) {
  71. $this->errstr = printf("File '%s' does not exist", $path);
  72. return 0;
  73. }
  74.  
  75. $fp = fopen($path, "rb");
  76. if (!$fp) {
  77. $this->errstr = "fopen() failed";
  78. return 0;
  79. }
  80.  
  81. $contenttype .= ";
  82. \tname=".basename($path);
  83. $data = fread($fp, filesize($path));
  84. return $this->attach($data,
  85. $description,
  86. $contenttype,
  87. $encoding,
  88. $disp);
  89. }
  90.  
  91. # | Attach data provided by user (rather than a file)
  92. # | Useful when you want to MIME encode user input
  93. # | like HTML. NOTE: This function returns key at which the requested
  94. # | data is attached. IT IS CURRENT KEY VALUE + 1!!
  95. # | Construct the body with MIME parts
  96. function attach($data, $description = "", $contenttype = OCTET, $encoding = BASE64, $disp = '') {
  97. $this->errstr = "";
  98.  
  99. if (empty($data)) {
  100. $this->errstr = "No data to be attached";
  101. return 0;
  102. }
  103.  
  104. if (trim($contenttype) == '') $contenttype = OCTET ;
  105. if (trim($encoding) == '') $encoding = BASE64;
  106. if ($encoding == BIT7) $emsg = $data;
  107. elseif ($encoding == QP)
  108. $emsg = $$this->qp_func($data);
  109. elseif ($encoding == BASE64) {
  110. if (!$this->base64_func)
  111. $emsg = base64_encode($data);
  112. else
  113. $emsg = $$this->base64_func($data);
  114. }
  115.  
  116. if ($contenttype != (HTML || TEXT)) {
  117. $emsg = chunk_split($emsg);
  118. }
  119.  
  120. # | Check if content-type is text/plain and if charset is not specified append default CHARSET
  121. if (preg_match("!^".TEXT."!i", $contenttype) && !preg_match("!;charset=!i", $contenttype))
  122. $contenttype .= ";
  123. \tcharset=".CHARSET ;
  124.  
  125. $msg = sprintf("Content-Type: %sContent-Transfer-Encoding: %s%s%s%s",
  126. $contenttype.CRLF,
  127. $encoding.CRLF,
  128. ((($description) && (BODY != $description))?"Content-Description: $description".CRLF:""),
  129. ($disp?"Content-Disposition: $disp".CRLF:""),
  130. CRLF.$emsg.CRLF);
  131.  
  132. BODY == $description? $this->mimeparts[0] = $msg: $this->mimeparts[] = $msg ;
  133.  
  134. return sizeof($this->mimeparts);
  135. }
  136.  
  137. # | Construct mail message header from info already given.
  138. # | This is a very important function. It shows how exactly
  139. # | the MIME message is constructed.
  140. function build_message() {
  141. $this->errstr = "";
  142. $msg = "";
  143. $boundary = 'PM'.chr(rand(65, 91)).'------'.md5(uniqid(rand())); # Boundary marker
  144. $nparts = sizeof($this->mimeparts);
  145.  
  146. # | Attachment list is there. Therefore MIME Message header must have multipart/mixed
  147. if (is_array($this->mimeparts) && ($nparts > 1)):
  148. $c_ver = "MIME-Version: 1.0".CRLF;
  149. $c_type = 'Content-Type: multipart/mixed;'.CRLF."\tboundary=\"$boundary\"".CRLF;
  150. $c_enc = "Content-Transfer-Encoding: ".BIT7.CRLF;
  151. $c_desc = $c_desc?"Content-Description: $c_desc".CRLF:"";
  152. $warning = CRLF.WARNING.CRLF.CRLF ;
  153.  
  154. # | Since we are here, it means we do have attachments => body must become an attachment too.
  155. if (!empty($this->body)) {
  156. $this->attach($this->body, BODY, TEXT, BIT7);
  157. }
  158.  
  159. # | Now create the MIME parts of the email!
  160. for ($i=0 ; $i < $nparts; $i++) {
  161. if (!empty($this->mimeparts[$i]))
  162. $msg .= CRLF.'--'.$boundary.CRLF.$this->mimeparts[$i].CRLF;
  163. }
  164. $msg .= '--'.$boundary.'--'.CRLF;
  165. $msg = $c_ver.$c_type.$c_enc.$c_desc.$warning.$msg;
  166. else:
  167. if (!empty($this->body)) $msg .= $this->body.CRLF.CRLF;
  168. endif;
  169.  
  170. return $msg;
  171. }
  172.  
  173.  
  174. # | Now Generate the entire Mail Message, header and body et al.
  175. function gen_email($force=false) {
  176. $this->errstr = "";
  177. if (!empty($this->email) && !$force) return $this->email ; # | saves processing
  178. $email = "";
  179. if (empty($this->subject)) $this->subject = NOSUBJECT;
  180. if (!empty($this->from)) $email .= 'From: '.$this->from.CRLF;
  181. if (!empty($this->headers)) $email .= $this->headers.CRLF;
  182. $email .= $this->build_message();
  183. $this->email = $email;
  184.  
  185. return $this->email;
  186. }
  187.  
  188. # | Printable form
  189. function print_mail($force=false) {
  190. $this->errstr = "";
  191. $email = $this->gen_email($force);
  192. if (!empty($this->to)) $email = 'To: '.$this->to.CRLF.$email;
  193. if (!empty($this->subject)) $email = 'Subject: '.$this->subject.CRLF.$email;
  194.  
  195. print $email;
  196. }
  197.  
  198. # | Send mail via local mailer
  199. function send_mail($force=false) {
  200. $this->errstr = "";
  201. $email = $this->gen_email($force);
  202.  
  203. if (empty($this->to)) {
  204. $this->errstr = "To Address not specified";
  205. return 0;
  206. }
  207.  
  208. if (is_array($this->mailer) && (1 == sizeof($this->mailer)) ) {
  209. $mail_obj = $this->mailer['name'];
  210. $mail_method = $this->mailer['method'];
  211.  
  212. if (empty($mail_obj)) {
  213. $this->errstr = "Invalid object name passed to send_mail()";
  214. return 0;
  215. }
  216.  
  217. global $mail_obj;
  218. eval("$ret = \$$mail_obj".'->'."$mail_method;");
  219. return $ret;
  220. }
  221.  
  222. return mail($this->to, $this->subject, "", $email);
  223. }
  224.  
  225. }
  226.  
  227. ?>

Report this snippet  

You need to login to post a comment.