Modifying email signatures with the Gmail API (The G Suite Dev Show)


[MUSIC PLAYING] WESLEY CHUN: Hi, everyone. This is Wesley Chun from Google. Have you ever wanted to
programmatically update your email signature,
say to send the latest news or random witty
quotes to your email messages? Me, too. In 2016, the Gmail API was
enhanced with the ability to update user settings,
including signatures. In a moment, I’ll walk
you through a short Python script that does exactly that. For more info on this
addition to the API, check out the launch blog post. But first, a quick word
about SendAs email addresses. Gmail lets you send
messages from addresses other than your actual Gmail
address, which is considered your primary address. This lets you manage multiple
accounts from the same Gmail user interface. As expected, you need
to own or have access to the alternate email
accounts in order to do this. However, most people only
use their primary address, so you may not know about it. You can learn more about
the SendAs addresses at the pair of links onscreen. Now, you may be tempted
to use the term “alias,” especially since it was in the
help pages you just looked at, right? But try to avoid
that word for now, as it will open up Pandora’s
box and distract us from the focus of this video. Hello? Email signatures? See? You almost forgot
already, right? If you stick with sender
addresses or SendAs email addresses, there won’t
be any confusion. So what’s the app
we’re building today? It’s a short Python
script that sets a quote of the day
as the signature in your primary address. Well, where does this
quote of the day come from? Well, it can be as
simple and as boring as this function that
returns a hardcoded string. Cute, but not very
random, right? A better idea is to choose
from a number of quotes you have in a
relational database with columns for
quotes and authors. Here’s some sample code for
data in a SQLite database– more random, which is cool,
but not as efficient, because we’re
selecting all the rows and then choosing one randomly. I prefer to use a web service
instead, using a REST API. The code snippet here
does exactly that. You only need to provide the URL
for a quote of the day service that returns a JSON payload. To play it safe, it does fall
back to a hardcoded string– the one we saw earlier–
if the service fails to return a quote. Now, be sure to have the latest
Python Client Library installed with pip or pip3. Also make sure to have a
Google API project in the dev console with the
Gmail API enabled. Finally, if you’re
new to the Gmail API, check out our intro video first. If you’re ready, let’s
go to the computer and take a look at the code. OK. Here in the desktop, I have
a window open to Gmail. And I clicked on Compose, and
I’m composing a new message, and you can see my
current email signature. As much as I love it,
it’s time for a change, so let’s go to the code. On lines 1 through
16, you can see that these are our standard
imports in boilerplate, except for line
8, which is where we get the quote of the day. And we saw three
examples of that earlier. On line 10, we need to
have the authorization scope to access Gmail settings. And then on line 16 is where
we build a service endpoint to Gmail. On lines 18 to 19,
this is where we get all of our sender addresses. And the fields requests
only the sendAs.isPrimary and sendAs.sendAsEmail fields. The first one, isPrimary,
is a Boolean flag on whether or not an address is
the current primary address. And the sendAsEmail is
just the email address. So we’re getting back a
flag and an email address. Got it? We execute this call to the
API, get the results keyed off of sendAs. And then lines 20
through 22 is where we cycle through all of
the sendAsEmail addresses and stopping when the primary
one is found, which you know will be contained in
the variable address. Lines 24 and 25 are where we
actually set the new signature. So given what we have
defined on line 8, we’re going to pass
that in as the body, and we’re going to use
the primary address’s sendAsEmail attribute,
which is just, like I said, the email address. And then we execute
that API call. And when it comes
back, we confirm that the primary address
signature was changed. Now, note that lines 18 through
22 are really only necessary if you have many
sender addresses and want to search for the
primary one to modify it. But if you already know
the primary sender address because you only
have one address, there’s no need
to do any of this. You just pass your
one Gmail address in here as the sendAsEmail
argument, and you’re done. So all this can go. But I think it’s a good idea
for you to get practice. Because one day, you
might have a whole bunch of sender addresses, right? And by the way, our
quote of the day– we’re just going to
use the simple one that I demoed earlier
with a slightly more interesting quote. All right. Let’s run it and
see what happens, assuming that you’ve
already run this before and you’ve gone through
the OAuth 2 prompt. OK. It says the primary
address has changed. So let’s kill this compose,
refresh our Gmail window. Click on Compose again,
and you see, boom, we got a new email signature. Isn’t that awesome? So, yes, you can change
an email signature in less than 30 lines of code. But now hold on, sailor. While you can go crazy with
your consumer Gmail settings, be aware that more sensitive
operations in a Google Apps domain, such as modifying
sender addresses or controlling forwarding, are
restricted to accounts with domain-wide authority. If you want to dig deeper
into the code sample, check out the
accompanying blog posts. Well, where can you
find out more info? Well, the first link takes
you to the first of the guides we have on Gmail settings. If you’re new to the API
and want to get an overview, check out the second link. The last link takes you directly
to the API reference docs. Want a challenge? Well, write some code that uses
a Gmail API to manage filters or configures your
vacation responder. Well, we hope these features
will help enhance your next app using the Gmail API. Be sure to subscribe
to our channel and tune in again next time. This is Wesley Chun from Google,
and we’ll see you online. [MUSIC PLAYING] Hey, everyone. Thanks for watching. Check out these related videos,
and we’ll see you next time.

Danny Hutson

Leave a Reply

Your email address will not be published. Required fields are marked *