我很难发送一封自动电子邮件(使用 Google Apps 脚本),其中包含包含查询参数的网址。
Google Apps 脚本(特别是
Gmail
服务)发送电子邮件,电子邮件正文的一部分包含带有查询参数的 URL。 URL 将如下所示:
http://my.app/products?id=Bz9n7PJLg8hufTj11gMF
Gmail
服务似乎正在从我的 URL 中删除 =
。因此,电子邮件的正文最终如下所示:
...
http://my.app/products?idBz9n7PJLg8hufTj11gMF
...
显然,该链接不起作用。
我在这里检查了有关 SO 的其他问题,并且尝试使用 GAS Utilities 服务中的 base编码 工具,以及使用
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 = '[email protected]'
let creatorUsername = 'Sam'
template = template.replace(/[email protected]/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(/=/, '=')
我找到的解决方案是 URL 的路径部分必须在查询参数之前以正斜杠
/
结尾。
https://example.com/this/is/a/path/?param=123