import { encode as base64UrlEncode } from "https://deno.land/std@0.82.0/encoding/base64url.ts";
/**
 * @param user_id User's email address. The special value `me` can be used to indicate the authenticated user.
 */
type Gmail = {
  token: string;
};
// Helper function to Base64-encode UTF-8 strings
function base64EncodeUTF8(str) {
  // Encode string as UTF-8 then convert to base64
  return btoa(unescape(encodeURIComponent(str)));
}
export async function main(
  gmail_auth: Gmail,
  to_email: string,
  subject: string,
  message: string,
  user_id: string = "me",
) {
  const token = gmail_auth["token"];
  if (!token) {
    throw Error(`
    No authentication token was found.
    Go to "https://app.windmill.dev/resources?connect_app=gmail" to connect Gmail, 
    then select your token from the dropdown in the arguments window.
    (Click "Refresh" if you don't see your resource in the list.)\n`);
  }
  // This breaks subject encoding of umlauts:
  //const text =
  //  `From: <${user_id}>\nTo: <${to_email}>\nSubject: ${subject}\nContent-Type: text/html; charset="UTF-8"\n\r <p>${message}</p>`;
  //const textEncoder = new TextEncoder();
  //const email = base64UrlEncode(textEncoder.encode(text));
  //const body = JSON.stringify({
  //  raw: email,
  //});
  // Instead: wrap the encoded subject in RFC 2047 format
  const encodedSubject = `=?UTF-8?B?${base64EncodeUTF8(subject)}?=`;
  // Build the raw email string with the encoded subject header
  const text = `From: <${user_id}>\n` +
    `To: <${to_email}>\n` +
    `Subject: ${encodedSubject}\n` +
    `Content-Type: text/html; charset="UTF-8"\n\n` +
    `<p>${message}</p>`;
  // Convert the email string into a Uint8Array and then Base64url encode it
  const textEncoder = new TextEncoder();
  const emailBytes = textEncoder.encode(text);
  const email = base64UrlEncode(emailBytes);
  
  // Prepare the request body for the Gmail API
  const body = JSON.stringify({
    raw: email,
  });
  const SEND_URL =
    `https://gmail.googleapis.com/gmail/v1/users/${user_id}/messages/send`;
  const response = await fetch(SEND_URL, {
    method: "POST",
    headers: { Authorization: "Bearer " + token },
    body,
  });
  const result = await handleSendEmailResult(await response.json(), to_email);
  return result;
}
async function handleSendEmailResult(result: object, to_email: string) {
  if (Object.keys(result).includes("error")) {
    return Promise.reject({ wm_to_email: to_email, ...result });
  }
  return result;
}
 Submitted by alexander koderman64 271 days ago
import { encode as base64UrlEncode } from "https://deno.land/std@0.82.0/encoding/base64url.ts";
/**
 * @param user_id User's email address. The special value `me` can be used to indicate the authenticated user.
 */
type Gmail = {
  token: string;
};
// Helper function to Base64-encode UTF-8 strings
function base64EncodeUTF8(str) {
  // Encode string as UTF-8 then convert to base64
  return btoa(unescape(encodeURIComponent(str)));
}
export async function main(
  gmail_auth: Gmail,
  to_email: string,
  subject: string,
  message: string,
  user_id: string = "me",
) {
  const token = gmail_auth["token"];
  if (!token) {
    throw Error(`
    No authentication token was found.
    Go to "https://app.windmill.dev/resources?connect_app=gmail" to connect Gmail, 
    then select your token from the dropdown in the arguments window.
    (Click "Refresh" if you don't see your resource in the list.)\n`);
  }
  // This breaks subject encoding of umlauts:
  //const text =
  //  `From: <${user_id}>\nTo: <${to_email}>\nSubject: ${subject}\nContent-Type: text/html; charset="UTF-8"\n\r <p>${message}</p>`;
  //const textEncoder = new TextEncoder();
  //const email = base64UrlEncode(textEncoder.encode(text));
  //const body = JSON.stringify({
  //  raw: email,
  //});
  // Instead: wrap the encoded subject in RFC 2047 format
  const encodedSubject = `=?UTF-8?B?${base64EncodeUTF8(subject)}?=`;
  // Build the raw email string with the encoded subject header
  const text = `From: <${user_id}>\n` +
    `To: <${to_email}>\n` +
    `Subject: ${encodedSubject}\n` +
    `Content-Type: text/html; charset="UTF-8"\n\n` +
    `<p>${message}</p>`;
  // Convert the email string into a Uint8Array and then Base64url encode it
  const textEncoder = new TextEncoder();
  const emailBytes = textEncoder.encode(text);
  const email = base64UrlEncode(emailBytes); // Assuming base64UrlEncode is defined elsewhere
  // Prepare the request body for the Gmail API
  const body = JSON.stringify({
    raw: email,
  });
  const SEND_URL =
    `https://gmail.googleapis.com/gmail/v1/users/${user_id}/messages/send`;
  const response = await fetch(SEND_URL, {
    method: "POST",
    headers: { Authorization: "Bearer " + token },
    body,
  });
  const result = await handleSendEmailResult(await response.json(), to_email);
  return result;
}
async function handleSendEmailResult(result: object, to_email: string) {
  if (Object.keys(result).includes("error")) {
    return Promise.reject({ wm_to_email: to_email, ...result });
  }
  return result;
}
 Submitted by alexander koderman64 271 days ago