Sugarcrm EmailFormatter $mailer->setHtmlBody($body) function not working when description field of task module contains formatted text without html tags.

Hi All,

SugarCRM $mailer->setHtmlBody($body) function not working when description field of task module contains formatted text without html tags.

Steps to reproduce:
1. Create Task record with description field value as below:
"Dear Sugar Administrator,

On XXX, XXX Xth 1555 , beginning Time, SugarCRM will be performing maintenance. Maintenance is expected to be complete.

Please note that this only affect you if you are using tXXX service."

2. Create email temaplte and add description field in template.

3. Try to send email using custom code as below:
"
try{
$oTemplate = new EmailTemplate();
$oMail = MailerFactory::getSystemDefaultMailer();
$mailTransmissionProtocol = $oMail->getMailTransmissionProtocol();
$oTemplate->retrieve_by_string_fields(array('name' => "Temaplate Name", 'type' => 'email'));
$oMail->clearRecipients();
$oMail->addRecipientsTo(new EmailIdentity("Test@email.com","Test User"));
$body = trim($oTemplate->body_html);
$oMail->setHtmlBody($body);
$oMail->Send()
}catch(MailerException $me) {
$message = $me->getMessage();
$GLOBALS["log"]->fatal(
"Send Email: error sending e-mail (method: {$mailTransmissionProtocol}), (error: {$message})"
);
}

"

4. check received email now:
"
Description :
Dear Sugar Administrator, On XXX, XXX Xth 1555 , beginning Time, SugarCRM will be performing maintenance. Maintenance is expected to be complete. Please note that this only affect you if you are using tXXX service.
"

But expected output was :
"Description :
Dear Sugar Administrator,

On XXX, XXX Xth 1555 , beginning Time, SugarCRM will be performing maintenance. Maintenance is expected to be complete.

Please note that this only affect you if you are using tXXX service.
"

Thanks,

