Пример #1
0
    /**
     * handleEmailFormInput
     * 
     * This method can be used in Models and Controllers. It puts the email body
     * and inline (image) attachments from the client in the message, which can
     * then be used for storage in the database or sending emails.
     * 
     * @param Array $params Must contain elements: body (string) and
     * 
     * inlineAttachments (string).
     */
    public function handleEmailFormInput($params)
    {
        if (!empty($params['subject'])) {
            $this->setSubject($params['subject']);
        }
        if (!empty($params['to'])) {
            $to = new EmailRecipients($params['to']);
            foreach ($to->getAddresses() as $email => $personal) {
                $this->addTo($email, $personal);
            }
        }
        if (!empty($params['cc'])) {
            $cc = new EmailRecipients($params['cc']);
            foreach ($cc->getAddresses() as $email => $personal) {
                $this->addCc($email, $personal);
            }
        }
        if (!empty($params['bcc'])) {
            $bcc = new EmailRecipients($params['bcc']);
            foreach ($bcc->getAddresses() as $email => $personal) {
                $this->addBcc($email, $personal);
            }
        }
        if (isset($params['alias_id'])) {
            $alias = \GO\Email\Model\Alias::model()->findByPk($params['alias_id']);
            $this->setFrom($alias->email, $alias->name);
            if (!empty($params['notification'])) {
                $this->setReadReceiptTo(array($alias->email => $alias->name));
            }
        }
        if (isset($params['priority'])) {
            $this->setPriority($params['priority']);
        }
        if (isset($params['in_reply_to'])) {
            $headers = $this->getHeaders();
            $headers->addTextHeader('In-Reply-To', $params['in_reply_to']);
            $headers->addTextHeader('References', $params['in_reply_to']);
        }
        if ($params['content_type'] == 'html') {
            $params['htmlbody'] = $this->_embedPastedImages($params['htmlbody']);
            //inlineAttachments is an array(array('url'=>'',tmp_file=>'relative/path/');
            if (!empty($params['inlineAttachments'])) {
                $inlineAttachments = json_decode($params['inlineAttachments']);
                /* inline attachments must of course exist as a file, and also be used in
                 * the message body
                 */
                if (count($inlineAttachments)) {
                    foreach ($inlineAttachments as $ia) {
                        //$tmpFile = new \GO\Base\Fs\File(\GO::config()->tmpdir.$ia['tmp_file']);
                        if (empty($ia->tmp_file)) {
                            continue;
                            // Continue to the next inline attachment for processing.
                            //throw new Exception("No temp file for inline attachment ".$ia->name);
                        }
                        $path = empty($ia->from_file_storage) ? \GO::config()->tmpdir . $ia->tmp_file : \GO::config()->file_storage_path . $ia->tmp_file;
                        $tmpFile = new \GO\Base\Fs\File($path);
                        if ($tmpFile->exists()) {
                            //Different browsers reformat URL's to absolute or relative. So a pattern match on the filename.
                            //$filename = rawurlencode($tmpFile->name());
                            $result = preg_match('/="([^"]*' . preg_quote($ia->token) . '[^"]*)"/', $params['htmlbody'], $matches);
                            if ($result) {
                                $img = \Swift_EmbeddedFile::fromPath($tmpFile->path());
                                $img->setContentType($tmpFile->mimeType());
                                $contentId = $this->embed($img);
                                //$tmpFile->delete();
                                $params['htmlbody'] = \GO\Base\Util\String::replaceOnce($matches[1], $contentId, $params['htmlbody']);
                            } else {
                                //this may happen when an inline image was attached but deleted in the editor afterwards.
                                //
                                //throw new \Exception("Error: inline attachment could not be found in text: ".$ia->token);
                            }
                        } else {
                            throw new \Exception("Error: inline attachment missing on server: " . $tmpFile->stripTempPath() . ".<br /><br />The temporary files folder is cleared on each login. Did you relogin?");
                        }
                    }
                }
            }
            $params['htmlbody'] = $this->_fixRelativeUrls($params['htmlbody']);
            $htmlTop = '<html>
<head>
<style type="text/css">
body,p,td,div,span{
	' . \GO::config()->html_editor_font . '
};
body p{
	margin:0px;
}
</style>
</head>
<body>';
            $htmlBottom = '</body></html>';
            $this->setHtmlAlternateBody($htmlTop . $params['htmlbody'] . $htmlBottom);
        } else {
            $this->setBody($params['plainbody'], 'text/plain');
        }
        if (!empty($params['attachments'])) {
            $attachments = json_decode($params['attachments']);
            foreach ($attachments as $att) {
                $path = empty($att->from_file_storage) ? \GO::config()->tmpdir . $att->tmp_file : \GO::config()->file_storage_path . $att->tmp_file;
                $tmpFile = new \GO\Base\Fs\File($path);
                if ($tmpFile->exists()) {
                    $file = \Swift_Attachment::fromPath($tmpFile->path());
                    $file->setContentType($tmpFile->mimeType());
                    $file->setFilename($att->fileName);
                    $this->attach($file);
                    //$tmpFile->delete();
                } else {
                    throw new \Exception("Error: attachment missing on server: " . $tmpFile->stripTempPath() . ".<br /><br />The temporary files folder is cleared on each login. Did you relogin?");
                }
            }
        }
    }