Home >Web Front-end >JS Tutorial >Sending Emails with the Gmail JavaScript API
This article demonstrates building a Gmail app using the Gmail JavaScript API, extending a previous example to include email sending, composing, and replying. No prior knowledge of the previous article is required. The complete source code is available on GitHub (folder 02 - Sending mail).
Key Features:
Users.messages: send
endpoint, constructing RFC 5322 compliant messages.Setting Up the Gmail API:
http://localhost
). A redirect URI is not needed.Application Setup:
git clone git@github.com:sitepoint-editors/gmail-api-javascript-example.git
index.html
, replace placeholders with your API key and client ID.scopes
variable in index.html
to include 'https://www.php.cn/link/ef1f3fc8be928fd5b5e721689bb92b7b'
.Sending Emails:
A "Compose" button is added to the UI, opening a modal for composing emails. The sendEmail()
function handles submission, disabling the send button during the asynchronous operation to prevent multiple submissions. The sendMessage()
function interacts with the Gmail API, constructing the RFC 5322 message (including headers) and base64 encoding it before sending. The composeTidy()
function cleans up the modal after successful sending.
Replying to Emails:
A "Reply" button is added to the message view modal. The fillInReply()
function populates a reply modal with recipient, subject (prefixed with "Re:"), and Message-ID
for proper threading. The sendReply()
function sends the reply, using the In-Reply-To
header. replyTidy()
cleans up the reply modal.
Further Improvements and Features:
Frequently Asked Questions (FAQs):
The article concludes with a FAQ section covering topics such as sending emails with attachments, multiple recipients, scheduling (using Google Apps Script), HTML emails, error handling, custom headers, authentication, emojis, inline images, and quota monitoring. These FAQs provide concise answers and point to relevant techniques.
The above is the detailed content of Sending Emails with the Gmail JavaScript API. For more information, please follow other related articles on the PHP Chinese website!