If you’ve used the Gmail API, you may have encountered some quirks with From Addressing when creating and sending a DRAFT. As an example, if you’re working with a DRAFT in the Gmail UI, and then you change the From Address to a different choice in the From dropdown in the Compose window, that change will not immediately be reflected in the DRAFT if you then call users.drafts.get on that particular DRAFT. Why? Because Gmail hasn’t saved the DRAFT yet. This is just one of the quirks we’ll examine and work around in this article.
1. Create a DRAFT with the Gmail API; then open it up in the UI
When you create a DRAFT with the API using users.drafts.create, it doesn’t necessarily need a From Name/From Address. But if you then open the DRAFT in the UI and make changes, a From Display Name will be set. Here is some sample C# where I’m calling the Gmail API to create a DRAFT with just a Subject and an HTML body.
I can then retrieve the DRAFT that was created by using the Gmail API Explorer. Notice how the From Line contains my account address, firstname.lastname@example.org but no From Name:
However, if I then open up this DRAFT in the Gmail web interface, and then make a simple change like adding a few spaces to the Subject, that action re-saves the DRAFT and also sets the From Display Name.
Now if I retrieve the same DRAFT using the API explorer, the From Name is also present in the From Line:
2. Retrieve the From Address/Name of a DRAFT using the API
A DRAFT created in the UI will almost always have the correct From Name and From Address, however this wasn’t always the case. For a while after the Gmail API was released in 2014, a DRAFT retrieved from the Gmail API would always show the Google Account Name as the From Name, and not the actual From Name set in Gmail Settings, or the From Name chosen from the From dropdown in a Compose window, in the case where multiple From Addresses/Names are set up. For most users, this was fine, because by default the From Name is NOT set separately in Gmail — the From Name by default is pulled from your Google Account. It’s only different if you proactively set it differently in Gmail’s Settings:
Additionally, if you had multiple From Addresses/Names configured for your Gmail account, even if you a From Address other than your default Gmail account, the behavior of the Gmail API method users.drafts.get was to show the Google Account’s name in the From Line, not the specific From Name that corresponded to the From Address.
That issue, however, was corrected sometime in 2019. Now, the DRAFT does correctly display the From Address and the right From Name, provided it has been saved since the last change was made to the From Address.
3. Changing the From Address using the dropdown in the Compose doesn’t always take effect
When you select a From Address/Name from the dropdown in the Compose window, the DRAFT does not save, and the change is therefore not reflected in the DRAFT. Let’s test this:
The DRAFT needs to be saved via a change to the Subject or Message for the change in the From dropdown to be reflected in the DRAFT.
Now, does this mean that if you were to click the Gmail Send button after making a change to the From dropdown, that your change wouldn’t be reflected in the email that is sent? No. Your change would be reflected, because clicking the blue Gmail Send button triggers a “save” operation on the DRAFT before it’s sent.
Here’s a code sample to accomplish this:
4. Retrieve account-wide From Address info
Another way you can retrieve From Line information is by using the users.settings.sendAs methods of the API. The “list” method will pull a list of all possible From Addresses/Names from the account, not specific to a DRAFT, though. The information here corresponds to the information under your Settings–>Accounts tab in the Gmail web interface:
There’s one key difference, though, that’s important to be aware of if you plan on retrieving From Line information using the Users.settings.sendAs.list method. If you have set your default Gmail account to use the From Name of your Google account, meaning you haven’t set it separately in Gmail settings, then the API will return an empty string for the From Display Name.
Let’s demonstrate this by video:
5. Working client-side? Use the Inbox SDK API to pull the From Line from the Gmail web UI.
If you’re working with Gmail inside the interface and are developing a Chrome extension and want to get the current DRAFT’s From Address and Display Name, you can also use Inbox SDK’s API, and specifically, the getFromContact method of the ComposeView object. A ComposeView is just a fancy term for an actual Compose window in Gmail.
However, there’s an important factor to consider when using the getFromContact method. If your Gmail account doesn’t have any additional From Addresses set up, meaning all your emails are sent by your default Gmail account, then a From dropdown doesn’t show up in the Compose window.
If there’s no From dropdown, then getFromContact has difficulty pulling accurate From Line information for the ComposeView. It can’t find what it can’t see.
If there’s no From dropdown, then the properties of the Contact object it retrieves are set to your logged-in Gmail account. Contact.name is set to the Name set for your overall Google account, and Contact.emailAddress is set to your logged-in Gmail account. Sometimes, it’s unable to pull the name set on your Google Account and instead sets Contact.name to “Default”. In the past, this has caused issues for GMass users, but we’ve now worked around this by ceasing our use of getFromContact and relying on the From Address/Name in the Gmail DRAFT retrieved from the API instead. Essentially, we’ve moved the processing from the front-end to the back-end to ensure greater accuracy.
There are several techniques to pull From Email and From Display Name information from a Gmail DRAFT and from an overall Gmail account, but there are some oddities to take into consideration.