| 1 | import { encode as base64UrlEncode } from "https://deno.land/std@0.82.0/encoding/base64url.ts";
 | 
 | 2 | 
 | 
 | 3 | 
 | 
 | 4 |  * @param user_id User's email address. The special value `me` can be used to indicate the authenticated user.
 | 
 | 5 |  */
 | 
 | 6 | type Gmail = {
 | 
 | 7 |   token: string;
 | 
 | 8 | };
 | 
 | 9 | 
 | 
 | 10 | export async function main(
 | 
 | 11 |   gmail_auth: Gmail,
 | 
 | 12 |   to_email: string,
 | 
 | 13 |   subject: string,
 | 
 | 14 |   message: string,
 | 
 | 15 |   file_name: string,
 | 
 | 16 |   file_content: string,
 | 
 | 17 |   user_id: string = "me",
 | 
 | 18 | ) {
 | 
 | 19 |   const token = gmail_auth["token"];
 | 
 | 20 |   if (!token) {
 | 
 | 21 |     throw Error(`
 | 
 | 22 |     No authentication token was found.
 | 
 | 23 |     Go to "https://app.windmill.dev/resources?connect_app=gmail" to connect Gmail, 
 | 
 | 24 |     then select your token from the dropdown in the arguments window.
 | 
 | 25 |     (Click "Refresh" if you don't see your resource in the list.)\n`);
 | 
 | 26 |   }
 | 
 | 27 | 
 | 
 | 28 |   const boundary = "boundary_string";
 | 
 | 29 | 
 | 
 | 30 |   const text = [
 | 
 | 31 |     `Content-Type: multipart/mixed; boundary="${boundary}"\r\n`,
 | 
 | 32 |     'MIME-Version: 1.0\r\n',
 | 
 | 33 |     `From: ${user_id}\r\n`,
 | 
 | 34 |     `To: ${to_email}\r\n`,
 | 
 | 35 |     `Subject: ${subject}\r\n\r\n`,
 | 
 | 36 | 
 | 
 | 37 |     `--${boundary}\r\n`,
 | 
 | 38 |     'Content-Type: text/plain; charset="UTF-8"\r\n',
 | 
 | 39 |     'MIME-Version: 1.0\r\n',
 | 
 | 40 |     'Content-Transfer-Encoding: 7bit\r\n\r\n',
 | 
 | 41 | 
 | 
 | 42 |     `${message}\r\n\r\n`,
 | 
 | 43 | 
 | 
 | 44 |     `--${boundary}\r\n`,
 | 
 | 45 |     'Content-Type: application/octet-stream\r\n',
 | 
 | 46 |     'MIME-Version: 1.0\r\n',
 | 
 | 47 |     'Content-Transfer-Encoding: base64\r\n',
 | 
 | 48 |     `Content-Disposition: attachment; filename="${file_name}"\r\n\r\n`,
 | 
 | 49 | 
 | 
 | 50 |     `${file_content}\r\n\r\n`,
 | 
 | 51 | 
 | 
 | 52 |     `--${boundary}--`
 | 
 | 53 |   ].join('');
 | 
 | 54 | 
 | 
 | 55 |   const textEncoder = new TextEncoder();
 | 
 | 56 |   const email = base64UrlEncode(textEncoder.encode(text));
 | 
 | 57 |   const body = JSON.stringify({
 | 
 | 58 |     raw: email,
 | 
 | 59 |   });
 | 
 | 60 |   const SEND_URL =
 | 
 | 61 |     `https://gmail.googleapis.com/gmail/v1/users/${user_id}/messages/send?uploadType=multipart`;
 | 
 | 62 | 
 | 
 | 63 |   const response = await fetch(SEND_URL, {
 | 
 | 64 |     method: "POST",
 | 
 | 65 |     headers: { Authorization: "Bearer " + token },
 | 
 | 66 |     body,
 | 
 | 67 |   });
 | 
 | 68 | 
 | 
 | 69 |   const result = await handleSendEmailResult(await response.json(), to_email);
 | 
 | 70 |   return result;
 | 
 | 71 | }
 | 
 | 72 | 
 | 
 | 73 | async function handleSendEmailResult(result: object, to_email: string) {
 | 
 | 74 |   if (Object.keys(result).includes("error")) {
 | 
 | 75 |     return Promise.reject({ wm_to_email: to_email, ...result });
 | 
 | 76 |   }
 | 
 | 77 | 
 | 
 | 78 |   return result;
 | 
 | 79 | }
 |