-
Notifications
You must be signed in to change notification settings - Fork 473
Send Email
- Note
- API documentation
- Definitions
- Use Gmail API to send messages
- Use SMTP to send messages
- Google Workspace SMTP relay service
- Send an email
- Send an email from a user sendas
- Send an email from a group
- Send an email to notify a person of their new Google Workspace account
- Send an email from users
- Send an email to users
- Simple
replace <Tag> <String>
processing - Example
Thanks to @bousquf for the following enhancement. You want to send a message from an authorized group but a group email address can't be used in the Gmail API as the sender. Alternatively, you want to send a message from a user's alternate sendas address.
Added the option mailbox <EmailAddress>
to gam sendemail
to allow specifying the sender email address used in the Gmail API as different from from <EmailAddress>
.
- https://developers.google.com/gmail/api/v1/reference/users/messages
- https://support.google.com/a/answer/176600?hl=en
<DomainName> ::= <String>(.<String>)+
<EmailAddress> ::=
<String>@<DomainName> |
<String> <<String>@<DomainName>> # The outer <> around <String>@<DomainName> are literal, e.g., IT Group<group@domain.com>
<EmailAddressList> ::= "<EmailAddress>(,<EmailAddress>)*"
<EmailAddressEntity> ::=
<EmailAddressList> | <FileSelector> | <CSVkmdSelector>
See: https://github.com/GAM-team/GAM/wiki/Collections-of-Users
<RecipientEntity> ::= <EmailAddressEntity> | (select <UserTypeEntity>)
<UserItem> ::= <EmailAddress>|<UniqueID>|<String>
<StorageBucketName> ::= <String>
<StorageObjectName> ::= <String>
<StorageBucketObjectName> ::=
https://storage.cloud.google.com/<StorageBucketName>/<StorageObjectName>|
https://storage.googleapis.com/<StorageBucketName>/<StorageObjectName>|
gs://<StorageBucketName>/<StorageObjectName>|
<StorageBucketName>/<StorageObjectName>
<UserGoogleDoc> ::=
<EmailAddress> <DriveFileIDEntity>|<DriveFileNameEntity>|(<SharedDriveEntity> <SharedDriveFileNameEntity>)
<MessageContent> ::=
(message|textmessage|htmlmessage <String>)|
(file|textfile|htmlfile <FileName> [charset <Charset>])|
(gdoc|ghtml <UserGoogleDoc>)|
(gcsdoc|gcshtml <StorageBucketObjectName>)
<Time> ::=
<Year>-<Month>-<Day>(<Space>|T)<Hour>:<Minute>:<Second>[.<MilliSeconds>](Z|(+|-(<Hour>:<Minute>))) |
(+|-)<Number>(m|h|d|w|y) |
never|
now|today
<SMTPDateHeader> ::=
date|
delivery-date|
expires|
expiry-date|
latest-delivery-time|
reply-by|
resent-date
<SMTPHeader> ::=
accept-language|
alternate-recipient|
autoforwarded|
autosubmitted|
bcc|
cc|
comments|
content-alternative|
content-base|
content-description|
content-disposition|
content-duration|
content-id|
content-identifier|
content-language|
content-location|
content-md5|
content-return|
content-transfer-encoding|
content-type|
content-features|
conversion|
conversion-with-loss|
dl-expansion-history|
deferred-delivery|
delivered-to|
discarded-x400-ipms-extensions|
discarded-x400-mts-extensions|
disclose-recipients|
disposition-notification-options|
disposition-notification-to|
encoding|
encrypted|
from|
generate-delivery-report|
importance|
in-reply-to|
incomplete-copy|
keywords|
language|
list-archive|
list-help|
list-id|
list-owner|
list-post|
list-subscribe|
list-unsubscribe|
mime-version|
message-context|
message-id|
message-type|
obsoletes|
original-encoded-information-types|
original-message-id|
originator-return-address|
pics-label|
prevent-nondelivery-report|
priority|
received|
references|
reply-to|
resent-bcc|
resent-cc|
resent-from|
resent-message-id|
resent-reply-to|
resent-sender|
resent-to|
return-path|
sender|
sensitivity|
subject|
supersedes|
to|
x400-content-identifier|
x400-content-return|
x400-content-type|
x400-mts-identifier|
x400-originator|
x400-received|
x400-recipients|
x400-trace
<SMTPHeaderList> ::= "<SMTPDateHeader|SMTPHeader>(,<SMTPDateHeader|SMTPHeader>)*"
By default, GAM uses the Gmail API to send email messages. Typically, Service Account access is used
but Client access will be used when gam.cfg todrive_clientaccess = True
.
Alternatively, GAM can use SMTP to send email messages. Messages are always sent on port 587 using starttls
.
There are four gam.cfg
variables used to configure SMTP.
-
smtp-fqdn
- Fully qualified domain name used in SMTP EHLO command; default is to use system FQDN -
smtp-host
- The host name or IP address of the SMTP server -
smtp-username
- The username used for SMTP authentication; default is to not use authentication -
smtp-password
- The password used for SMTP authentication; default is to not use authentication
GAM uses SMTP to send email messages when smtp-host
is defined. SMTP authentication is performed
when both smtp_username
and smtp_password
are defined; otherwise, the connection is made without authentication.
The smtp_password
can be specified as a plain text string or a Base64 encoded string.
-
plain text gam.cfg
-smtp_password = password
-
plain text command line
-gam config smtp_password password save
-
Base64 gam.cfg
-smtp_password = b'base64string'
-
Base64 command line
-gam config smtp_password = "b'base64string'"
save
To convert a plain text string to Base 64 encoded string perform the following command:
- Windows -
powershell "[convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes(\"password\"))"
- Linux/MacOS -
echo -n "password" | base64
You can have GAM use SMTP in conjunction with the Google Workspace SMTP relay service. Configure it at Admin Console > Apps > Google Workspace > Gmail > Routing > SMTP relay service > CONFIGURE
-
Give the service a name, e.g., "GAM SMTP"
-
1. Allowed senders
-Only addresses in my domain(s)
-
2. Authentication
- Check
Only accept mail from the specified IP addresses
- Click
Add
-
Description
- Describe the IP address, e.g., "GAM Server IP" -
IP address/range
- Enter the public IP address of your GAM server, e.g., 1.2.3.4/32 - Check
Enabled
- Click
SAVE
- Check/uncheck
Require SMTP Authentication
as desired. If checked,smtp-password
must be a Gmail Application Specific password
- Check
-
3. Encryption
- Check
Require TLS encryption
- Check
-
Click
SAVE
in the lower right
gam sendemail [recipient|to] <RecipientEntity>
[from <EmailAddress>] [mailbox <EmailAddress>] [replyto <EmailAddress>]
[cc <RecipientEntity>] [bcc <RecipientEntity>] [singlemessage]
[subject <String>]
[<MessageContent>] (replace <Tag> <String>)*
[html [<Boolean>]] (attach <FileName> [charset <Charset>])*
(embedimage <FileName> <String>)*
[newuser <EmailAddress> firstname|givenname <String> lastname|familyname <string> password <Password>]
(<SMTPDateHeader> <Time>)* (<SMTPHeader> <String>)* (header <String> <String>)*
By default, emails will be sent from the admin user named in oauth2.txt, override this with the from <EmailAddress>
option.
When using the Gmail API/SMTP, GAM gets no/little indication as to the status of the message delivery; the from user will get a non-delivery receipt if the message could not be sent to the specified recipients.
By default, messages are sent as plain text, use html
or html true
to indicate that the message is HTML.
By default, a separate email is sent to each recipient; this isolates the recipient email addresses. If any of these options are specified, a single email will be sent to all of the recipients:
cc <RecipientEntity>
bcc <RecipientEntity>
singlemessage
The <SMTPDateHeader> <Time>
argument requires <Time>
values which will be converted to RFC2822 dates. If you have these headers with values that
are not in <Time>
format, use the argument header <SMTPDateHeader> <String>
.
If you have an SMTP header that is not found in <SMTPDateHeader>
or <SMTPHeader>
, use header <String> <String>
.
You can embed images in HTML email messages.
Your HTML message will contain lines like this:
<img src="cid:image1"/>
<img src="cid:image2"/>
Your command line will have: embedimage file1.jpg image1 embedimage file2.jpg image2
You want to send an email from a user's sendas address.
Use the mailbox <EmailAddress>
to specify the user's primary email address and from <EmailAddress>
to specify the sendas address.
gam sendemail destination@email.com from useralias@domain.com mailbox user@domain.com subject "test subject" message "text body" ...
You want to send an email from a group for which you are an authorized sender.
Use the mailbox <EmailAddress>
to specify the sender email address used in the Gmail API as different from from <EmailAddress>
.
gam sendemail destination@email.com from "IT Group<group@domain.com>" mailbox user@domain.com subject "test subject" message "text body" ...
You can send a message with account details to an email address when creating|updating a user; this might be the user's secondary email address. You can specify additional recipients, e.g., help desk personnel.
gam sendemail [recipient|to] <RecipientEntity> [from <EmailAddress>]
[replyto <EmailAddress>]
[cc <RecipientEntity>] [bcc <RecipientEntity>] [singlemessage]
[subject <String>]
[<MessageContent>] (replace <Tag> <String>)*
[html [<Boolean>]] (attach <FileName> [charset <Charset>])*
(embedimage <FileName> <String>)*
[newuser <EmailAddress> firstname|givenname <String> lastname|familyname <string> password <Password>]
(<SMTPDateHeader> <Time>)* (<SMTPHeader> <String>)* (header <String> <String>)*
By default, emails will be sent from the admin user named in oauth2.txt, override this with the from <EmailAddress>
option.
When using the Gmail API, GAM gets no/little indication as to the status of the message delivery; the from user will get a non-delivery receipt if the message could not be sent to the specified recipients.
By default, messages are sent as plain text, use html
or html true
to indicate that the message is HTML.
The recipient|to
, firstname|givenname
, lastname|familyname
and password
options are required when newuser <EmailAddress>
is specified.
In the subject and message, these strings will be replaced with the specified values:
-
#givenname#
- first/given name -
#familyname#
- last/family name -
#email#
- user's email address -
#user#
- user's email address -
#username#
- portion of user's email address before @ -
#domain#
- portion of user's email after after @ -
#password#
- password
If subject
is not specified, the following value will be used:
Welcome to #domain#
If message
is not specified, the following value will be used:
Hello #givenname# #familyname#,\n\nYou have a new account at #domain#\nAccount details:\n\nUsername\n#user#\n\nPassword\n#password#\n\n Start using your new account by signing in at\nhttps://www.google.com/accounts/AccountChooser?Email=#user#&continue=https://apps.google.com/user/hub\n
If you want a language/organization specific message, use a template file: message file <FileName> [charset <Charset>]
The <SMTPDateHeader> <Time>
argument requires <Time>
values which will be converted to RFC2822 dates. If you have these headers with values that
are not in <Time>
format, use the argument header <SMTPDateHeader> <String>
.
If you have an SMTP header that is not found in <SMTPDateHeader>
or <SMTPHeader>
, use header <String> <String>
.
You can embed images in HTML email messages.
Your HTML message will contain lines like this:
<img src="cid:image1"/>
<img src="cid:image2"/>
Your command line will have: embedimage file1.jpg image1 embedimage file2.jpg image2
Send an email to a user's personal address notifying them of their new Google Workspace account;
use the newuser
option and default subject and message.
gam sendemail bob@gmail.com newuser bob@domain.com firstname Bob lastname Smith password xxxxxx
Send an email to a user's personal address notifying them of their new Google Workspace account; use custom subject and message.
gam sendemail bob@gmail.com subject "Your new #domain# account` message 'Bob, your new domain email address is bob@domain.com with password xxxxxx'
You can use a CSV file to do multiple new users.
Users.csv
personal,primaryemail,firstname,password
bob@gmail.com,bob@domain.com,Bob,xxxxxx
mary@gmail.com,mary@domain.com,Mary,xxxxxx
...
gam csv Users.csv gam sendemail "~personal" subject "Your new #domain# account` message '~~firstname~~, your new domain email address is ~~primaryemail~~ with password ~~password~~'
gam <UserTypeEntity> sendemail recipient|to <RecipientEntity>
[replyto <EmailAddress>]
[cc <RecipientEntity>] [bcc <RecipientEntity>] [singlemessage]
[subject <String>]
[<MessageContent>] (replace <Tag> <String>)*
[html [<Boolean>]] (attach <FileName> [charset <Charset>])*
(embedimage <FileName> <String>)*
[newuser <EmailAddress> firstname|givenname <String> lastname|familyname <string> password <Password>]
(<SMTPDateHeader> <Time>)* (<SMTPHeader> <String>)* (header <String> <String>)*
Emails will be sent from the users in <UserTypeEntity>
to the recipients in <RecipientEntity>
.
When using the Gmail API/SMTP, GAM gets no/little indication as to the status of the message delivery; the from user will get a non-delivery receipt if the message could not be sent to the specified recipients.
By default, messages are sent as plain text, use htmlmessage
, htmlfile
or html true
to indicate that the message is HTML.
By default, a separate email is sent to each recipient; this isolates the recipient email addresses. If any of these options are specified, a single email will be sent to all of the recipients:
cc <RecipientEntity>
bcc <RecipientEntity>
singlemessage
The <SMTPDateHeader> <Time>
argument requires <Time>
values which will be converted to RFC2822 dates. If you have these headers with values that
are not in <Time>
format, use the argument header <SMTPDateHeader> <String>
.
If you have an SMTP header that is not found in <SMTPDateHeader>
or <SMTPHeader>
, use header <String> <String>
.
You can embed images in HTML email messages.
Your HTML message will contain lines like this:
<img src="cid:image1"/>
<img src="cid:image2"/>
Your command line will have: embedimage file1.jpg image1 embedimage file2.jpg image2
gam <UserTypeEntity> sendemail [from <EmailAddress>]
[replyto <EmailAddress>]
[cc <RecipientEntity>] [bcc <RecipientEntity>] [singlemessage]
[subject <String>]
[<MessageContent>] (replace <Tag> <String>)*
[html [<Boolean>]] (attach <FileName> [charset <Charset>])*
(embedimage <FileName> <String>)*
[newuser <EmailAddress> firstname|givenname <String> lastname|familyname <string> password <Password>]
(<SMTPDateHeader> <Time>)* (<SMTPHeader> <String>)* (header <String> <String>)*
Emails will be sent to the users in <UserTypeEntity>
.
By default, emails will be sent from the admin user named in oauth2.txt, override this with the from <EmailAddress>
option.
When using the Gmail API/SMTP, GAM gets no/little indication as to the status of the message delivery; the from user will get a non-delivery receipt if the message could not be sent to the specified recipients.
By default, messages are sent as plain text, use htmlmessage
, htmlfile
or html true
to indicate that the message is HTML.
By default, a separate email is sent to each recipient; this isolates the recipient email addresses. If any of these options are specified, a single email will be sent to all of the recipients:
cc <RecipientEntity>
bcc <RecipientEntity>
singlemessage
The <SMTPDateHeader> <Time>
argument requires <Time>
values which will be converted to RFC2822 dates. If you have these headers with values that
are not in <Time>
format, use the argument header <SMTPDateHeader> <String>
.
If you have an SMTP header that is not found in <SMTPDateHeader>
or <SMTPHeader>
, use header <String> <String>
.
You can embed images in HTML email messages.
Your HTML message will contain lines like this:
<img src="cid:image1"/>
<img src="cid:image2"/>
Your command line will have: embedimage file1.jpg image1 embedimage file2.jpg image2
Send a message to a user, save the Message-ID so that a later reminder message can be sent referring to the first message.
$ gam user user1@domain.com sendemail to user2domain.com subject Test textmessage "Message"
User: user1@domain.com, Send Email to 1 Recipient
Recipient: user2@domain.com, Message: Test, Email Sent: 17677ccdfdeae7ad
$ gam redirect csv UserEmail.csv user user1@domain.com print messages ids 17677ccdfdeae7ad headers To,Subject,Message-ID
$ more UserEmail.csv
User,threadId,id,Subject,To,Message-ID
user1@comain.com,17677d3e79d5f68a,17677d3e79d5f68a,Test,user2@domain.com,<CAPMj=+p3vdDz1Mjfpe1BpNPT7g-MN1SKifjjCFDme2ftppsMJA@mail.gmail.com>
Send reminder message.
$ gam csv UserEmail.csv gam user "~User" sendemail to "~To" subject "~Subject" textmessage "Reminder Message" references "~Message-ID" in-reply-to "~Message-ID"
User: user1@domain.com, Send Email to 1 Recipient
Recipient: user2@domain.com, Message: Test, Email Sent: 17677cdfbe1146f4
Need more help? Ask on the GAM Discussion Group
Update History
Installation
- How to Install GAM7
- How to Upgrade GAMADV-XTD3 to GAM7
- How to Upgrade Legacy GAM to GAM7
- How to Update GAM7
- Verifying a GAM7 Build is Legitimate and Official
- Install GAM as Python Library
- GAM7 on Chrome OS Devices
- GAM7 on Android Devices
- Google Network Addresses
- HTTPS Proxy
- SSL Root CA Certificates
- How to Uninstall GAM7
Configuration
- Authorization
- GAM Configuration
- Running GAM7 securely on a Google Compute Engine
- Using GAM7 with a delegated admin service account
- Using GAM7 with a YubiKey
- GAM with minimal GCP rights
Notes and Information
- Upgrade Benefits
- Questions? Visit the GAM Discussion Forum
- GAM Public Chat Room
- Scripts
- Other Resources
- Drive REST API v3
- BNF Syntax
- GAM Return Codes
- Python Regular Expressions
- Rclone
Definitions
Command Processing
- Bulk Processing
- Command Line Parsing
- Command Logging and Progress
- Command data from Google Docs/Sheets/Storage
- CSV Special Characters
- CSV Input Filtering
- CSV Output Filtering
- Meta Commands and File Redirection
- Permission matches
- Tag Replace
- Todrive
Collections
Client Access
- Addresses
- Administrators
- Alert Center
- Aliases
- Calendars
- Calendars - Access
- Calendars - Events
- Chrome Auto Update Expiration Counts
- Chrome Browser Cloud Management
- Chrome Device Needs Attention Counts
- Chrome Installed Apps
- Chrome Policies
- Chrome Printers
- Chrome Profile Management
- Chrome Version Counts
- Chrome Version History
- ChromeOS Devices
- Classroom - Courses
- Classroom - Guardians
- Classroom - Invitations
- Classroom - Membership
- Cloud Channel
- Cloud Identity Devices
- Cloud Identity Groups
- Cloud Identity Groups - Membership
- Cloud Identity Policies
- Cloud Storage
- Context Aware Access Levels
- Customer
- Domains
- Domains - Verification
- Domain People - Contacts & Profiles
- Domain Shared Contacts - Global Address List
- Email Audit Monitor
- Find File Owner
- Google Data Transfers
- Groups
- Groups - Membership
- Inbound SSO
- Licenses
- Mobile Devices
- Organizational Units
- Reports
- Reseller
- Resources
- Send Email
- Schemas
- Shared Drives
- Sites
- Users
- Unmanaged Accounts
- Users - Signout and Turn off 2-Step Verification
- Vault - Takeout
- Version and Help
Special Service Account Access
Service Account Access
- Users - Analytics Admin
- Users - Application Specific Passwords
- Users - Backup Verification Codes
- Users - Calendars
- Users - Calendars - Access
- Users - Calendars - Events
- Users - Chat
- Users - Classification Labels
- Users - Classroom - Profile
- Users - Deprovision
- Users - Contacts
- Users - Contacts - Delegates
- Users - Drive - File Selection
- Users - Drive - Activity/Settings
- Users - Drive - Cleanup
- Users - Drive - Comments
- Users - Drive - Copy/Move
- Users - Drive - Files-Display
- Users - Drive - Files-Manage
- Users - Drive - Orphans
- Users - Drive - Ownership
- Users - Drive - Permissions
- Users - Drive - Query
- Users - Drive - Revisions
- Users - Drive - Shortcuts
- Users - Drive - Transfer
- Users - Forms
- Users - Gmail - Client Side Encryption
- Users - Gmail - Delegates
- Users - Gmail - Filters
- Users - Gmail - Forwarding
- Users - Gmail - Labels
- Users - Gmail - Messages/Threads
- Users - Gmail - Profile
- Users - Gmail - S/MIME
- Users - Gmail - SendAs/Signature/Vacation
- Users - Gmail - Settings
- Users - Group Membership
- Users - Keep
- Users - Looker Studio
- Users - Meet
- Users - Classroom - Profile
- Users - People - Contacts & Profiles
- Users - Photo
- Users - Profile Sharing
- Users - Shared Drives
- Users - Spreadsheets
- Users - Tasks
- Users - Tokens
- Users - YouTube
GAM Tutorials
- Account Auditing
- Calendar Settings
- Chat Bot commands
- Chrome Browser Management
- Chrome Policy Settings
- Context Aware Access levels
- Data Transfers
- Domain Verification
- Google Drive Management
- Group Settings
- Inbound SSO Settings
- Managing Admins
- Managing Classroom
- Managing Custom User Schemas
- Managing Devices
- Managing Organizations
- Managing Product Licenses
- Managing Users, Groups, Aliases, Domains, Mobile and Chrome Devices, and Resource Calendars
- OAuth Authentication Related Commands
- Print Users, Groups, Aliases, Mobile and Chrome OS devices, OUs, Licenses and Reports
- Printers
- Unmanaged Users and Invitations
- User Email Settings
- User Security Settings