使用 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"))


冉冉说
浏览 86回答 1
1回答

三国纷争

基于正则表达式的解决方案在Tanaike和Rafa Guillermo的帮助下,最终为我工作的解决方案是用这样的 小东西=代替:=.replace()listingUrl = listingUrl.replace(/=/, '=')
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript