diff --git a/api/src/Mail/Html.php b/api/src/Mail/Html.php index d89d2a0f0b..b1958a756e 100644 --- a/api/src/Mail/Html.php +++ b/api/src/Mail/Html.php @@ -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
+ * + * @param string $text + * @return string + */ + public static function convertTextToHtml($text) + { + return '

'.implode("

\n

", array_map('nl2br', preg_split("/\r?\n\r?\n/", $text)))."

\n"; + } } \ No newline at end of file diff --git a/mail/inc/class.mail_compose.inc.php b/mail/inc/class.mail_compose.inc.php index 9ac2f3e0cc..8c4d9237fd 100644 --- a/mail/inc/class.mail_compose.inc.php +++ b/mail/inc/class.mail_compose.inc.php @@ -1025,7 +1025,7 @@ class mail_compose { if ($_REQUEST['preset']['mimeType'] === 'plain') { - $_REQUEST['preset']['body'] = '

'.nl2br($_REQUEST['preset']['body'])."

\n"; + $_REQUEST['preset']['body'] = Mail\Html::convertTextToHtml($_REQUEST['preset']['body']); } else { diff --git a/mail/src/ApiHandler.php b/mail/src/ApiHandler.php index ff327f6d11..a8d02885c4 100644 --- a/mail/src/ApiHandler.php +++ b/mail/src/ApiHandler.php @@ -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));