PageRenderTime 54ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/application/classes/email.php

https://bitbucket.org/trujka/game-services
PHP | 146 lines | 88 code | 24 blank | 34 comment | 13 complexity | c6631f4fdae3f5b8985829719b80247f MD5 | raw file
  1. <?php
  2. /* game-services
  3. * https://bitbucket.org/polarbearswithchainsaws/game-services
  4. * Copyright (c) 2013 Scott Wichser
  5. *
  6. * Licensed under the MIT License. See COPYING for license text.
  7. */
  8. namespace app\Classes;
  9. use mvcish\Config;
  10. // The Email class is used for calculating the moon phase. Nah, just kidding,
  11. // it's for sending email. :P
  12. class Email {
  13. var $from = "";
  14. var $replyto = "";
  15. var $to = "";
  16. var $subject = "";
  17. var $textBody = "";
  18. var $htmlBody = "";
  19. var $attachments = Array();
  20. function __construct($to, $subject) {
  21. $this->from = Config::load('main', 'fromEmail');
  22. $this->to = $to;
  23. $this->subject = $subject;
  24. }
  25. function setFromAddress($from) {
  26. $this->from = $from;
  27. }
  28. function setReplyToAddress($replyto) {
  29. $this->replyto = $replyto;
  30. }
  31. // Plain text body
  32. function setTextBody($text) {
  33. $this->textBody = $text;
  34. }
  35. // HTML body
  36. function setHTMLBody($html) {
  37. $this->htmlBody = $html;
  38. }
  39. // Add an attachment (file is not read until the send method is called)
  40. function addAttachment($filename, $displayname, $mimetype) {
  41. $this->attachments[] = Array(
  42. 'filename' => $filename,
  43. 'displayname' => $displayname,
  44. 'mimetype' => $mimetype
  45. );
  46. }
  47. // Send the message!
  48. function send() {
  49. // Plain text body is required
  50. if (!strlen($this->textBody)) {
  51. return false;
  52. }
  53. // Add the From header
  54. $headers = "From: {$this->from}";
  55. // If set, add the Reply-To header
  56. if (!!strlen($this->replyto)) {
  57. $headers .= "\r\nReply-To: {$this->from}";
  58. }
  59. // Check if there are attachments and/or an HTML body
  60. $hasAttachments = (!!sizeof($this->attachments));
  61. $hasHTML = (!!strlen($this->htmlBody));
  62. // If it has either attachments or an HTML body, we'll need a hash for the
  63. // multipart boundary
  64. if ($hasAttachments || $hasHTML) {
  65. $random_hash = md5(mcrypt_create_iv(16, MCRYPT_DEV_URANDOM));
  66. }
  67. // If we have attachments, this will be a multipart/mixed message. It will
  68. // have a multipart/alternative message embedded as well if there is an
  69. // HTML body
  70. if ($hasAttachments) {
  71. $headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-{$random_hash}\"";
  72. }
  73. // Otherwise, if we just have an HTML body and no attachments, it will be
  74. // a multipart/alternative message
  75. else if ($hasHTML) {
  76. $headers .= "\r\nContent-Type: multipart/alternative; boundary=\"PHP-alt-{$random_hash}\"";
  77. }
  78. // If we have either attachments or an HTML body, do the fancy processing
  79. if ($hasAttachments || $hasHTML) {
  80. $message = '';
  81. // If we have attachments, we need to start with this for containing the
  82. // message body/bodies
  83. if ($hasAttachments) {
  84. $message .= "--PHP-mixed-{$random_hash}\r\n";
  85. $message .= "Content-Type: multipart/alternative; boundary=\"PHP-alt-{$random_hash}\"\r\n\r\n";
  86. }
  87. // Text body first
  88. $message .= "--PHP-alt-{$random_hash}\r\n";
  89. $message .= "Content-Type: text/plain; charset=\"utf-8\"\r\n";
  90. $message .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
  91. $message .= $this->textBody;
  92. // Then HTML body if it exists
  93. if ($hasHTML) {
  94. $message .= "\r\n\r\n--PHP-alt-{$random_hash}\r\n";
  95. $message .= "Content-Type: text/html; charset=\"utf-8\"\r\n";
  96. $message .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
  97. $message .= $this->htmlBody;
  98. }
  99. // Finish off the multipart/alternative message
  100. $message .= "\r\n\r\n--PHP-alt-{$random_hash}";
  101. // Then add attachments if they exist
  102. if ($hasAttachments) {
  103. foreach($this->attachments as $attachment) {
  104. $message .= "\r\n\r\n--PHP-mixed-{$random_hash}\r\n";
  105. $message .= "Content-Type: {$attachment['mimetype']}; name=\"{$attachment['displayname']}\"\r\n";
  106. $message .= "Content-Transfer-Encoding: base64\r\n";
  107. $message .= "Content-Disposition: attachment\r\n\r\n";
  108. $message .= chunk_split(base64_encode(file_get_contents($attachment['filename'])));
  109. }
  110. // Finish off the multipart/mixed message
  111. $message .= "--PHP-mixed-{$random_hash}\r\n\r\n";
  112. }
  113. }
  114. // If it's just a plain text message with no attachments or HTML, we can
  115. // just have a nice simply message.
  116. else {
  117. $message = $this->textBody;
  118. }
  119. // Send it off!
  120. return @mail($this->to, $this->subject, $message, $headers);
  121. }
  122. }