Skip to contentSkip to navigationSkip to topbar
Rate this page:
On this page

Send email with Twilio SendGrid


The CLI features a built-in integration with Twilio SendGrid, allowing you to send emails directly from your terminal.


Set your SendGrid API Key

set-your-sendgrid-api-key page anchor

The first prerequisite to send an email with the Twilio CLI is to have a SendGrid API Key(link takes you to an external page) set as the SENDGRID_API_KEY environment variable.

Create and copy a new SendGrid API Key, or copy an existing one, then run:


_10
export SENDGRID_API_KEY=the_key_you_copied_from_SendGrid

(warning)

Warning

Remember, emails will fail to send if you have not defined your SENDGRID_API_KEY environment variable. Refer to the previous directions to do so.


Run twilio email:set to set defaults for the sender's email address and subject line.


_10
$ twilio email:set
_10
? Default email address of the sender: your.email@example.com
_10
? Default subject line for all emails: Ahoy, there!
_10
Default sending email address has been set to: your.email@example.com
_10
Default subject line has been set to: Ahoy, there!
_10
twilio-cli configuration saved to "/Users/example/.twilio-cli/config.json"

After you set these defaults, twilio email:send will automatically apply them to any sent emails.


To send emails with an interactive prompt, run:


_10
twilio email:send

You will be prompted for details such as the recipient's email address, email text, and if you want to provide an attachment.

If you'd like to skip the prompt, provide details as flags instead:


_10
twilio email:send \
_10
--to="me@example.com" \
_10
--text="Look at this fluff: https://unsplash.com/photos/uhnZZUaTIOs"

Override defaults

override-defaults page anchor

To change the sending email address or subject line, you can either re-run twilio email:set, or use the corresponding flag to set a new value for the item you want to change in that specific command.

For example, to override the default subject:


_10
twilio email:send \
_10
--to="me@example.com" \
_10
--subject="That cat pic you wanted" \
_10
--text="Look at this fluff: https://unsplash.com/photos/uhnZZUaTIOs"


To send an email with an attachment, run twilio email:send and wait to be prompted to add an attachment. You can also use --attachment=filePath to attach a file:


_10
twilio email:send \
_10
--attachment=/Users/example/Downloads/cutest-kitteh.png

(warning)

Warning

You may run into permission issues when the CLI tries to read your file. If so, use sudo with the -E flag to preserve your SendGrid key:


_10
sudo -E twilio email:send \
_10
--attachment=/Users/example/Downloads/cutest-kitteh.png

Send command output as an attachment

send-command-output-as-an-attachment page anchor

To send the output of a different command as an email attachment, pipe that command to twilio email:send.


_10
ps au \
_10
| twilio email:send \
_10
--to="me@example.com" \
_10
--text="See attachment"

If a default sending email address and subject line has been set, the command will automatically use the defaults, and you only need to include values for --text and --to.

If there is not a default subject line and sender's email address, all flags need to be included to send the output of the piped command.


_10
ps au \
_10
| twilio email:send \
_10
--from="me@example.com" \
_10
--to="me@example.com" \
_10
--subject="Current processes" \
_10
--text="See attachment"


Rate this page: