PageRenderTime 26ms CodeModel.GetById 34ms RepoModel.GetById 0ms app.codeStats 0ms

/reporting/includes/class.mail.inc

https://bitbucket.org/boamaod/frontaccounting
PHP | 163 lines | 119 code | 13 blank | 31 comment | 21 complexity | 774a0c5223c3943c275d26d715b9d783 MD5 | raw file
Possible License(s): GPL-3.0
  1. <?php
  2. /*
  3. Name: eMail
  4. Description: Simple sending eMail in text and HTML with CC, BCC and attachment
  5. Version: 1.0
  6. last modified: 2004-05-14
  7. Autor: Daniel Käfer
  8. Homepage: http://www.danielkaefer.de
  9. Leave this header in this file!
  10. ------------------------------------------------------------------
  11. Updated: 2010-10-25
  12. Updated by: Michael Hahn (MPH)
  13. Problem: The Suhosin patch for PHP was blocking the email before it ever reached
  14. the email server due to double line feeds (\n) in the header of the email.
  15. Also, the body of the message was included in the header. This would also
  16. trip the security measure everytime it spotted a double line feed.
  17. Fix: Remove any double line feed from the header info and seperate the body of
  18. the message from the header.
  19. Other updates: I'm not sure about RFC compliance but, every other email I've look at had
  20. certain information included in double quotes. More than likely to avoid
  21. erroneous file naming. I tried to emulate this mindset.
  22. Added line length and EOL char for file chunking. For some reason without
  23. it there were extra line feeds in the chunked file.
  24. * Lots of fixes for FA
  25. */
  26. class email
  27. {
  28. var $to = array();
  29. var $cc = array();
  30. var $bcc = array();
  31. var $attachment = array();
  32. var $boundary = "";
  33. var $header = "";
  34. var $subject = "";
  35. var $body = "";
  36. var $charset = 'ISO-8859-1';
  37. function email($name, $mail)
  38. {
  39. $this->boundary = md5(uniqid(time()));
  40. $this->header = "From: $name <$mail>\n";
  41. }
  42. function to($mail)
  43. {
  44. $this->to[] = $mail;
  45. }
  46. function cc($mail)
  47. {
  48. $this->cc[] = $mail;
  49. }
  50. function bcc($mail)
  51. {
  52. $this->bcc[] = $mail;
  53. }
  54. function attachment($file)
  55. {
  56. $this->attachment[] = $file;
  57. }
  58. function subject($subject)
  59. {
  60. $this->subject = $subject;
  61. }
  62. function text($text)
  63. {
  64. $this->body = "--$this->boundary\n";
  65. $this->body .= "Content-Type: text/plain; charset=\"{$this->charset}\"\n";
  66. $this->body .= "Content-Transfer-Encoding: 8bit\n";
  67. $this->body .= $text."\n";
  68. }
  69. function html($html)
  70. {
  71. $this->body = "--$this->boundary\n";
  72. $this->body .= "Content-Type: text/html; charset=\"{$this->charset}\"\n";
  73. $this->body .= "Content-Transfer-Encoding: quoted-printable\n";
  74. $this->body .= "<html><body>\n".$html."\n</body></html>\n";
  75. }
  76. function mime_type($filename)
  77. {
  78. $file = basename($filename, '.zip');
  79. if ($filename == $file . '.zip') return 'application/x-zip-compressed';
  80. $file = basename($filename, '.pdf');
  81. if ($filename == $file . '.pdf') return 'application/pdf';
  82. $file = basename($filename, '.csv');
  83. if ($filename == $file . '.csv') return 'application/vnd.ms-excel';
  84. $file = basename($filename, '.tar');
  85. if ($filename == $file . '.tar') return 'application/x-tar';
  86. $file = basename($filename, '.tar.gz');
  87. if ($filename == $file . '.tar.gz') return 'application/x-tar-gz';
  88. $file = basename($filename, '.tgz');
  89. if ($filename == $file . '.tgz') return 'application/x-tar-gz';
  90. $file = basename($filename, '.gz');
  91. if ($filename == $file . '.gz') return 'application/x-gzip';
  92. return 'application/unknown';
  93. }
  94. function send()
  95. {
  96. // CC Empfänger hinzufügen
  97. $max = count($this->cc);
  98. if ($max > 0)
  99. {
  100. $this->header .= "Cc: ".$this->cc[0];
  101. for ($i = 1; $i < $max; $i++)
  102. {
  103. $this->header .= ", ".$this->cc[$i];
  104. }
  105. $this->header .= "\n";
  106. }
  107. // BCC Empfänger hinzufügen
  108. $max = count($this->bcc);
  109. if ($max > 0)
  110. {
  111. $this->header .= "Bcc: ".$this->bcc[0];
  112. for ($i = 1; $i < $max; $i++)
  113. {
  114. $this->header .= ", ".$this->bcc[$i];
  115. }
  116. $this->header .= "\n";
  117. }
  118. $this->header .= "MIME-Version: 1.0\n";
  119. $this->header .= "Content-Type: multipart/mixed;\n boundary=\"$this->boundary\"\n";
  120. $this->header .= "This is a multi-part message in MIME format.\n";
  121. // Attachment hinzufügen
  122. $max = count($this->attachment);
  123. if ($max > 0)
  124. {
  125. for ($i = 0; $i < $max; $i++)
  126. {
  127. $file = fread(fopen($this->attachment[$i], "r"), filesize($this->attachment[$i]));
  128. $this->body .= "--".$this->boundary."\n";
  129. $this->body .= "Content-Type: " .$this->mime_type(basename($this->attachment[$i])). "; name=\"".basename($this->attachment[$i])."\"\n";
  130. $this->body .= "Content-Transfer-Encoding: base64\n";
  131. $this->body .= "Content-Disposition: attachment; filename=\"".basename($this->attachment[$i])."\"\n\n";
  132. $this->body .= chunk_split(base64_encode($file),"72","\n");
  133. $file = "";
  134. }
  135. }
  136. $this->body .= "--".$this->boundary."--\n";
  137. $ret = 0;
  138. foreach($this->to as $mail)
  139. {
  140. if (mail($mail, $this->subject, $this->body, $this->header))
  141. $ret++;
  142. }
  143. return $ret;
  144. }
  145. }
  146. ?>