【问题标题】:How to change email signature using GMAIL API?如何使用 GMAIL API 更改电子邮件签名?
【发布时间】:2021-09-21 18:14:52
【问题描述】:

我想为域中的每个用户设置电子邮件签名。问题是我已经准备好了 html 签名,但我不知道如何将 html 签名与 GMAil API 一起使用。我正在使用 python,当我尝试这样做时,它只会将其转换为文本。有没有办法将 html 签名嵌入到变量中,然后使用 GMAil API 插入到 gmail 帐户中?

【问题讨论】:

    标签: python google-api gmail-api


    【解决方案1】:

    我建议查看文档Managing signatures

    primary_alias = None
    aliases = gmail_service.users().settings().sendAs().\
        list(userId='me').execute()
    for alias in aliases.get('sendAs'):
        if alias.get('isPrimary'):
            primary_alias = alias
            break
    
    sendAsConfiguration = {
        'signature': 'I heart cats'
    }
    result = gmail_service.users().settings().sendAs().\
        patch(userId='me',
              sendAsEmail=primary_alias.get('sendAsEmail'),
              body=sendAsConfiguration).execute()
    print 'Updated signature for: %s' % result.get('displayName')
    

    documentation 声明它应该是 HTML

    【讨论】:

      【解决方案2】:

      感谢您的回复。我找到了使用 html 签名的解决方案。我所做的是使用 python 脚本创建 html 文件(您也可以以任何其他方式创建它们。我只是使用 python,因为我想要一个脚本来创建 html 签名以及更改用户的电子邮件签名)。

      然后将整个 html 作为文本导入到 python 变量中,如下所示。

      file = open(test.html, 'r')
          htmlfile = file.read()
          ESignature = {'signature': htmlfile}
      

      在那之后,在电子邮件签名正文参数中,我只使用了存储 html 签名的变量,并且它起作用了。

      sig = service.users().settings().sendAs().update(userId='me', sendAsEmail="test@example.org", body=ESignature).execute()
      

      【讨论】:

        猜你喜欢
        • 2021-09-24
        • 2017-11-01
        • 2015-08-08
        • 2018-11-03
        • 1970-01-01
        • 2018-05-11
        • 2016-11-25
        • 2014-08-27
        • 2018-02-23
        相关资源
        最近更新 更多