Sample - Twilio SendGrid Mail API
POST/v3/mail/send

Send an email with Twilio SendGrid

Submits an email for delivery through Twilio SendGrid. Provide personalizations, from, and message content, and use template_id when a stored template should supply the subject and content. A 202 response confirms that SendGrid accepted the message for processing.

  • IdempotentThe SDK sends Idempotency-Key, so a retried request is only applied once.

1 parameter · 17 body fields
Content-Encodingstringoptional
Use `gzip` when sending a gzip-compressed mail body; compression must be enabled for your account by SendGrid support.
Allowed:gzip

Email submission details, including recipients, sender, content, templates, attachments, scheduling, and delivery settings. personalizations and from are required.

personalizationsarray<object>required
An array of messages and their metadata. Each object within the `personalizations` array can be thought of as a mail envelope—it defines who should receive an individual message and how that message should be handled. See [**Personalizations**](https://sendgrid.com/docs/for-developers/sending-email/personalizations/) for more information.
fromobjectrequired
Sender email object. The `email` address must use email format and should be a verified sender in your Twilio SendGrid account.
reply_toobjectoptional
Email address and optional name to receive replies. Personalization-level values override this message-level address.
reply_to_listarray<ToEmailObject>optional
An array of recipients to whom replies will be sent. Each object in this array must contain a recipient's email address. Each object in the array may optionally contain a recipient's name. You can use either the `reply_to` property or `reply_to_list` property but not both.
subjectstringoptional
The global or _message level_ subject of your email. Subject lines set in personalizations objects will override this global subject line. See line length limits specified in [RFC 2822](https://www.rfc-editor.org/rfc/rfc2822#section-2.1.1) for guidance on subject line character limits.
contentarray<object>optional
An array of objects, each containing a message body's content and [MIME type](https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types). You must specify at least one MIME type and may include multiple. To include more than one MIME type, add an object for each type to the array.
attachmentsarray<object>optional
An array of objects where you can define any attachments to be included with the message. Each object contains a `content` property, which must be a Base64 encoded string of the attachment itself, and `type`, `filename`, `disposition`, and `content_id` properties that tell SendGrid how to handle the attachment.
template_idstringoptional
An email template ID. A template that contains a subject and content—either text or html—will override any subject and content values specified at the `personalizations` or message level. If a template ID begins with `d-`, it is a dynamic template and will work with the `dynamic_template_data` property. If the template ID does not begin with `d-`, it is a standard SendGrid template and will work with the `substitutions` property. See [**How to Send an Email with Dynamic Templates**](https://docs.sendgrid.com/ui/sending-email/how-to-send-an-email-with-dynamic-templates) for more information about working with templates.
headersobjectoptional
A collection of JSON property name and property value pairs allowing you to specify handling instructions for your email. You may not override the following headers: `x-sg-id`, `x-sg-eid`, `received`, `dkim-signature`, `Content-Type`, `Content-Transfer-Encoding`, `To`, `From`, `Subject`, `Reply-To`, `CC`, `BCC`.
categoriesarray<string>optional
An array of category names assigned to this message. Categories allow you to group messages by categories you define. Categories should be used sparingly to create general groups that are relevant to you. Categories are not meant to be used to track individual mail sends. For more granular categorization and tracking, use the `custom_args` property. A category name cannot exceed 255 characters. See [**Working with Categories**](https://docs.sendgrid.com/for-developers/sending-email/categories) for more information.
custom_argsstringoptional
Values that are specific to the entire send that will be carried along with the email and its activity data. Substitutions will not be made on custom arguments, so any string that is assigned to this property will be assumed to be the custom argument that you would like to be used. This parameter is overridden by `custom_args` set at the personalizations level. Total `custom_args` size may not exceed 10,000 bytes.
send_atintegeroptional
A [unix timestamp](https://en.wikipedia.org/wiki/Unix_time) allowing you to specify when your email should be sent. A send cannot be scheduled more than 72 hours in advance. This property may be overridden by the `send_at` property set at the personalizations level.
batch_idstringoptional
An ID representing a batch of emails to be sent at the same time. Including a `batch_id` in your request allows you to include this email in that batch. It also enables you to cancel or pause the delivery of that batch. See the [Scheduled Sends API](https://sendgrid.com/docs/api-reference/) for more information about scheduling your email.
asmobjectoptional
An object allowing you to specify how to handle unsubscribes. With SendGrid, an unsubscribe is the action an email recipient takes when they opt-out of receiving your messages. A suppression is the action you take as a sender to filter or suppress an unsubscribed address from your email send. From the perspective of the recipient, your suppression is the result of their unsubscribe. See [**Suppression Groups**](https://www.twilio.com/docs/sendgrid/api-reference/suppressions-unsubscribe-groups/create-a-new-suppression-group) for more information.
ip_pool_namestringoptional
The IP Pool that you would like to send this email from. IP Pools allow you to group your dedicated Twilio SendGrid IP addresses in order to have more control over your deliverability. See [**IP Pools**](https://docs.sendgrid.com/ui/account-and-settings/ip-pools) for more information.
mail_settingsobjectoptional
A collection of different mail settings that you can use to specify how you would like this email to be handled. Mail settings provide extra functionality to your mail send. See [**Mail Settings**](https://docs.sendgrid.com/ui/account-and-settings/mail) for more information.
tracking_settingsobjectoptional
Settings to determine how you would like to track the metrics of how your recipients interact with your email. See [**Tracking Settings**](https://docs.sendgrid.com/ui/account-and-settings/tracking) for more information.

8 status codes
202Confirms that SendGrid accepted the email for processing.
400Returned when the mail submission fails request validation; the response can contain an `errors` array with field-level messages and an optional error `id`.
errorsarray<object>optional
idstringoptional
When applicable, this property value will be an error ID.
401Returned when the request is unauthorized; the response can contain an `errors` array and an optional error `id`.
errorsarray<object>optional
idstringoptional
When applicable, this property value will be an error ID.
403Returned when the mail submission is forbidden; the response can contain an `errors` array and an optional error `id`.
errorsarray<object>optional
idstringoptional
When applicable, this property value will be an error ID.
404Returned when a requested resource is not found; the response can contain an `errors` array and an optional error `id`.
errorsarray<object>optional
idstringoptional
When applicable, this property value will be an error ID.
405Returned when the HTTP method is not allowed; the response can contain an `errors` array and an optional error `id`.
errorsarray<object>optional
idstringoptional
When applicable, this property value will be an error ID.
413Returned when the mail submission content is too large; the response can contain an `errors` array and an optional error `id`.
errorsarray<object>optional
idstringoptional
When applicable, this property value will be an error ID.
500Returned when SendGrid encounters an internal server error; the response can contain an `errors` array and an optional error `id`.
errorsarray<object>optional
idstringoptional
When applicable, this property value will be an error ID.

Error handling

A 400 is returned when the request fails validation, a 401 when authentication is not accepted, a 403 when the request is forbidden, and a 404 when the requested resource is not found. A 405 is returned when the HTTP method is not allowed, a 413 when the request content is too large, and a 500 when SendGrid encounters an internal error. personalizations and from are required; each personalization must include to, and Content-Encoding must be gzip when supplied.