使用Google Apps脚本时使用查询参数保护URL

问题描述

我很难发送自动电子邮件(使用Google Apps脚本),该电子邮件包含包含查询参数的URL。

预期行为

Google Apps脚本(特别是Gmail服务)发送电子邮件,并且电子邮件正文的一部分包含带有查询参数的URL。该网址将如下所示: http://my.app/products?id=Bz9n7PJLg8hufTj11gMF

观察到的行为

Gmail服务似乎正在从我的URL中剥离=。因此,电子邮件的主体最终看起来像这样: ... http://my.app/products?idBz9n7PJLg8hufTj11gMF ...

显然,该链接无效。

我已经在SO上检查了其他问题,并尝试使用GAS Utilities服务中的base encoding工具以及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(/=/,'=')

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...