/application/classes/email.php
PHP | 146 lines | 88 code | 24 blank | 34 comment | 13 complexity | c6631f4fdae3f5b8985829719b80247f MD5 | raw file
- <?php
- /* game-services
- * https://bitbucket.org/polarbearswithchainsaws/game-services
- * Copyright (c) 2013 Scott Wichser
- *
- * Licensed under the MIT License. See COPYING for license text.
- */
- namespace app\Classes;
- use mvcish\Config;
- // The Email class is used for calculating the moon phase. Nah, just kidding,
- // it's for sending email. :P
- class Email {
- var $from = "";
- var $replyto = "";
- var $to = "";
-
- var $subject = "";
- var $textBody = "";
- var $htmlBody = "";
- var $attachments = Array();
-
- function __construct($to, $subject) {
- $this->from = Config::load('main', 'fromEmail');
- $this->to = $to;
- $this->subject = $subject;
- }
-
- function setFromAddress($from) {
- $this->from = $from;
- }
-
- function setReplyToAddress($replyto) {
- $this->replyto = $replyto;
- }
-
- // Plain text body
- function setTextBody($text) {
- $this->textBody = $text;
- }
-
- // HTML body
- function setHTMLBody($html) {
- $this->htmlBody = $html;
- }
-
- // Add an attachment (file is not read until the send method is called)
- function addAttachment($filename, $displayname, $mimetype) {
- $this->attachments[] = Array(
- 'filename' => $filename,
- 'displayname' => $displayname,
- 'mimetype' => $mimetype
- );
- }
-
- // Send the message!
- function send() {
- // Plain text body is required
- if (!strlen($this->textBody)) {
- return false;
- }
-
- // Add the From header
- $headers = "From: {$this->from}";
-
- // If set, add the Reply-To header
- if (!!strlen($this->replyto)) {
- $headers .= "\r\nReply-To: {$this->from}";
- }
-
- // Check if there are attachments and/or an HTML body
- $hasAttachments = (!!sizeof($this->attachments));
- $hasHTML = (!!strlen($this->htmlBody));
-
- // If it has either attachments or an HTML body, we'll need a hash for the
- // multipart boundary
- if ($hasAttachments || $hasHTML) {
- $random_hash = md5(mcrypt_create_iv(16, MCRYPT_DEV_URANDOM));
- }
-
- // If we have attachments, this will be a multipart/mixed message. It will
- // have a multipart/alternative message embedded as well if there is an
- // HTML body
- if ($hasAttachments) {
- $headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-{$random_hash}\"";
- }
- // Otherwise, if we just have an HTML body and no attachments, it will be
- // a multipart/alternative message
- else if ($hasHTML) {
- $headers .= "\r\nContent-Type: multipart/alternative; boundary=\"PHP-alt-{$random_hash}\"";
- }
-
- // If we have either attachments or an HTML body, do the fancy processing
- if ($hasAttachments || $hasHTML) {
- $message = '';
-
- // If we have attachments, we need to start with this for containing the
- // message body/bodies
- if ($hasAttachments) {
- $message .= "--PHP-mixed-{$random_hash}\r\n";
- $message .= "Content-Type: multipart/alternative; boundary=\"PHP-alt-{$random_hash}\"\r\n\r\n";
- }
-
- // Text body first
- $message .= "--PHP-alt-{$random_hash}\r\n";
- $message .= "Content-Type: text/plain; charset=\"utf-8\"\r\n";
- $message .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
- $message .= $this->textBody;
-
- // Then HTML body if it exists
- if ($hasHTML) {
- $message .= "\r\n\r\n--PHP-alt-{$random_hash}\r\n";
- $message .= "Content-Type: text/html; charset=\"utf-8\"\r\n";
- $message .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
- $message .= $this->htmlBody;
- }
-
- // Finish off the multipart/alternative message
- $message .= "\r\n\r\n--PHP-alt-{$random_hash}";
-
- // Then add attachments if they exist
- if ($hasAttachments) {
- foreach($this->attachments as $attachment) {
- $message .= "\r\n\r\n--PHP-mixed-{$random_hash}\r\n";
- $message .= "Content-Type: {$attachment['mimetype']}; name=\"{$attachment['displayname']}\"\r\n";
- $message .= "Content-Transfer-Encoding: base64\r\n";
- $message .= "Content-Disposition: attachment\r\n\r\n";
- $message .= chunk_split(base64_encode(file_get_contents($attachment['filename'])));
- }
-
- // Finish off the multipart/mixed message
- $message .= "--PHP-mixed-{$random_hash}\r\n\r\n";
- }
- }
- // If it's just a plain text message with no attachments or HTML, we can
- // just have a nice simply message.
- else {
- $message = $this->textBody;
- }
-
- // Send it off!
- return @mail($this->to, $this->subject, $message, $headers);
- }
- }