Posted By

Cory on 07/15/08


Tagged

php mail easy attachments


Versions (?)

Who likes this?

4 people have marked this snippet as a favorite

romanos
jamesming
Jdub7
elainevc


SimpleMail


 / Published in: PHP
 

A simple very easy to use class for sending emails with / without attachments.

  1. <?php
  2. class SimpleMail {
  3. public $to;
  4. public $from;
  5. public $fromName;
  6. public $subject;
  7. public $cc;
  8. public $bcc;
  9. public $message;
  10.  
  11. private $uid;
  12. private $mailer;
  13. private $attachments;
  14.  
  15. public function __construct($to = null, $from = null, $subject = null, $message = null) {
  16. if(!empty($to) && !empty($from)) {
  17. $this->to = $to;
  18. $this->from = $from;
  19.  
  20. if(!empty($subject)) {
  21. $this->subject = $subject;
  22. }
  23. if(!empty($message)) {
  24. $this->message = $message;
  25. }
  26. }
  27.  
  28. $this->mailer = "PHP " . phpversion();
  29. $this->attachments = array();
  30. }
  31.  
  32. public function SetMailer($mailer) {
  33. if(!empty($mailer) && is_string($mailer)) {
  34. $this->mailer = $mailer;
  35. }
  36. }
  37.  
  38. public function SetTo($to) {
  39. if(!empty($to) && is_string($to)) {
  40. $this->to = $to;
  41. }
  42. }
  43.  
  44. public function SetFrom($from) {
  45. if(!empty($from) && is_string($from)) {
  46. $this->from = $from;
  47. }
  48. }
  49.  
  50. public function SetSubject($subject) {
  51. if(!empty($subject) && is_string($subject)) {
  52. $this->subject = $subject;
  53. }
  54. }
  55.  
  56. public function AddCC($email) {
  57. if(!empty($email) && is_string($email)) {
  58. if(strlen($this->cc) > 0) {
  59. $this->cc .= ",".$email;
  60. }
  61. else {
  62. $this->cc = $email;
  63. }
  64. }
  65. }
  66.  
  67. public function AddBCC($email) {
  68. if(!empty($email) && is_string($email)) {
  69. if(strlen($this->bcc) > 0) {
  70. $this->bcc .= ",".$email;
  71. }
  72. else {
  73. $this->bcc = $email;
  74. }
  75. }
  76. }
  77.  
  78. public function AddMessage($message) {
  79. if(!empty($message) && is_string($message)) {
  80. $this->message = $message;
  81. }
  82. }
  83.  
  84. public function AddAttachment($attachment) {
  85. if(file_exists($attachment)) {
  86. $attachmentInfo = array();
  87.  
  88. $this->uid = "==Multipart_Boundary_x{" . md5(uniqid(time())) . "}x";
  89. $attachmentInfo['Attachment'] = $attachment;
  90. $attachmentInfo['Size'] = filesize($attachment);
  91.  
  92. $fp = fopen($attachment, "rb");
  93. flock($fp, LOCK_SH);
  94. $data = fread($fp, filesize($attachment) + 1);
  95. flock($fp, LOCK_UN);
  96. fclose($fp);
  97.  
  98. $attachmentInfo['Content'] = chunk_split(base64_encode($data), 64);
  99.  
  100. $extStart = strrpos($this->attachment, ".");
  101. $attachmentInfo['Extension'] = substr($attachmentInfo['Attachment'], $extStart, (strlen($attachmentInfo['Attachment']) - $extStart));
  102. $attachmentInfo['Name'] = self::GenerateRandomFilename($attachmentInfo['Extension']);
  103. $attachmentInfo['Mime'] = "application/octet-stream; name=\"{$attachmentInfo['Name']}\"
  104.  
  105. ";
  106.  
  107. $this->attachments[] = $attachmentInfo;
  108. }
  109. }
  110.  
  111. public function SendMessage($to = null, $from = null, $subject = null) {
  112. if(empty($to) && !empty($this->to)) { $to = $this->to; }
  113. if(empty($from) && !empty($this->from)) { $from = $this->from; }
  114. if(empty($subject) && !empty($this->subject)) { $subject = $this->subject; }
  115. if(empty($to) || empty($from) || empty($subject)) { return false; }
  116.  
  117. $headers = "";
  118. $headers.= "From: {$this->fromName} <{$from}>
  119. ";
  120. $headers.= "Reply-To: {$to}
  121. ";
  122. $headers.= "Subject: {$subject}
  123. ";
  124. $headers.= "X-Mailer: {$this->mailer}
  125. ";
  126. $headers.= "MIME-Version: 1.0
  127. ";
  128. $headers.= "Content-Type: multipart/mixed; boundary=\"{$this->uid}\"
  129. ";
  130. $headers.= "--{$this->uid}
  131. ";
  132. $headers.= "Content-Type: text/plain; charset=\"iso-8859-1\"
  133. ";
  134. $headers.= "Content-Trasfer-Encoding: 7bit
  135.  
  136. ";
  137. $headers.= $this->message . "
  138.  
  139. ";
  140.  
  141. if(!empty($this->attachments) && is_array($this->attachments)) {
  142. foreach($this->attachments as $attactment) {
  143. $headers.= "--{$this->uid}
  144. ";
  145. $headers.= "Content-Type: application/octet-stream; name=\"{$attactment['Name']}\"
  146. ";
  147. $headers.= "Content-Transfer-Encoding: base64
  148. ";
  149. $headers.= "Content-Disposition: attachment; filename=\"{$attactment['Name']}\"
  150.  
  151. ";
  152. $headers.= $attactment['Content'] . "
  153.  
  154. ";
  155. }
  156. }
  157.  
  158. $headers.= "--{$this->uid}--";
  159.  
  160. if(mail($to, $from, "", $headers)) {
  161. return true;
  162. }
  163. return false;
  164. }
  165.  
  166. private function GenerateRandomFilename($extension) {
  167. $chars = "abcdefghijklmnopqrstuvwxyz0123456789";
  168. $salt = "";
  169.  
  170. for($i = 0; $i < mt_rand(5, 25); $i++) {
  171. $salt.= $chars{$i};
  172. }
  173.  
  174. $fname = md5($salt . time());
  175. return $fname . $extension;
  176. }
  177. }
  178. ?>

Report this snippet  

You need to login to post a comment.