示例#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;
 }
function rtmb_get_reply_to_from_ref_id($reference_id)
{
    if (empty($reference_id)) {
        return '';
    }
    $reference_ids = rtmb_get_reference_id_array($reference_id);
    return end($reference_ids);
}