How to change email signature using GMAIL API?

Viewed 656

I want to setup email signature for each user in the domain. The thing is I already have a html signature ready but I dont know how to use html signature with GMAIl API. I am using python and when I try to do that, it only converts it to text. Is there any way to embed html signature in a variable which then inserts into the gmail account using GMAIl API?

2 Answers

I recommend checking the documentation 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')

The documentation states it should be HTML

TBH Im not sure if there is any way to force it to convert to HTML it seams it only supports text.

Thanks for the reply. I found the solution to use html signatures. What I did is created html files using python script (You can also create them in any other way. I just did using python because I wanted a single script to create a html signature as well as change the email signature for the user).

And then imported whole html as text inside a python variable as shown below.

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

After that in the email signature body parameter I just used the variable where I stored the html signature and it worked.

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