0
Send Email Single Recipient
One script reply has been approved by the moderators Verified
Created by rossrekrutkin 930 days ago Viewed 11042 times
0
Submitted by adam186 Deno
Verified 753 days ago
1
import sendgrid from "npm:@sendgrid/mail@^7.7.0";
2

3
/**
4
 * @param is_message_html If `true` then the message will be sent and parsed as HTML,
5
 * otherwise it will be sent as plain text.
6
 */
7
type Sendgrid = {
8
  token: string;
9
};
10
export async function main(
11
  api_token: Sendgrid,
12
  from: string,
13
  to: string,
14
  subject: string,
15
  message: string,
16
  is_message_html: boolean,
17
) {
18
  sendgrid.setApiKey(api_token.token);
19
  const messageObject: Record<string, string> = { to, from, subject };
20
  messageObject[is_message_html ? "html" : "text"] = message;
21

22
  try {
23
    return await sendgrid.send(messageObject);
24
  } catch (error) {
25
    throw Error("\n" + JSON.stringify(error?.response?.body || error));
26
  }
27
}
28