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

Parents
  • 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

Reply Children
  • 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.