To send email from a user’s Gmail account in JavaScript, authorize the user with OAuth, build a standards-compliant MIME message, encode it as base64URL, then pass that string as the Gmail API’s raw field to users.messages.send. A plain object with to, subject, and body is not a sendable Gmail message.
1. Set up the Gmail API and OAuth
-
Create or select a project in Google Cloud and enable the Gmail API.
-
Configure the OAuth consent screen for your app, then create an OAuth client appropriate to its runtime. For a browser app, register the site’s origin under the client’s authorized JavaScript origins.
-
Use OAuth to obtain the signed-in user’s authorization. An API key alone does not grant access to a user’s Gmail account.
What’s actually slowing this PC down?
Pick the symptom - the matching free tool is one click away.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Google’s JavaScript quickstart demonstrates browser setup and authorization, but Google describes its simplified authentication as a testing approach. For a production app, follow Google’s scope and authorization guidance and choose credentials and an authorization design suited to your application. Browser and server-side applications have different credential-handling needs; the quickstart should not be treated as a production architecture.
2. Request only the Gmail scope the feature needs
For a feature that only sends messages, request https://www.googleapis.com/auth/gmail.send. The users.messages.send method also accepts gmail.compose, gmail.modify, and mail.google.com, but those broader permissions are unnecessary unless the app needs their additional capabilities.
Rank #2
Google classifies gmail.send as a sensitive scope. Broader scopes can be restricted and may bring additional verification or security-assessment requirements. The exact requirements depend on the app’s audience and requested scopes; do not assume a public app can avoid review simply by using OAuth. Check Google’s current scope documentation when deciding what to request.
3. Construct a MIME message and send it
The Gmail API expects the message content in RFC 2822-compliant MIME format. Put headers such as To, Cc, Bcc, and Subject, along with the message body, into that MIME content. Encode the MIME bytes as base64URL and set the resulting string as raw on a Gmail Message resource.
Recommended Free Tools
Once you have that encoded string, the request shape is:
const requestBody = { raw: base64UrlEncodedMimeMessage };
const result = await gmail.users.messages.send({
userId: "me",
requestBody,
});
me is the authenticated-user alias. The recipient is specified in the MIME message’s headers, not as a separate recipient parameter in this call. Google’s send method reference defines the request as a Gmail Message.
Rank #4
The snippet illustrates only the API request shape; it does not construct or encode MIME. Google’s sending guide describes the required sequence but does not provide a JavaScript-specific end-to-end MIME serialization example. Use and verify a MIME library or implementation appropriate to your target runtime, including its base64URL behavior, before relying on it. The required steps are to create the content, encode it, assign it to raw, and call the send method.
4. Choose immediate sending or a reviewable draft
| Workflow | Use it when | API operation |
|---|---|---|
| Send immediately | The message is ready to send without an in-product review or edit step. | users.messages.send |
| Create a draft, then send | The user should review or edit the message before it is sent. | Create a draft, then call drafts.send |
Both workflows require the MIME message and its encoded content. Google documents the sequence in Create and send email messages.
Best Value
5. Account for API quotas and recipient limits
Google’s quota page, updated September 10, 2026, lists the following limits for projects subject to the quota changes effective May 1, 2026. Projects that used the Gmail API from November 2025 through April 2026 retain their previous quotas, so these figures do not apply universally to every project.
| Limit | Documented value |
|---|---|
| Quota per project | 1,200,000 quota units per minute |
| Quota per user per project | 6,000 quota units per minute |
Cost of one messages.send call |
100 quota units |
| Recipients in one email | 500 |
These API quota-unit limits are distinct from a Gmail user’s daily sending allowance. Daily limits are account-dependent and shared across the user’s Gmail API, Gmail web and mobile clients, and SMTP usage. See Google’s current Usage limits page for project-specific applicability and details.
6. Handle errors without treating a success response as delivery confirmation
For time-based quota errors, Google recommends truncated exponential backoff. A 429 caused by exceeding a daily sending limit may continue for hours. Retrying is an error-handling strategy, not proof that a message was or will be delivered.
Google also warns that quota enforcement can be delayed by several minutes. As its error guidance puts it: “You can’t assume that a 200 response means the email was successfully sent.” Treat the API response accordingly and avoid telling users that delivery is confirmed solely because the request returned HTTP 200.
Quick wins for a faster PC:
Repair Windows errors before they cause bigger problemsFix Now →Fix the driver behind crashes, sound loss and screen glitchesFind Drivers →Clear out junk files and repair common Windows errorsFree Scan →Quick Recap
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

