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

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

Reply Children
No Data