Send Email Multiple Recipients

Script sendgrid Verified

by stephanebooop ยท 6/6/2022

The script

Submitted by hugo989 Bun
Verified 3 days ago
1
import sendgrid from "@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 | string[]> = {
20
    to,
21
    from,
22
    subject,
23
  };
24
  messageObject[is_message_html ? "html" : "text"] = message;
25

26
  try {
27
    return await sendgrid.send(messageObject, true);
28
  } catch (error) {
29
    throw Error("\n" + JSON.stringify(error?.response?.body || error));
30
  }
31
}
32

Other submissions
  • Submitted by adam186 Deno
    Created 395 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 | string[]> = {
    20
        to,
    21
        from,
    22
        subject,
    23
      };
    24
      messageObject[is_message_html ? "html" : "text"] = message;
    25
    
    
    26
      try {
    27
        return await sendgrid.send(messageObject, true);
    28
      } catch (error) {
    29
        throw Error("\n" + JSON.stringify(error?.response?.body || error));
    30
      }
    31
    }
    32