Newer
Older
EmailSender / mail.php
0xRoM on 8 Sep 2023 7 KB first commit
  1. <?php
  2.  
  3. /**
  4. * A PHP class for sending email messages which includes capabilities for sending plaintext, html and multipart messages, including support for attachments.
  5. *
  6. * While none of the capabilities of this class exceed those of more established packages such as PEARMail, my hope is that this class offers the advantage of
  7. * portability, since not every setup has the ability to install PEAR packages. Also, this class is ideal for embedding in other systems, which I have done in
  8. * the case of my MVC framework, DirtyMVC.
  9. *
  10. * @author Steve Lewis <steve@thoughtsandrambles.com>
  11. * @version 0.1.0
  12. * @package Mail
  13. *
  14. **/
  15. class Mail{
  16. /**
  17. * @var string $to The email address, (or addresses, if comma separated), of the person to which this email should be sent.
  18. **/
  19. public $to;
  20.  
  21. /**
  22. * @var string $from The email address that this mail will be delivered from. Bear in mind that this can be anything, but that if the email
  23. * domain doesn't match the actual domain the message was sent from, some email clients will reject the message as spam.
  24. **/
  25. public $from;
  26. /**
  27. * @var string $subject The subject line of the email
  28. **/
  29. public $subject;
  30.  
  31. /**
  32. * @var string $text_content The plaintext version of the message to be sent.
  33. **/
  34. public $text_content;
  35.  
  36. /**
  37. * @var string $html_content The HTML version of the message to be sent.
  38. **/
  39. public $html_content;
  40. /**
  41. * @var string $body The complete body of the email that will be sent, including all mixed content.
  42. **/
  43. private $body;
  44.  
  45. /**
  46. * @var array $attachments An array of file paths pointing to the attachments that should be included with this email.
  47. **/
  48. private $attachments;
  49. /**
  50. * @var array $headers An array of the headers that will be included in this email.
  51. **/
  52. private $headers;
  53.  
  54. /**
  55. * @var string $header_string The string, (and therefore final), representation of the headers for this email message.
  56. **/
  57. private $header_string;
  58.  
  59. /**
  60. * @var string $boundary_hash The string that acts as a separator between the various mixed parts of the email message.
  61. **/
  62. private $boundary_hash;
  63.  
  64. /**
  65. * @var boolean $sent Whether or not this email message was successfully sent.
  66. **/
  67. private $sent;
  68.  
  69. /**
  70. * Upon initialization of a Mail object, you have to pass it certain vital pieces of information.
  71. *
  72. * At a minimum, an email must consist of a receiver address, a sender address, and a subject.
  73. * The body can be left blank.
  74. **/
  75. public function __construct($to, $from, $subject, $text_content = "", $html_content = ""){
  76. $this->to = $to;
  77. $this->from = $from;
  78. $this->subject = $this->convert_utf8($subject);
  79. $this->text_content = $text_content;
  80. $this->html_content = $html_content;
  81. $this->body = "";
  82. $this->attachments = array();
  83. $this->base64_attachments = array();
  84. $this->headers = array();
  85. $this->boundary_hash = md5(date('r', time()));
  86. }
  87.  
  88. /**
  89. * The send() method processes all headers, body elements and attachments and then actually sends the resulting final email.
  90. **/
  91. public function send(){
  92. $this->prepare_headers();
  93. $this->prepare_body();
  94. if(!empty($this->attachments)){
  95. $this->prepare_attachments();
  96. }
  97. if(!empty($this->base64_attachments)){
  98. $this->prepare_base64_attachments();
  99. }
  100. $this->sent = mail($this->to, $this->subject, $this->body, $this->header_string);
  101. return $this->sent;
  102. }
  103.  
  104. /**
  105. * This method allows the user to add a new header to the message
  106. * @param string $header The text for the header the user wants to add. Note that this string must be a properly formatted email header.
  107. **/
  108. public function add_header($header){
  109. $this->headers[] = $header;
  110. }
  111.  
  112. /**
  113. * Add a filepath to the list of files to be sent with this email.
  114. * @param string $file The path to the file that should be sent.
  115. **/
  116. public function add_attachment($file){
  117. $this->attachments[] = $file;
  118. }
  119. public function add_base64_attachment($name,$data){
  120. $this->base64_attachments[] = array('name'=>$name, 'data'=>$data);
  121. }
  122. private function prepare_body(){
  123. $this->body .= "--PHP-mixed-{$this->boundary_hash}\n";
  124. $this->body .= "Content-Type: multipart/alternative; boundary=\"PHP-alt-{$this->boundary_hash}\"\n\n";
  125. if(!empty($this->text_content)) $this->prepare_text();
  126. if(!empty($this->html_content)) $this->prepare_html();
  127. $this->body .= "--PHP-alt-{$this->boundary_hash}--\n\n";
  128. }
  129.  
  130. private function prepare_headers(){
  131. $this->set_default_headers();
  132. $this->header_string = implode(PHP_EOL, $this->headers).PHP_EOL;
  133. }
  134.  
  135. private function set_default_headers(){
  136. $this->headers[] = 'MIME-Version: 1.0';
  137. $this->headers[] = "From: {$this->from}";
  138. # We'll assume a multi-part message so that we can include an HTML and a text version of the email at the
  139. # very least. If there are attachments, we'll be doing the same thing.
  140. $this->headers[] = "Content-type: multipart/mixed; boundary=\"PHP-mixed-{$this->boundary_hash}\"";
  141. }
  142.  
  143. private function prepare_base64_attachments(){
  144. foreach($this->base64_attachments as $attachment){
  145.  
  146. $this->body .= "--PHP-mixed-{$this->boundary_hash}\n";
  147. $this->body .= "Content-Type: application/octet-stream; name=\"{$attachment['name']}\"\n";
  148. $this->body .= "Content-Transfer-Encoding: base64\n";
  149. $this->body .= "Content-Disposition: attachment\n\n";
  150. $this->body .= chunk_split($attachment['data']);
  151. $this->body .= "\n\n";
  152. }
  153. $this->body .= "--PHP-mixed-{$this->boundary_hash}--\n\n";
  154. }
  155. private function prepare_attachments(){
  156. foreach($this->attachments as $attachment){
  157. $file_name = basename($attachment);
  158.  
  159. $file_name = $this->convert_utf8($file_name);
  160. $this->body .= "--PHP-mixed-{$this->boundary_hash}\n";
  161. $this->body .= "Content-Type: application/octet-stream; name=\"{$file_name}\"\n";
  162. $this->body .= "Content-Transfer-Encoding: base64\n";
  163. $this->body .= "Content-Disposition: attachment\n\n";
  164. $this->body .= chunk_split(base64_encode(file_get_contents($attachment)));
  165. $this->body .= "\n\n";
  166. }
  167. $this->body .= "--PHP-mixed-{$this->boundary_hash}--\n\n";
  168. }
  169.  
  170. private function prepare_text(){
  171. $this->body .= "--PHP-alt-{$this->boundary_hash}\n";
  172. $this->body .= "Content-Type: text/plain; charset=\"utf-8\"\n";
  173. $this->body .= "Content-Transfer-Encoding: 8bit\n\n";
  174. $this->body .= $this->text_content."\n\n";
  175. }
  176.  
  177. private function prepare_html(){
  178. $this->body .= "--PHP-alt-{$this->boundary_hash}\n";
  179. $this->body .= "Content-Type: text/html; charset=\"utf-8\"\n";
  180. $this->body .= "Content-Transfer-Encoding: 8bit\n\n";
  181. $this->body .= $this->html_content."\n\n";
  182. }
  183. //convert to utf8
  184. private function convert_utf8($subject){
  185. return '=?UTF-8?B?'.base64_encode($subject).'?=';
  186. }
  187.  
  188. }
Buy Me A Coffee