Exemplo n.º 1
0
 /**
  * send email
  *
  * @param        $fromemail
  * @param        $accessToken
  * @param        $email_type
  * @param        $imap_server
  * @param        $subject
  * @param        $body
  * @param        $toEmail
  * @param        $ccEmail
  * @param        $bccEmail
  * @param        $attachemnts
  * @param string $mailtype
  *
  * @return bool|void
  *
  * @since rt-Helpdesk 0.1
  */
 public function sendemail($fromname, $fromemail, $accessToken, $email_type, $imap_server, $subject, $body, $toEmail, $ccEmail, $bccEmail, $attachemnts, $email = null, $mailtype = 'notification')
 {
     set_time_limit(0);
     if (!$this->try_imap_login($fromemail, $accessToken, $email_type, $imap_server)) {
         return false;
     }
     $transport = new SmtpTransport();
     $smtp_args = array();
     switch ($email_type) {
         case 'goauth':
             $smtp_args['name'] = 'gmail-smtp';
             $smtp_args['host'] = 'smtp.gmail.com';
             $smtp_args['port'] = 465;
             $smtp_args['connection_class'] = 'oauth2';
             $smtp_args['connection_config'] = array('xoauth2_request' => $this->authString, 'ssl' => 'ssl');
             break;
         case 'imap':
             global $rt_imap_server_model;
             $server = $rt_imap_server_model->get_server_by_id($imap_server);
             if (empty($server)) {
                 echo 'Mail Server Not Found. Invalid Server id.';
                 return false;
             }
             $smtp_args['name'] = $server->outgoing_smtp_server;
             $smtp_args['host'] = $server->outgoing_smtp_server;
             $smtp_args['port'] = $server->outgoing_smtp_port;
             $smtp_args['connection_class'] = 'login';
             $smtp_args['connection_config'] = array('username' => $fromemail, 'password' => rtmb_encrypt_decrypt($accessToken), 'ssl' => $server->outgoing_smtp_enc);
             break;
         default:
             break;
     }
     $options = new SmtpOptions($smtp_args);
     $transport->setOptions($options);
     $message = new Message();
     $message->addFrom($fromemail, $fromname);
     if (!empty($email)) {
         $message_id = $reference_id = $in_reply_to = '';
         if ('comment' == $email->refrence_type) {
             $message_id = get_comment_meta($email->refrence_id, '_rtlib_messageid', true);
             $reference_id = get_comment_meta($email->refrence_id, '_rtlib_references', true);
             if (empty($message_id)) {
                 $comment = get_comment($email->refrence_id);
                 $post_id = $comment->comment_post_ID;
             }
         } else {
             if ('post' == $email->refrence_type) {
                 $post_id = $email->refrence_id;
             }
         }
         if (isset($post_id)) {
             $reference_id = get_post_meta($post_id, '_rtlib_references', true);
             $message_id = rtmb_get_reply_to_from_ref_id($reference_id);
             $reply_to = apply_filters('rtlib_reply_to_header', '', $fromemail, $post_id);
             if (!empty($reply_to)) {
                 $message->addCustomeHeader('Reply-To', trim($reply_to));
             }
         }
         //Get reply to header
         if (!empty($message_id)) {
             $message->addCustomeHeader('In-Reply-To', trim($message_id));
         }
         //Get References header
         if (!empty($message_id)) {
             $reference_id = rtmb_add_message_id_in_ref_id($message_id, $reference_id);
         }
         if (!empty($reference_id)) {
             $reference_ids = rtmb_get_reference_id_array($reference_id);
             $_reference_id = implode(' ', $reference_ids);
             $message->addCustomeHeader('References', $_reference_id);
         }
         // Add x-mailer
         if (!empty($email->refrence_id)) {
             $message->addCustomeHeader('X-Mailer', 'rtCamp-mail-lib');
             if ('comment' == $email->refrence_type) {
                 $comment = get_comment($email->refrence_id);
                 $post_id = $comment->comment_post_ID;
             } else {
                 $post_id = $email->refrence_id;
             }
             $new_message_id = rtmb_generate_message_id($post_id, $email->id);
             rtmb_add_message_id_in_ref_id($new_message_id, $reference_id, $post_id);
             $message->addCustomeHeader('Message-ID', $new_message_id);
         }
     }
     //$mail->setFrom($fromemail);
     $message->setSubject(stripslashes_deep(html_entity_decode($subject, ENT_QUOTES, 'UTF-8')));
     //$mail->setSubject($subject);
     if (!empty($toEmail)) {
         foreach ($toEmail as $temail) {
             //$mail->addTo($temail["email"], isset($temail["name"]) ? $temail["name"] : '');
             $message->addTo($temail['email'], isset($temail['name']) ? $temail['name'] : '');
         }
     }
     if (!empty($ccEmail)) {
         foreach ($ccEmail as $temail) {
             //$mail->addCc($temail["email"], isset($temail["name"]) ? $temail["name"] : '');
             $message->addCc($temail['email'], isset($temail['name']) ? $temail['name'] : '');
         }
     }
     if (!empty($bccEmail)) {
         foreach ($bccEmail as $temail) {
             if (isset($temail['email'])) {
                 $message->addBcc($temail['email'], isset($temail['name']) ? $temail['name'] : '');
             }
         }
     }
     // create a MimeMessage object that will hold the mail body and any attachments
     $bodyPart = new MimeMessage();
     $bodyMessage = new MimePart($body);
     $bodyMessage->type = 'text/html';
     $bodyMessage->encoding = Mime::ENCODING_QUOTEDPRINTABLE;
     $bodyPart->addPart($bodyMessage);
     if (!empty($attachemnts) && is_array($attachemnts)) {
         foreach ($attachemnts as $attach) {
             $file_array = explode('/', $attach);
             $fileName = $file_array[count($file_array) - 1];
             $attachment = new MimePart(file_get_contents($attach));
             $attachment->type = rtmb_get_mime_type($attach);
             $attachment->filename = $fileName;
             $attachment->encoding = Zend\Mime\Mime::ENCODING_BASE64;
             $attachment->disposition = Zend\Mime\Mime::DISPOSITION_ATTACHMENT;
             $bodyPart->addPart($attachment);
         }
     }
     $message->setBody($bodyPart);
     $transport->send($message);
     return true;
 }
