diff --git a/resources/classes/email.php b/resources/classes/email.php new file mode 100644 index 0000000000..b21ac48744 --- /dev/null +++ b/resources/classes/email.php @@ -0,0 +1,422 @@ + + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +/** + * email class + * + * @method boolean send + */ +if (!class_exists('email')) { + class email { + + /** + * declare the variables + */ + private $app_name; + private $app_uuid; + private $name; + + public $domain_uuid; + public $method; + public $recipients; + public $subject; + public $body; + public $from_address; + public $from_name; + public $priority; + public $debug_level; + public $attachments; + public $read_confirmation; + public $error; + public $response; + + /** + * called when the object is created + */ + public function __construct() { + //assign the variables + $this->app_name = 'email'; + $this->name = 'email'; + //$this->app_uuid = 'zzz'; + $this->priority = 3; + $this->debug_level = 0; + $this->read_confirmation = false; + } + + /** + * called when there are no references to a particular object + * unset the variables used in the class + */ + public function __destruct() { + foreach ($this as $key => $value) { + unset($this->$key); + } + } + + /** + * send emails + */ + public function send() { + + //set the send_method if not already set + if (!isset($this->method)) { + if ($_SESSION['email_queue']['enabled']['boolean'] == 'true') { + $this->method = 'queue'; + } + else { + $this->method = 'direct'; + } + } + + //add the email to the queue + if ($this->method == 'queue') { + + //set the domain_uuid if not set + if (!isset($this->domain_uuid)) { + $this->domain_uuid = $_SESSION['domain_uuid']; + } + + //add the email_queue_uuid + $email_queue_uuid = uuid(); + + //prepare the array + $array['email_queue'][0]['email_queue_uuid'] = $email_queue_uuid; + $array['email_queue'][0]['domain_uuid'] = $this->domain_uuid; + $array['email_queue'][0]['hostname'] = gethostname(); + $array['email_queue'][0]['email_date'] = 'now()'; + $array['email_queue'][0]['email_from'] = $this->from_address; + $array['email_queue'][0]['email_to'] = $this->recipients; + $array['email_queue'][0]['email_subject'] = $this->subject; + $array['email_queue'][0]['email_body'] = $this->body; + $array['email_queue'][0]['email_status'] = 'waiting'; + $array['email_queue'][0]['email_retry_count'] = null; + //$array['email_queue'][0]['email_action_before'] = $email_action_before; + //$array['email_queue'][0]['email_action_after'] = $email_action_after; + + //add email attachments + if (is_array($this->attachments) && sizeof($this->attachments) > 0) { + $y = 0; + foreach ($this->attachments as $attachment) { + //set the name of the file + $attachment['name'] = $attachment['name'] != '' ? $attachment['name'] : basename($attachment['value']); + + //add the attachments to the array + $array['email_queue_attachments'][$y]['email_queue_attachment_uuid'] = uuid(); + $array['email_queue_attachments'][$y]['email_queue_uuid'] = $email_queue_uuid; + $array['email_queue_attachments'][$y]['domain_uuid'] = $_SESSION['domain_uuid']; + $array['email_queue_attachments'][$y]['email_attachment_type'] = $email_attachment_type; + $array['email_queue_attachments'][$y]['email_attachment_path'] = $email_attachment_path; + $array['email_queue_attachments'][$y]['email_attachment_name'] = $email_attachment_name; + + if ($attachment['type'] == 'file' || $attachment['type'] == 'path') { + $array['email_queue_attachments'][$y]['email_attachment_path'] = $attachment['value']; + } + else if ($attachment['type'] == 'string') { + $array['email_queue_attachments'][$y]['email_attachment_base64'] = base64_decode($attachment['value']); + } + $y++; + } + } + + //add temporary permissions + $p = new permissions; + $p->add("email_queue_add", 'temp'); + $p->add("email_queue_attachment_add", 'temp'); + + //save the dialplan + $database = new database; + $database->app_name = 'email'; + $database->app_uuid = 'e24b5dab-3bcc-42e8-99c1-19b0c558c2d7'; + $database->save($array); + //$dialplan_response = $database->message; + unset($array); + + //remove temporary permissions + $p->delete("dialplan_add", 'temp'); + $p->delete("dialplan_detail_add", 'temp'); + + } + + //send the email directly + if ($this->method == 'direct') { + /* + RECIPIENTS NOTE: + + Pass in a single email address... + + user@domain.com + + Pass in a comma or semi-colon delimited string of e-mail addresses... + + user@domain.com,user2@domain2.com,user3@domain3.com + user@domain.com;user2@domain2.com;user3@domain3.com + + Pass in a simple array of email addresses... + + Array ( + [0] => user@domain.com + [1] => user2@domain2.com + [2] => user3@domain3.com + ) + + Pass in a multi-dimentional array of addresses (delivery, address, name)... + + Array ( + [0] => Array ( + [delivery] => to + [address] => user@domain.com + [name] => user 1 + ) + [1] => Array ( + [delivery] => cc + [address] => user2@domain2.com + [name] => user 2 + ) + [2] => Array ( + [delivery] => bcc + [address] => user3@domain3.com + [name] => user 3 + ) + ) + + ATTACHMENTS NOTE: + + Pass in as many files as necessary in an array in the following format... + + Array ( + [0] => Array ( + [type] => file (or 'path') + [name] => filename.ext + [value] => /folder/filename.ext + ) + [1] => Array ( + [type] => string + [name] => filename.ext + [value] => (string of file contents - if base64, will be decoded automatically) + ) + ) + + ERROR RESPONSE: + + Error messages are stored in the variable passed into $this->error BY REFERENCE + */ + + try { + //include the phpmailer classes + include_once("resources/phpmailer/class.phpmailer.php"); + include_once("resources/phpmailer/class.smtp.php"); + + //create the email object and set general settings + $mail = new PHPMailer(); + $mail->IsSMTP(); + if ($_SESSION['email']['smtp_hostname']['text'] != '') { + $mail->Hostname = $_SESSION['email']['smtp_hostname']['text']; + } + $mail->Host = $_SESSION['email']['smtp_host']['text']; + if (is_numeric($_SESSION['email']['smtp_port']['numeric'])) { + $mail->Port = $_SESSION['email']['smtp_port']['numeric']; + } + + if ($_SESSION['email']['smtp_auth']['text'] == "true") { + $mail->SMTPAuth = true; + $mail->Username = $_SESSION['email']['smtp_username']['text']; + $mail->Password = $_SESSION['email']['smtp_password']['text']; + } + else { + $mail->SMTPAuth = false; + } + + $smtp_secure = true; + if ($_SESSION['email']['smtp_secure']['text'] == "") { + $mail->SMTPSecure = 'none'; + $mail->SMTPAutoTLS = false; + $smtp_secure = false; + } + elseif ($_SESSION['email']['smtp_secure']['text'] == "none") { + $mail->SMTPSecure = 'none'; + $mail->SMTPAutoTLS = false; + $smtp_secure = false; + } + else { + $mail->SMTPSecure = $_SESSION['email']['smtp_secure']['text']; + } + + if ($smtp_secure && isset($_SESSION['email']['smtp_validate_certificate']) && $_SESSION['email']['smtp_validate_certificate']['boolean'] == "false") { + //bypass certificate check e.g. for self-signed certificates + $smtp_options['ssl']['verify_peer'] = false; + $smtp_options['ssl']['verify_peer_name'] = false; + $smtp_options['ssl']['allow_self_signed'] = true; + } + + //used to set the SSL version + if ($smtp_secure && isset($_SESSION['email']['smtp_crypto_method'])) { + $smtp_options['ssl']['crypto_method'] = $_SESSION['email']['smtp_crypto_method']['text']; + } + + //add SMTP Options if the array exists + if (is_array($smtp_options)) { + $mail->SMTPOptions = $smtp_options; + } + + $this->from_address = ($this->from_address != '') ? $this->from_address : $_SESSION['email']['smtp_from']['text']; + $this->from_name = ($this->from_name != '') ? $this->from_name : $_SESSION['email']['smtp_from_name']['text']; + $mail->SetFrom($this->from_address, $this->from_name); + $mail->AddReplyTo($this->from_address, $this->from_name); + $mail->Subject = $this->subject; + $mail->MsgHTML($this->body); + $mail->Priority = $this->priority; + if ($this->read_confirmation) { + $mail->AddCustomHeader('X-Confirm-Reading-To: '.$this->from_address); + $mail->AddCustomHeader('Return-Receipt-To: '.$this->from_address); + $mail->AddCustomHeader('Disposition-Notification-To: '.$this->from_address); + } + if (is_numeric($this->debug_level) && $this->debug_level > 0) { + $mail->SMTPDebug = $this->debug_level; + } + + //add the email recipients + $address_found = false; + if (!is_array($this->recipients)) { // must be a single or delimited recipient address(s) + $this->recipients = str_replace(' ', '', $this->recipients); + $this->recipients = str_replace(',', ';', $this->recipients); + $this->recipients = explode(';', $this->recipients); // convert to array of addresses + } + + foreach ($this->recipients as $this->recipient) { + if (is_array($this->recipient)) { // check if each recipient has multiple fields + if ($this->recipient["address"] != '' && valid_email($this->recipient["address"])) { // check if valid address + switch ($this->recipient["delivery"]) { + case "cc" : $mail->AddCC($this->recipient["address"], ($this->recipient["name"]) ? $this->recipient["name"] : $this->recipient["address"]); break; + case "bcc" : $mail->AddBCC($this->recipient["address"], ($this->recipient["name"]) ? $this->recipient["name"] : $this->recipient["address"]); break; + default : $mail->AddAddress($this->recipient["address"], ($this->recipient["name"]) ? $this->recipient["name"] : $this->recipient["address"]); + } + $address_found = true; + } + } + else if ($this->recipient != '' && valid_email($this->recipient)) { // check if recipient value is simply (only) an address + $mail->AddAddress($this->recipient); + $address_found = true; + } + } + + if (!$address_found) { + $this->error = "No valid e-mail address provided."; + return false; + } + + //add email attachments + if (is_array($this->attachments) && sizeof($this->attachments) > 0) { + foreach ($this->attachments as $attachment) { + + //set the type if not set + if (!isset($attachment['type'])) { + if (strlen($attachment['value']) < 255 && file_exists($attachment['value'])) { + $attachment['type'] = 'file'; + } + else { + $attachment['type'] = 'string'; + } + } + + //set the name of the file + $attachment['name'] = $attachment['name'] != '' ? $attachment['name'] : basename($attachment['value']); + + //set the mime type + switch (substr($attachment['name'], -4)) { + case ".png": + $attachment['mime_type'] = 'image/png'; + break; + case ".pdf": + $attachment['mime_type'] = 'application/pdf'; + break; + case ".mp3": + $attachment['mime_type'] = 'audio/mpeg'; + break; + case ".wav": + $attachment['mime_type'] = 'audio/x-wav'; + break; + case ".opus": + $attachment['mime_type'] = 'audio/opus'; + break; + case ".ogg": + $attachment['mime_type'] = 'audio/ogg'; + break; + } + + //add the attachments + if ($attachment['type'] == 'file' || $attachment['type'] == 'path') { + $mail->AddAttachment($attachment['value'], $attachment['name'], 'base64', $attachment['mime_type']); + } + else if ($attachment['type'] == 'string') { + if (base64_encode(base64_decode($attachment['value'], true)) === $attachment['value']) { + $mail->AddStringAttachment(base64_decode($attachment['value']), $attachment['name'], 'base64', $attachment['mime_type']); + } + else { + $mail->AddStringAttachment($attachment['value'], $attachment['name'], 'base64', $attachment['mime_type']); + } + } + } + } + + //send the email + if (!$mail->Send()) { + if (isset($mail->ErrorInfo) && strlen($mail->ErrorInfo) > 0) { + $this->error = $mail->ErrorInfo; + } + return false; + } + + //cleanup the mail object + $mail->ClearAddresses(); + $mail->SmtpClose(); + unset($mail); + return true; + + } + catch (Exception $e) { + $this->error = $mail->ErrorInfo; + return false; + } + + } + } + + } +} + +/* +$email = new email; +$email->recipients = $recipients; +$email->subject = $email_subject; +$email->body = $email_body; +$email->from_address = $email_from_address; +$email->from_name = $email_from_name; +$email->attachments = $email_attachments; +$response = $mail->error; +$sent = $email->send(); +*/ + +?>