Martin Smaha
Published © GPL3+

Easy Email Notifications with SendGrid API

Use a free SendGrid account and this example project code to get emails direct from your project.

EasyProtip1 hour320
Easy Email Notifications with SendGrid API

Things used in this project

Story

Read more

Code

SendGrid Example

Arduino
Simple example of how to use the SendGrid API
/*
 *  Simple Email notification with SendGrid API and HTTPS POST
 */

#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>

const char* ssid     = "your wifi id";
const char* password = "your wifi password";

const char* host = "api.sendgrid.com";
const char* url = "https://api.sendgrid.com/v3/mail/send";
const char* apiKey = "paste your SendGrid API Key here";
const int httpsPort = 443;
const char* emailMessageToSend =
"{"
  "\"personalizations\": ["
    "{"
      "\"to\": ["
        "{"
          "\"email\": \"your_email_address@gmail.com\","
          "\"name\": \"your_name\""
        "}"
      "],"
      "\"subject\": \"SendGrid Test Email\"" /* put your subject here */
    "}"
  "],"
  "\"from\": {"
    "\"email\": \"sam.smith@example.com\","
    "\"name\": \"Sam Smith\""
  "},"
  "\"reply_to\": {"
    "\"email\": \"sam.smith@example.com\","
    "\"name\": \"Sam Smith\""
  "},"
  "\"content\": ["
      "{"
        "\"type\": \"text/plain\","
        "\"value\": \"this is a test message\"" /* put your message text here */
      "}"
    "]"
"}";



void sendMessageHTTPS(String msg) {
  
  // Use WiFiClientSecure class to create TLS connection
  WiFiClientSecure client;
  Serial.print("connecting to : '");
  Serial.print(host);
  Serial.println("'");
  
  if (!client.connect(host, httpsPort)) {
    Serial.println("connection failed");
    return;
  }

  Serial.print("requesting URL: '");
  Serial.print(url);
  Serial.println("'");

  client.print(String("POST ") + url + " HTTP/1.1\r\n" +
          "Host: " + host + "\r\n" +
          "Connection: close\r\n" +
          "Accept: */*\r\n" +
          "User-Agent: Mozilla/4.0 (compatible; esp8266 Lua; Windows NT 5.1)\r\n" +
          "Content-Type: application/json;charset=utf-8\r\n" +
          "Authorization: Bearer "+apiKey+"\r\n" + 
          "Content-Length: "+msg.length()+"\r\n" +
          "\r\n" + 
          msg + "\r\n");

  Serial.println("request sent");
  unsigned long timeout = millis();
  while (client.available() == 0) {
    if (millis() - timeout > 5000) {
      Serial.println(">>> Client Timeout !");
      client.stop();
      return;
    }
  }
  
  // Read all the lines of the reply from server and print them to Serial
  while(client.available()){
    String line = client.readStringUntil('\r');
    Serial.print(line);
  }
}


void setup() {
  Serial.begin(115200);
  delay(100);

  // We start by connecting to a WiFi network

  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
  
  WiFi.begin(ssid, password);
  
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected");  
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
  pinMode(0, OUTPUT);

  sendMessageHTTPS(emailMessageToSend);
}


void loop() {
  while(true){
  digitalWrite(0, HIGH);
  delay(500);
  digitalWrite(0, LOW);
  delay(500);
  }
}

Credits

Martin Smaha

Martin Smaha

4 projects • 4 followers
Designing hardware and software since 1975.
Contact

Comments