Shreya

  •  ,

    Your "expected output" is not correct for the data you supplied. The input has been correctly marked up as HTML, exactly as you asked it to.

    HTML does not recognise whitespace as formatting and will convert any and all whitespace to a single space character. If you want to specify formatting for the output as HTML then you need to use HTML tags to do the formatting. That is what "Markup" means Slight smile

    Any text supplied without tags is simply treated as a single string of text characters, any multiple spaces, tabs, new lines etc. are all replaced by a single space.

    Try pasting your above input into a text editor, save it as a .html file and then view it in a web browser. You will likely get the same result as you see in the email received.

    Thanks,

    JH.

  • As replied here:

    https://sugarclub.sugarcrm.com/dev-club/f/questions-answers/5705/sugar-11-0-2-email-to-case-causes-formatting-to-be-incorrect/27995#27995

    I use Mailer Factory to send emails, I think it's better than using the Email bean directly.

    support.sugarcrm.com/.../

    I built a small utility function that I reuse in various parts of my code and it seems to work fine. The $EmailBody is strictly HTML in my case.

    Per the manual you can set the Text Body and/or the HTML body:

    $mailer->setTextBody("This is a text body message");
    
    // HTML Body
    $mailer->setHtmlBody("This is an <b>HTML</b> body message. <br> You can use html tags.");

    This is my Utility function, which can probably be improved (I wrote it several years ago), but it still works on the v11, I have not tested it in v12 yet but see no reason it should stop working, at least for now.

    <?php
      //Function to send Email message
      //one email per recipient
    
    function sendEmail($ToEmailAdd, $FromEmailAdd, $FromEmailName, $EmailSubject, $EmailBody) {
      global $sugar_config;
      require_once ('modules/Emails/Email.php');
      if(is_array($ToEmailAdd)){
        $To = $ToEmailAdd;
      }else{
        $To = explode(';',$ToEmailAdd);
      }
      foreach ($To as $to_addr){
        if (filter_var($to_addr, FILTER_VALIDATE_EMAIL)){
          try{
            $phpMailer = MailerFactory::getSystemDefaultMailer();
            $mailTransmissionProtocol = $phpMailer->getMailTransmissionProtocol();
            $FromEmailIdentity = new EmailIdentity($FromEmailAdd, $FromEmailName);
            $header_array = array(
              'From'=>$FromEmailIdentity,
              'ReplyTo'=>'',
              'Sender'=>$FromEmailIdentity, //mandatory
              'Subject'=>$EmailSubject,
            );
            $phpMailer->constructHeaders($header_array);
            $phpMailer->addRecipientsTo(new EmailIdentity($to_addr, $to_addr));
            $phpMailer->setHtmlBody($EmailBody);
            $phpMailer->send();
          }catch(MailerException $me) {
            $message = $me->getMessage();
            $GLOBALS["log"]->fatal(
              "SendEmail: error sending e-mail (method: {$mailTransmissionProtocol}), (error: {$message})"
            );
            return($message);
          }
        }else{
          $message = 'BAD EMAIL ADDRESS To: ' . $to_addr;
          return($message);
        }
      }
      return;
    }
    ?>

  • But if description is as below then what we have to use setTextBody or setHtmlBody?:

    Description :
    Dear Sugar Administrator,

    On XXX, XXX Xth 1555 , beginning Time, SugarCRM will be performing maintenance. Maintenance is expected to be complete.

    Please note that this only affect you if you are using tXXX service.
    "

    Thanks,

    Shreya

  • Shreya,

    As I tried to explain (but probably not very well!!) in my first reply:

    You are supplying plain text and expecting it to be formatted as HTML. If you put plain text into the bodyHTML field it will simply remove all spaces and line-break formatting and show it as one long line of text. This is what you are seeing and this is documented HTML behaviour.

    If you want to send out plain text then it needs to be sent as a text-only email, not HTML. That would then respect the newline and other spacing you have in your "description" text field.

    If you want to send it as HTML, you have to re-format the plain text to convert all the formatting into HTML tags. e.g. you need to replace all "\n" characters with "<br />" and use "<p>" for new paragraphs etc. Or use a specific HTML field to format it before sending out as HTML.

    Thanks,

    JH.

  • In my example, if the body is as shown, then use setTextBody and it will send a plain text email.
    The HTML won't work because you don't have the HTML formatting, as explained.

  • Dear Francesca,

    I have tried your solution but still no luck.

    Can you please review my code below:

    $oMail = MailerFactory::getSystemDefaultMailer();
    $oTemplate = new EmailTemplate();
    $oTemplate->retrieve_by_string_fields(array('name' => "Temaplate Name", 'type' => 'email'));
    $oTemplate->body_html = $oTemplate->parse_template_bean($oTemplate->body_html,$oTask->module_dir, $oTask);
    $body = trim($oTemplate->body_html);
    //$oMail->setHtmlBody($body);
    $textBody = strip_tags(br2nl($oTemplate->body_html)); // need to create the plain-text part
    $oMail->setTextBody($textBody);
    $oMail->setHtmlBody($body);

    Expected Description field value:

    "Meetings notes:
    The Meeting was attended by Mr. xxx& YYY
    - Discussion were mostly xxxx

    - Also interested in xxxx

    "

    Template :

    "

    <p>Hello, <br /><br />Please find below the meeting notes for:</p>
    <p><a href="task_instance_url" target="_blank" rel="noreferrer noopener">$task_name</a></p>
    <table style="width: 70%; height: 144px;">
    <tbody>
    <tr style="height: 18px;">
    <td style="height: 18px;"><strong>Status</strong></td>
    <td style="height: 18px;">:</td>
    <td style="height: 18px;">$task_status</td>
    </tr>
    <tr style="height: 18px;">
    <td style="height: 18px;"><strong>Assigned to</strong></td>
    <td style="height: 18px;">:</td>
    <td style="height: 18px;">$task_assigned_user_name</td>
    </tr>
    </tbody>
    </table>
    <p><strong> <strong>Description</strong>  : </strong></p>
    <p>$task_description <br /><br /><strong></p>

    "

    I will appreciate your help in advance.

    Thanks,

    Shreya

  • Are trying to send an Email message (Emails module)
    or trying to programmatically create a new Email Template (EmailTemplates module)?

    They are two very different things.