

Navigate to (or wherever you have placed your index.html file). Strangely there is a similarly named scope ( compose) which allows much greater access than we require. These two scopes are all we need for this application. Modify the scopes variable definition in the index.html as follows (note that the scopes variable is a space-separated string):Īs mentioned in the previous article, it’s always best practice to request the bare minimum permissions when working with someone else’s data - especially something as sensitive as their email account. However, sending an email requires further permissions. Previously we required read-only access to the users’ Gmail account. Enter Our CredentialsĪnd we should enter our credentials in index.html: var clientId = '' var apiKey = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' Tweak the Authentication Scopesįinally, we should tweak the authentication scopes.
Send code#
git clone folder 01 - Basic client contains the code from the previous article. Now that the API credentials are set up we should check out the source code of the existing demo application. Keep this info open in a browser tab for the next step. Once we’ve filled in the necessary fields we should be able to see our credentials back in the Credentials section. Clicking Create will generate a client ID and client secret. We do not need to enter an authorized redirect URI. For a local development environment this will likely be or similar.

Select Web application as the application type and enter at least one authorized JavaScript origin. Click Create and Google will generate an API key.įor the OAuth client ID, click once more on New credentials and select OAuth client ID. However, for production I’d recommend adding a HTTP referrer (this will prevent abuse of our API key from non-authorized domains). On the following page we need only fill in the name field. Now we need to create two sets of credentials: a browser API key and an OAuth client ID.įor the browser API key select API key then Browser key. From here we need to pop out the hamburger navigation menu and choose API Manager, then in the sidebar on the left we need to choose Credentials, before clicking on the New credentials button on the following page. Clicking Create will take us to the new project dashboard interface. Navigate to the Google Developer Console and create a project.
Send how to#
Since we last looked at the Gmail API, Google have changed the user interface for the API management console - so here’s a quick update on how to create the necessary Gmail API credentials. This will allow us to create applications which utilise Gmail functionality. The first step is to set up the Gmail API on your Google account. Enabling the Gmail API on Your Google Account As ever, the complete source code for this article can be found on our GitHub repo (in the folder 02 - Sending mail). It’s not necessary to have read the previous article to follow along with this one (although it will give you a deeper understanding of the subject matter). We’ll end up with a customizable app which can display recent messages, allow the composition of new messages and allow us to reply to specific messages. Today I’m going to enhance that app with email sending capability using Gmail’s powerful back-end. In a previous article, I demonstrated how to build a basic Gmail inbox and message viewing app using the Gmail JavaScript API. Thanks to all of SitePoint’s peer reviewers for making SitePoint content the best it can be! Var dataRange = sheet.getRange(startRow, 1, numRows, 3) This article was peer reviewed by Simon Codrington. * Sends non-duplicate emails with data from the current spreadsheet. This constant is written in column C for rows for which an email To 'EMAIL_SENT' for each row after sendEmail is called. Here's a simple extension of the code that sets the cells in column C
/cdn.vox-cdn.com/uploads/chorus_image/image/69461859/Scheduled_Send.0.jpg)
Minute or day) you will be able to re-run the script later on and avoid Your code or you reach the maximum number of emails you can send every This way, if your script stops running (for instance, if there is a bug in You might want to mark a cell in each row every time an email is sent. ndEmail() (note that there are four versions of this method).Sheet.getRange() (note that there are four versions of this method).You might want to have a look at the documentation for the following methods Messages are usually immediately delivered, but Select the function sendEmails in the function dropdown list and clickĬheck out your email inbox. Var subject = 'Sending emails from a Spreadsheet' Fetch values for each row in the Range. Var dataRange = sheet.getRange(startRow, 1, numRows, 2) Var numRows = 2 // Number of rows to process Var startRow = 2 // First row of data to process Var sheet = SpreadsheetApp.getActiveSheet() * Sends emails with data from the current spreadsheet.
