【问题标题】:Protecting a URL with a Query Parameter when Using Google Apps Script使用 Google Apps 脚本时使用查询参数保护 URL
【发布时间】:2020-12-26 05:39:00
【问题描述】:

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

预期行为

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

【问题讨论】:

  • 不幸的是,在我的环境中,我无法使用您的脚本复制您的问题。
  • 我可以重现这个。 @TheMaster = 一直保留在代码中,直到发送消息。 Gmail UI 中的邮件不显示 =(顺便说一句,它链接到的位置也不显示),但是,如果您从 UI 中签出 ⋮ > Show original,您可以看到= 实际上存在于文本部分,但已在 html 部分中删除。但是,记录 Utilities.newBlob(template, "message/rfc822").getDataAsString() 确实会使用 = 显示它。在我看来是马车。 My test message
  • @TheMaster 也尝试了rfc2822,但同样缺少=。尝试使用 python 库复制它,看看这是否是 API 或 GAS 包装器的问题。
  • @Rafa Guillermo 我认为这是一个很好的评论。我没有注意到这种情况发生在 HTML 正文中。在正文的情况下,可以正确看到http://my.app/products?id=xyz。所以我认为OP的情况无法复制。当使用 HTML 正文时,我认为将 http://my.app/products?id=xyz 修改为 http://my.app/products?id=xyz 可能是一种解决方法。在这种情况下,Gmail.Users.Messages.send(message, "me"); 也可以使用。
  • @Tanaike 用= 替换= 绝对有效。但是,我不确定是否更简单的不呈现 html 的邮件应用程序会直接将其显示为=,而不是呈现=。此外,我也无法使用 python 库复制它,所以我认为这是 GAS 包装器中某处的编码问题。

标签: javascript google-apps-script gmail gmail-api urlencode


【解决方案1】:

基于正则表达式的解决方案

TanaikeRafa Guillermo 的帮助下,最终为我工作的解决方案是将= 替换为=,使用一点.replace(),如下所示: listingUrl = listingUrl.replace(/=/, '=')

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-15
    • 2014-02-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多