如何在MailApp.sendEmail的HTMLBody中为指定文本添加超链接
解决方案
直接在HTML邮件内容中使用原生HTML <a> 标签包裹目标文本即可,无需大幅改动现有代码结构:
固定URL的情况
如果指南URL是固定值,直接把原htmlBody中对应的文本替换为带链接的HTML代码:
修改前的片段:
"Here is a one pager on how to action notices in OneDMS.<br/><br/>"
修改后的片段:
"Here is a one pager on <a href=\"https://你的OneDMS指南实际URL\">how to action notices</a> in OneDMS.<br/><br/>"
使用变量插入URL的情况
如果需要动态传入URL,注意正确处理字符串转义和拼接:
- 先定义URL变量(确保变量值是合法的完整URL):
const guideUrl = "https://你的OneDMS指南实际URL";
- 替换
htmlBody中的对应片段为:
"Here is a one pager on <a href=\"" + guideUrl + "\">how to action notices</a> in OneDMS.<br/><br/>"
完整修改后的代码示例
MailApp.sendEmail({ to: fvs[key][0][1], cc: "", subject: "[FOR YOUR ACTION] Outstanding Tax Mail", htmlBody: "Dear Vialter,</p>This is a follow up email on the below list of notices that are still sitting in the Awaiting Second Approval status in OneDMS.<br/><br/>It is essential that we resolve these outstanding items to ensure compliance and maintain an accurate and up-to-date record in OneDMS to manage our clients and Vialto's risk.<br/><br/>If you have already actioned the notice (sent to the taxpayer or sent to file), please go and perform the Second Approval on the notice to move it from the 'Awaiting Second Approval' to 'Final and Published'.<br/><br/>Here is a one pager on <a href=\"https://你的OneDMS指南实际URL\">how to action notices</a> in OneDMS.<br/><br/>List of notices that you are listed on:<br/><br/>" + messageBody2 + "" + messageBody + "<br/><br/>Kind Regards,<br/>CTA Team </p>", // Options: Body (HTML) name: "", });
关键注意事项
- URL必须包含
http://或https://前缀,否则无法正常跳转 - JavaScript字符串中的双引号需加反斜杠
\转义,避免语法报错 - 之前变量插入失败大概率是未正确转义引号,或变量未被正确定义/传入
内容的提问来源于stack exchange,提问作者WN Legacy
相关产品推荐
相关产品推荐

