* Mail REST Api: respect user preference to compose in HTML and convert plain body from REST Api call to HTML (bodyHtml attribute forces HTML)

This commit is contained in:
ralf 2024-02-01 13:37:11 +02:00
parent b175b14294
commit 4ce3298241
3 changed files with 24 additions and 3 deletions

View File

@ -595,4 +595,15 @@ class Html
//error_log(__METHOD__.__LINE__.array2string($html2ret));
return $html2ret;
}
/**
* Convert plain text into HTML replacing empty lines (double newline) with paragraphs and single newlines with <br>
*
* @param string $text
* @return string
*/
public static function convertTextToHtml($text)
{
return '<p>'.implode("</p>\n<p>", array_map('nl2br', preg_split("/\r?\n\r?\n/", $text)))."</p>\n";
}
}

View File

@ -1025,7 +1025,7 @@ class mail_compose
{
if ($_REQUEST['preset']['mimeType'] === 'plain')
{
$_REQUEST['preset']['body'] = '<p>'.nl2br($_REQUEST['preset']['body'])."</p>\n";
$_REQUEST['preset']['body'] = Mail\Html::convertTextToHtml($_REQUEST['preset']['body']);
}
else
{

View File

@ -109,9 +109,19 @@ class ApiHandler extends Api\CalDAV\Handler
}
}
// determine to use html or plain-text based on user preference and what's supplied in REST API call
$type = $GLOBALS['egw_info']['user']['preferences']['mail']['composeOptions'] === 'html' ||
!empty($data['bodyHtml']) ? 'html' : 'plain';
$body = $data['bodyHtml'] ?? null ?: $data['body'] ?? '';
// if user wants html, but REST API caller supplied plain --> convert to html
if (!empty($body) && empty($data['bodyHtml']))
{
$body = Api\Mail\Html::convertTextToHtml($body);
}
$preset = array_filter(array_intersect_key($data, array_flip(['to', 'cc', 'bcc', 'replyto', 'subject', 'priority', 'reply_id']))+[
'body' => $data['bodyHtml'] ?? null ?: $data['body'] ?? '',
'mimeType' => !empty($data['bodyHtml']) ? 'html' : 'plain',
'body' => $body,
'mimeType' => $type,
'identity' => $ident_id,
]+self::prepareAttachments($data['attachments'] ?? [], $data['attachmentType'] ?? 'attach',
$data['shareExpiration'], $data['sharePassword'], $do_compose));