如何保持为前端JavaScript格式化的MS Graph电子邮件的正文格式?

huangapple go评论131阅读模式
英文:

How to maintain Body formatting for MS Graph email that is formatted for Javascript on the frontend?

问题

在我的前端中,我正在使用Vuetify,这是一个VueJS UI框架,用于电子邮件输入的正文部分采用了一个组合框。用户可以编辑它等等。我最初使用以下形式的预测文本来填充它:

  1. textModel = "To whom it may concern, \n\n" +
  2. "We are writing to you about blah blah blah. \n\n" +
  3. "Thank You"

然后,将这些数据通过[FormData]发送到后端API,并将其绑定到一个较大的电子邮件模型(主题、收件人等)中。然而,对于我的电子邮件界面,我使用以下方式将正文分配给消息对象:

  1. var message = new Message
  2. {
  3. Subject = email.Subject,
  4. Body = new ItemBody
  5. {
  6. ContentType = Microsoft.Graph.BodyType.Html,
  7. Content = email.Body,
  8. },
  9. ToRecipients = ToRec,
  10. CcRecipients = CcRec,
  11. BccRecipients = BccRec,
  12. Attachments = attachments,
  13. };

当我通过MS Graph API发送电子邮件并收到电子邮件时,正文格式化为单行,因此"\n"不起作用。如何处理这种情况以便在前端和后端使用换行符?我是否需要在正文字符串上执行查找和替换操作,并将其替换为HTML中的
标签?

谢谢

英文:

On my front end I am using Vuetify which is a VueJS UI framework which uses a combobox for the body of the email input. The user can edit it and what not. I initially prefil it with predicted text of this form:

  1. textModel = "To whom it may concern, \n\n" +
  2. "We are writing to you about blah blah blah. \n\n" +
  3. "Thank You"

This data is then sent to the backend API via [FormData] and binding it to a model as it is apart of a larging email Model (subject, recipients, etc). However, for my email interface I use the following to assign the body to the message object:

  1. var message = new Message
  2. {
  3. Subject = email.Subject,
  4. Body = new ItemBody
  5. {
  6. ContentType = Microsoft.Graph.BodyType.Html,
  7. Content = email.Body,
  8. },
  9. ToRecipients = ToRec,
  10. CcRecipients = CcRec,
  11. BccRecipients = BccRec,
  12. Attachments = attachments,
  13. };

When I send the email via MS Graph API and I receive the email the body is formatted as a single line so the "\n" is doing nothing. How would I account for new lines in this manner to use on the front end and backend? Do I have to do a find and replace on the body string and replace with a br tags in html?

Thanks

答案1

得分: 1

"你试图发送的内容在 textModel 中格式化为文本,但在 Body 中,你将正文类型设置为了 HTML。如果你想发送纯文本正文,你应该这样设置:

  1. Body = new ItemBody
  2. {
  3. ContentType = Microsoft.Graph.BodyType.Text,
  4. Content = email.Body,
  5. },

或者,你可以将你的文本格式化为 HTML,例如:

  1. textModel = "To whom it may concern, <br>" +
  2. "We are writing to you about blah blah blah. <br><br>" +
  3. "Thank You<br>"
英文:

What your trying to send in

  1. textModel = "To whom it may concern, \n\n" +
  2. "We are writing to you about blah blah blah. \n\n" +
  3. "Thank You"

is formatted text but in

  1. Body = new ItemBody
  2. {
  3. ContentType = Microsoft.Graph.BodyType.Html,
  4. Content = email.Body,
  5. },

your saying the body is Html if you want to send a Text body you should make it

  1. Body = new ItemBody
  2. {
  3. ContentType = Microsoft.Graph.BodyType.Text,
  4. Content = email.Body,
  5. },

or format your text as html instead eg

  1. textModel = "To whom it may concern, <br>" +
  2. "We are writing to you about blah blah blah. <br><br>" +
  3. "Thank You<br>"

huangapple
  • 本文由 发表于 2023年4月4日 04:03:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/75923366.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定