# SendGrid

## SendGrid

Find the documentation, sample code, and developer tools you need to build your transactional and marketing email solutions on the platform that offers a 99% deliverability rate.

[Let's build](#send-your-first-email)

## Tutorial

```js !sample
const sgMail = require('@sendgrid/mail');
sgMail.setApiKey(process.env.SENDGRID_API_KEY);
const msg = {
  to: 'recipient@example.com',
  from: 'sender@example.com',
  subject: 'Ahoy!',
  html: 'Ahoy, World!',
};

sgMail
  .send(msg)
  .then((error) => console.error(error));
```

1. Complete Domain Authentication.
2. Send an email using the Mail Send API.
3. Twilio SendGrid sends the "Ahoy, World!" email to the recipient.

Tutorial code output: "Ahoy, world!"

[Find more examples](/docs/sendgrid/api-reference/mail-send/mail-send)

## Send your first email

Get started quickly with the Twilio SendGrid Mail Send API and our open-source helper libraries. The sample code below will send your first message in no time.

## C\#

```csharp
using System;
using System.Threading.Tasks;
using SendGrid;
using SendGrid.Helpers.Mail;

class Program
{
    static async Task Main()
    {
        var apiKey = Environment.GetEnvironmentVariable("SENDGRID_API_KEY");
        var client = new SendGridClient(apiKey);
        var from = new EmailAddress("test@example.com", "Example User");
        var subject = "Sending with Twilio SendGrid is Fun";
        var to = new EmailAddress("test@example.com", "Example User");
        var plainTextContent = "and easy to do anywhere, even with C#";
        var htmlContent = "<strong>and easy to do anywhere, even with C#</strong>";
        var msg = MailHelper.CreateSingleEmail(from, to, subject, plainTextContent, htmlContent);
        var response = await client.SendEmailAsync(msg).ConfigureAwait(false);
    }
}
```

## Go

```go
package main

import (
	"fmt"
	"log"
	"os"

	"github.com/sendgrid/sendgrid-go"
	"github.com/sendgrid/sendgrid-go/helpers/mail"
)

func main() {
	from := mail.NewEmail("Example User", "test@example.com")
	subject := "Sending with Twilio SendGrid is Fun"
	to := mail.NewEmail("Example User", "test@example.com")
	plainTextContent := "and easy to do anywhere, even with Go"
	htmlContent := "<strong>and easy to do anywhere, even with Go</strong>"
	message := mail.NewSingleEmail(from, subject, to, plainTextContent, htmlContent)
	client := sendgrid.NewSendClient(os.Getenv("SENDGRID_API_KEY"))
	response, err := client.Send(message)
	if err != nil {
		log.Println(err)
	} else {
		fmt.Println(response.StatusCode)
		fmt.Println(response.Body)
		fmt.Println(response.Headers)
	}
}
```

## Java

```java
import com.sendgrid.*;
import java.io.IOException;

public class Example {
  public static void main(String[] args) throws IOException {
    Email from = new Email("test@example.com");
    String subject = "Sending with Twilio SendGrid is Fun";
    Email to = new Email("test@example.com");
    Content content = new Content("text/plain", "and easy to do anywhere, even with Java");
    Mail mail = new Mail(from, subject, to, content);

    SendGrid sg = new SendGrid(System.getenv("SENDGRID_API_KEY"));
    Request request = new Request();
    try {
      request.setMethod(Method.POST);
      request.setEndpoint("mail/send");
      request.setBody(mail.build());
      Response response = sg.api(request);
      System.out.println(response.getStatusCode());
      System.out.println(response.getBody());
      System.out.println(response.getHeaders());
    } catch (IOException ex) {
      throw ex;
    }
  }
}
```

## Node.js

```js
const sgMail = require('@sendgrid/mail');
sgMail.setApiKey(process.env.SENDGRID_API_KEY);

const msg = {
  to: 'test@example.com',
  from: 'test@example.com', // Use the email address or domain you verified above
  subject: 'Sending with Twilio SendGrid is Fun',
  text: 'and easy to do anywhere, even with Node.js',
  html: '<strong>and easy to do anywhere, even with Node.js</strong>',
};

(async () => {
  try {
    await sgMail.send(msg);
  } catch (error) {
    console.error(error);

    if (error.response) {
      console.error(error.response.body)
    }
  }
})();
```

## PHP

```php
$email = new \SendGrid\Mail\Mail();
$email->setFrom("test@example.com", "Example User");
$email->setSubject("Sending with Twilio SendGrid is Fun");
$email->addTo("test@example.com", "Example User");
$email->addContent("text/plain", "and easy to do anywhere, even with PHP");
$email->addContent(
    "text/html", "<strong>and easy to do anywhere, even with PHP</strong>"
);
$sendgrid = new \SendGrid(getenv('SENDGRID_API_KEY'));
try {
    $response = $sendgrid->send($email);
    print $response->statusCode() . "\n";
    print_r($response->headers());
    print $response->body() . "\n";
} catch (Exception $e) {
    echo 'Caught exception: '. $e->getMessage() ."\n";
}
```

## Python

```python
import sendgrid
import os
from sendgrid.helpers.mail import *

sg = sendgrid.SendGridAPIClient(api_key=os.environ.get('SENDGRID_API_KEY'))
from_email = Email("test@example.com")
to_email = To("test@example.com")
subject = "Sending with SendGrid is Fun"
content = Content("text/plain", "and easy to do anywhere, even with Python")
mail = Mail(from_email, to_email, subject, content)
response = sg.client.mail.send.post(request_body=mail.get())
print(response.status_code)
print(response.body)
print(response.headers)
```

## Ruby

```ruby
require 'sendgrid-ruby'
include SendGrid

from = SendGrid::Email.new(email: 'test@example.com')
to = SendGrid::Email.new(email: 'test@example.com')
subject = 'Sending with Twilio SendGrid is Fun'
content = SendGrid::Content.new(type: 'text/plain', value: 'and easy to do anywhere, even with Ruby')
mail = SendGrid::Mail.new(from, subject, to, content)

sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY'])
response = sg.client.mail._('send').post(request_body: mail.to_json)
puts response.status_code
puts response.body
puts response.parsed_body
puts response.headers
```

### Email API quickstarts

Jump to an email quickstart in your programming language of choice for a full guide on how to implement the code samples above.

* [C#](/docs/sendgrid/for-developers/sending-email/email-api-quickstart-for-c)
* [Go](/docs/sendgrid/for-developers/sending-email/quickstart-go)
* [Java](/docs/sendgrid/for-developers/sending-email/email-quickstart-for-java)
* [Node.js](/docs/sendgrid/for-developers/sending-email/quickstart-nodejs)
* [PHP](/docs/sendgrid/for-developers/sending-email/quickstart-php)
* [Python](/docs/sendgrid/for-developers/sending-email/quickstart-python)
* [Ruby](/docs/sendgrid/for-developers/sending-email/quickstart-ruby)

## Do more with email

Sending email is just the start. With SendGrid, you get rich data that allows you to establish and maintain a complete email program. You can also parse inbound messages to create rich email-based experiences for your customers.

Looking to leverage SendGrid's reliability and scale for your SMTP needs or integrate with a cloud partner such as Azure? You can do that too.

https://www.youtube.com/watch?v=2OF2hLjkKOc

### Beyond sending your first email

* [Email API Onboarding Guide](/docs/sendgrid/onboarding/email-api)
* [How to Set Up Domain Authentication](/docs/sendgrid/ui/account-and-settings/how-to-set-up-domain-authentication)
* [API reference](/docs/sendgrid/api-reference)
* [Email Glossary](/docs/sendgrid/glossary/dmarc)

### SMTP

* [How to Send an SMTP Email](/docs/sendgrid/for-developers/sending-email/getting-started-smtp)
* [Building an X-SMTPAPI Header](/docs/sendgrid/for-developers/sending-email/building-an-x-smtpapi-header)

### Send with a SendGrid Partner

* [Microsoft Azure](/docs/sendgrid/for-developers/partners/microsoft-azure-2021)

### Templates and dynamic data

* [How to Send an Email with Dynamic Templates](/docs/sendgrid/ui/sending-email/how-to-send-an-email-with-dynamic-templates)
* [Personalizations](/docs/sendgrid/for-developers/sending-email/personalizations)
* [Using Handlebars](/docs/sendgrid/for-developers/sending-email/using-handlebars)

### Analytics and reporting

* [Getting Started with the Event Webhook](/docs/sendgrid/for-developers/tracking-events/getting-started-event-webhook)
* [Event Webhook Reference](/docs/sendgrid/for-developers/tracking-events/event)
* [Email Activity Feed](/docs/sendgrid/ui/analytics-and-reporting/email-activity-feed)
* [SendGrid Engagement Quality API](/docs/sendgrid/api-reference/sendgrid-engagement-quality-api)

### Account and settings

* [API Keys](/docs/sendgrid/ui/account-and-settings/api-keys)
* [Subusers](/docs/sendgrid/ui/account-and-settings/subusers)
* [Teammates](/docs/sendgrid/ui/account-and-settings/teammates)
* [How to Set Up Domain Authentication](/docs/sendgrid/ui/account-and-settings/how-to-set-up-domain-authentication)
* [Single Sign On](/docs/sendgrid/ui/account-and-settings/sso)

### Parsing email

* [Setting Up the Inbound Parse Webhook](/docs/sendgrid/for-developers/parsing-email/setting-up-the-inbound-parse-webhook)
* [Inbound Parse API](/docs/sendgrid/api-reference/settings-inbound-parse/)

### SDKs and tools

* [C#](https://github.com/sendgrid/sendgrid-csharp)
* [Go](https://github.com/sendgrid/sendgrid-go)
* [Java](https://github.com/sendgrid/sendgrid-java)
* [Node.js](https://github.com/sendgrid/sendgrid-nodejs)
* [PHP](https://github.com/sendgrid/sendgrid-php)
* [Python](https://github.com/sendgrid/sendgrid-python)
* [Ruby](https://github.com/sendgrid/sendgrid-ruby)
* [OpenAPI specification](https://github.com/twilio/sendgrid-oai)

## Get started with Marketing Campaigns

Create beautifully designed messages, personalized to each recipient, with best in class deliverability when you send with Marketing Campaigns.

### Build and send with Marketing Campaigns

* [How to Send Email with Marketing Campaigns](/docs/sendgrid/ui/sending-email/how-to-send-email-with-marketing-campaigns)
* [Getting Started with Automation](/docs/sendgrid/ui/sending-email/getting-started-with-automation)

### Manage contacts

* [Create and Manage Contacts](/docs/sendgrid/ui/managing-contacts/create-and-manage-contacts)
* [Building your Contact List](/docs/sendgrid/ui/managing-contacts/building-your-contact-list)
* [Segmenting your Contacts](/docs/sendgrid/ui/managing-contacts/segmenting-your-contacts)
* [Using Custom Fields](/docs/sendgrid/ui/managing-contacts/custom-fields)

### Work with designs

* [Working with Marketing Campaigns Email Designs](/docs/sendgrid/ui/sending-email/working-with-marketing-campaigns-email-designs)
* [The Design and Code Editor](/docs/sendgrid/ui/sending-email/editor)
* [Adding Dynamic Content with Handlebars in Marketing Campaigns](/docs/sendgrid/ui/sending-email/adding-dynamic-content-with-handlebars-in-marketing-campaigns)

## Related Products

Explore Twilio's other communication channels to build customer engagement infrastructure that fits the unique requirements of your business.

### Messaging

Send and receive SMS/MMS and/or WhatsApp messages with the Twilio's Programmable Messaging API.

[Product Docs](/docs/messaging)

### Voice

Quickly make and receive voice calls in your application with Twilio Voice.

[Product Docs](/docs/voice)

### Flex

Build your digital engagement center for sales and customer support teams with Twilio Flex.

[Product Docs](/docs/flex)
