javascript - 使用 Google Apps 脚本时使用查询参数保护 URL
问题描述
我很难发送包含包含查询参数的 URL 的自动电子邮件(使用 Google Apps 脚本)。
预期行为
Google Apps 脚本(特别是Gmail
服务)发送一封电子邮件,电子邮件正文的一部分包含一个带有查询参数的 URL。URL 如下所示:
http://my.app/products?id= Bz9n7PJLg8hufTj11gMF
观察到的行为
该Gmail
服务似乎正在=
从我的 URL 中删除。因此,电子邮件的正文最终看起来像这样: ...
http://my.app/products?idBz9n7PJLg8hufTj11gMF
...
显然,该链接不起作用。
我在这里检查了关于 SO 的其他问题,我尝试使用 GAS Utilities 服务中的基本编码工具,以及使用encodeURI()
JavaScript 方法。到目前为止没有运气。
电子邮件发送代码
//////// GENERATING MESSAGE FROM ID ////////////
// Gets message from ID
var id = Gmail.Users.Drafts.get('me', 'r-1006091711303067868').message.id
var message = GmailApp.getMessageById(id)
var template = message.getRawContent()
// Replaces template variables with custom ones for the user using RegExes
let listingUrl = 'http://my.app/products?id=xyz'
let creatorEmail = 'hello@gmail.com'
let creatorUsername = 'Sam'
template = template.replace(/templates@my.app/g, creatorEmail)
template = template.replace(/firstName/g, creatorUsername)
//** Below is the string that gets modified and broken **//
template = template.replace(/listingUrl/g, listingUrl)
// Creates the new message
var message = Gmail.newMessage()
var encodedMsg = Utilities.base64EncodeWebSafe(template)
message.raw = encodedMsg
// Sends it
Gmail.Users.Messages.send(message, "me", Utilities.newBlob(template, "message/rfc822"))
解决方案
基于正则表达式的解决方案
在Tanaike和Rafa Guillermo的帮助下,最终为我工作的解决方案是使用以下代码=
替换:
=
.replace()
listingUrl = listingUrl.replace(/=/, '=')
推荐阅读
- postgresql - 在我的 postgresql 函数的表中插入值后,如何返回集合、数组或任何插入的值?
- laravel - Laravel 存储多态关系模型名称而不是类型
- python - 无法在 python 3.9 上安装 evdev
- java - 改变地图中的价值,正确的方式
- java - Spring Batch Job Param 总是显示 zsh:找不到匹配项
- android - Flutter - html页面内带有href链接的WebView错误
- hibernate - SchemaGen 错误地生成了不必要的关联表
- javascript - 当我将鼠标悬停在背景上时,我想更改背景颜色和文本颜色
- ionic-framework - 由于 /home 路由,离子应用程序无法刷新
- android - CI/CD 颤振、firebase、android play store 不工作