Send an email

To send an email using SendGrid, you can use @sendgrid/mail SDK. This example demonstrates how to send an email on every script request. You should set SendGrid Api Key to SENDGRID_API_KEY environment secret to be able to use SendGrid SDK.

See example below:

import * as BunnySDK from "@bunny.net/edgescript-sdk";
import sgMail from "https://esm.sh/@sendgrid/[email protected]";

// Set your SendGrid API key from environment variable SENDGRID_API_KEY
sgMail.setApiKey(Deno.env.get('SENDGRID_API_KEY'));

BunnySDK.net.http.serve(async (req) => { 
    const msg = {
        to: '[email protected]',
        from: '[email protected]',
        subject: 'Welcome to Bunny Edge Scripting!',
        text: 'This email was sent directly from the edge.',
    };

    try {
        await sgMail.send(msg);
        return new Response("Email sent successfully!");
    } catch (error) {
        console.error(error);
        return new Response("Failed to send email.", { status: 500 });
    }
});