Edits history of script submission #23355 for ' Send Email (outlook)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    import * as wmill from "windmill-client"
    import { S3Object } from "windmill-client"
    
    /**
     * Send Email
     * Send an email from the connected mailbox, with optional cc/bcc, reply-to and file attachments from object storage (about 3 MB in total). Requires Mail.Send (a connection default); a shared mailbox also needs Mail.Send.Shared, not a default.
     */
    export async function main(
      auth: RT.Outlook,
      to: string[],
      subject: string,
      body: string,
      content_type: "HTML" | "Text" = "HTML",
      cc: string[] | undefined,
      bcc: string[] | undefined,
      reply_to: string[] | undefined,
      attachments: S3Object[] | undefined,
      save_to_sent_items: boolean = true,
      mailbox: string | undefined
    ) {
      const base = `https://graph.microsoft.com/v1.0/${
        mailbox != null && mailbox !== ""
          ? `users/${encodeURIComponent(mailbox)}`
          : "me"
      }`
      const recipients = (addresses: string[] | undefined) =>
        (addresses ?? [])
          .filter((a) => a != null && a.trim() !== "")
          .map((address) => ({ emailAddress: { address } }))
    
      const files = []
      for (const file of attachments ?? []) {
        const bytes = await wmill.loadS3File(file)
        if (!bytes) throw new Error(`Could not load ${file.s3}`)
        files.push({
          "@odata.type": "#microsoft.graph.fileAttachment",
          name: file.s3.split("/").pop(),
          contentBytes: Buffer.from(bytes).toString("base64"),
        })
      }
    
      const response = await fetch(`${base}/sendMail`, {
        method: "POST",
        headers: {
          Authorization: `Bearer ${auth.token}`,
          "Content-Type": "application/json",
          Accept: "application/json",
        },
        body: JSON.stringify({
          message: {
            subject,
            body: { contentType: content_type, content: body },
            toRecipients: recipients(to),
            ccRecipients: recipients(cc),
            bccRecipients: recipients(bcc),
            replyTo: recipients(reply_to),
            attachments: files,
          },
          saveToSentItems: save_to_sent_items,
        }),
      })
    
      if (!response.ok) {
        throw new Error(`${response.status} ${await response.text()}`)
      }
    
      // 202 Accepted with an empty body
      return { success: true }
    }
    

    Submitted by hugo989 6 hours ago

  • bun
    import * as wmill from "windmill-client"
    import { S3Object } from "windmill-client"
    
    /**
     * Send Email
     * Send an email from the connected mailbox, with optional cc/bcc, reply-to and file attachments from object storage (about 3 MB in total). Requires Mail.Send (the connection default); a shared mailbox also needs Mail.Send.Shared.
     */
    export async function main(
      auth: RT.Outlook,
      to: string[],
      subject: string,
      body: string,
      content_type: "HTML" | "Text" = "HTML",
      cc: string[] | undefined,
      bcc: string[] | undefined,
      reply_to: string[] | undefined,
      attachments: S3Object[] | undefined,
      save_to_sent_items: boolean = true,
      mailbox: string | undefined
    ) {
      const base = `https://graph.microsoft.com/v1.0/${
        mailbox != null && mailbox !== ""
          ? `users/${encodeURIComponent(mailbox)}`
          : "me"
      }`
      const recipients = (addresses: string[] | undefined) =>
        (addresses ?? [])
          .filter((a) => a != null && a.trim() !== "")
          .map((address) => ({ emailAddress: { address } }))
    
      const files = []
      for (const file of attachments ?? []) {
        const bytes = await wmill.loadS3File(file)
        if (!bytes) throw new Error(`Could not load ${file.s3}`)
        files.push({
          "@odata.type": "#microsoft.graph.fileAttachment",
          name: file.s3.split("/").pop(),
          contentBytes: Buffer.from(bytes).toString("base64"),
        })
      }
    
      const response = await fetch(`${base}/sendMail`, {
        method: "POST",
        headers: {
          Authorization: `Bearer ${auth.token}`,
          "Content-Type": "application/json",
          Accept: "application/json",
        },
        body: JSON.stringify({
          message: {
            subject,
            body: { contentType: content_type, content: body },
            toRecipients: recipients(to),
            ccRecipients: recipients(cc),
            bccRecipients: recipients(bcc),
            replyTo: recipients(reply_to),
            attachments: files,
          },
          saveToSentItems: save_to_sent_items,
        }),
      })
    
      if (!response.ok) {
        throw new Error(`${response.status} ${await response.text()}`)
      }
    
      // 202 Accepted with an empty body
      return { success: true }
    }
    

    Submitted by hugo989 6 hours ago