Exemplo n.º 2
0
 /**
  * create new mailbox
  *
  * @param $obj_data
  *
  * @return bool
  */
 function rtmailbox_add_mailbox($obj_data)
 {
     global $rt_mail_settings;
     $result = array();
     $result['status'] = false;
     if (!empty($obj_data['email']) && !empty($obj_data['password']) && !empty($obj_data['provider'])) {
         $email = $obj_data['email'];
         $password = $obj_data['password'];
         $email_data = array('email' => $email, 'mail_folders' => 'INBOX');
         $email_type = 'imap';
         $module = $obj_data['module'];
         $imap_server = $obj_data['provider'];
         $hdZendEmail = new Rt_Zend_Mail();
         try {
             if (!$hdZendEmail->try_imap_login($email, rtmb_encrypt_decrypt($password), $email_type, $imap_server)) {
                 $result['error'] = 'Error: login failed. Please enter correct credential or enable IMAP in your mailbox';
             } else {
                 if (!empty($obj_data['mailboxid'])) {
                     $mailboxid = $rt_mail_settings->update_user_google_ac(rtmb_encrypt_decrypt($password), $email, '', '', $email_type, $imap_server, $module, $obj_data['mailboxid']);
                     if ($mailboxid) {
                         $mailboxid = $obj_data['mailboxid'];
                     }
                 } else {
                     $mailboxid = $rt_mail_settings->add_user_google_ac(rtmb_encrypt_decrypt($password), $email, maybe_serialize($email_data), '', $email_type, $imap_server, $module);
                     do_action('rt_mailbox_add_mailbox', $email, $module);
                 }
                 if (!empty($mailboxid)) {
                     ob_start();
                     $this->render_list_mailbox_page($module, $mailboxid);
                     $result['html_list'] = ob_get_clean();
                     $result['moduleid'] = $mailboxid;
                     $result['status'] = true;
                 } else {
                     if (!empty($obj_data['mailboxid'])) {
                         $result['error'] = 'Error: Mailbox configured not updated';
                     } else {
                         $result['error'] = 'Error: Mailbox not configured';
                     }
                 }
             }
         } catch (Exception $e) {
             $result['error'] = 'Caught exception: ' . $e->getMessage();
         }
     } else {
         $result['error'] = 'Error: Required mailbox field missing.';
     }
     return $result;
 }