0

In my app, there is a lightning:textarea in which the user can type the content that will be send as an email to the users. This is the textarea: enter image description here As you can see from the image, the 'Thanks' is after 2 lines of the first line. But when I send the mail, I get like this:

enter image description here

In the mail, both sentences are on the same line. This is the code I used:

Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();
message.toAddresses = new String[] { emailId };
message.optOutPolicy = 'FILTER';
message.subject = emailSubject;
message.setSenderDisplayName(senderDisplay);
message.setHtmlBody(emailBody);//'emailBody' is the content from textarea
Messaging.SingleEmailMessage[] messages = new List<Messaging.SingleEmailMessage> {message};
Messaging.SendEmailResult[] results = Messaging.sendEmail(messages);

As the content in the textarea is typed by the user himself, he cannot put <br/> in it. He may not have the technical knowledge. Basically, each 'Enter' pressed by the user has to be a new line in the mail body.

Also, I know this will work if I use setPlainTextBody(plainTextBody). But the problem with setPlainTextBody(plainTextBody) is that, I also want to include <a href> links in the mail body. Those links will not work if I use setPlainTextBody(plainTextBody).

So, how can I solve that? How can I include both new lines and links in my email body?

  • Blank space between tags in html are ignored. You need to replace the enter/return character with BR tag. – Hengky Ilawan Jun 12 at 4:47
  • thanks for the comment. how can i find the enter/return character? – Jayesh Babu Jun 12 at 4:51
  • 2
    \n is the return char. So try this emailBody.replaceAll('\n', '<br/>'); – Hengky Ilawan Jun 12 at 4:57
  • 1
    @HengkyIlawan I think you should post that in answer. – rahul gawale Jun 12 at 7:37
  • 1
    I have put my comments as answer @rahulgawale! – Hengky Ilawan Jun 12 at 7:59
2

Blank space between tags in html are ignored. You need to replace the enter/return character with <br> tag. For example:

emailBody.replaceAll('\n', '<br/>');
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.