|
| 1 | +import { |
| 2 | + Description, |
| 3 | + DisplayIcon, |
| 4 | + DisplayMessage, |
| 5 | + Documentation, |
| 6 | + FunctionContext, |
| 7 | + Identifier, |
| 8 | + Name, |
| 9 | + Parameter, |
| 10 | + RuntimeError, |
| 11 | + Signature, |
| 12 | +} from "@code0-tech/hercules"; |
| 13 | +import { sendEmail } from "../helpers.js"; |
| 14 | +import { SmtpSendResult } from "../data_types/smtpSendResult.js"; |
| 15 | +import { SmtpAttachment } from "../data_types/smtpAttachment.js"; |
| 16 | + |
| 17 | +@Identifier("sendEmailWithAttachments") |
| 18 | +@DisplayIcon("tabler:mail") |
| 19 | +@Signature("(To: string, Subject: string, Text: string, Attachments: SMTP_ATTACHMENT, Html?: string, From?: string, Cc?: string, Bcc?: string): SMTP_SEND_RESULT") |
| 20 | +@Name({ code: "en-US", content: "Send email with attachments" }) |
| 21 | +@DisplayMessage({ code: "en-US", content: "Send email with attachments to ${To}" }) |
| 22 | +@Documentation({ |
| 23 | + code: "en-US", |
| 24 | + content: |
| 25 | + "Sends an email with one or more attachments through the configured SMTP server.\nBuild each attachment with the `createAttachment` function. `To`, `Cc`, and `Bcc` accept comma-separated address lists.", |
| 26 | +}) |
| 27 | +@Description({ |
| 28 | + code: "en-US", |
| 29 | + content: "Sends an email with attachments through the configured SMTP server.", |
| 30 | +}) |
| 31 | +@Parameter({ |
| 32 | + runtimeName: "To", |
| 33 | + name: [{ code: "en-US", content: "To" }], |
| 34 | + description: [{ code: "en-US", content: "Comma-separated list of recipient email addresses." }], |
| 35 | +}) |
| 36 | +@Parameter({ |
| 37 | + runtimeName: "Subject", |
| 38 | + name: [{ code: "en-US", content: "Subject" }], |
| 39 | + description: [{ code: "en-US", content: "The subject line of the email." }], |
| 40 | +}) |
| 41 | +@Parameter({ |
| 42 | + runtimeName: "Text", |
| 43 | + name: [{ code: "en-US", content: "Text body" }], |
| 44 | + description: [{ code: "en-US", content: "The plain-text body of the email." }], |
| 45 | +}) |
| 46 | +@Parameter({ |
| 47 | + runtimeName: "Attachments", |
| 48 | + name: [{ code: "en-US", content: "Attachments" }], |
| 49 | + description: [{ code: "en-US", content: "One or more attachments built with the createAttachment function." }], |
| 50 | +}) |
| 51 | +@Parameter({ |
| 52 | + runtimeName: "Html", |
| 53 | + name: [{ code: "en-US", content: "HTML body" }], |
| 54 | + description: [{ code: "en-US", content: "Optional HTML body. When set it is sent alongside the plain-text body." }], |
| 55 | + optional: true, |
| 56 | +}) |
| 57 | +@Parameter({ |
| 58 | + runtimeName: "From", |
| 59 | + name: [{ code: "en-US", content: "From" }], |
| 60 | + description: [{ code: "en-US", content: "The sender address. Falls back to the configured default sender when omitted." }], |
| 61 | + optional: true, |
| 62 | +}) |
| 63 | +@Parameter({ |
| 64 | + runtimeName: "Cc", |
| 65 | + name: [{ code: "en-US", content: "CC" }], |
| 66 | + description: [{ code: "en-US", content: "Comma-separated list of CC recipients." }], |
| 67 | + optional: true, |
| 68 | +}) |
| 69 | +@Parameter({ |
| 70 | + runtimeName: "Bcc", |
| 71 | + name: [{ code: "en-US", content: "BCC" }], |
| 72 | + description: [{ code: "en-US", content: "Comma-separated list of BCC recipients." }], |
| 73 | + optional: true, |
| 74 | +}) |
| 75 | +export class SendEmailWithAttachmentsFunction { |
| 76 | + async run( |
| 77 | + context: FunctionContext, |
| 78 | + To: string, |
| 79 | + Subject: string, |
| 80 | + Text: string, |
| 81 | + Attachments: SmtpAttachment[], |
| 82 | + Html?: string, |
| 83 | + From?: string, |
| 84 | + Cc?: string, |
| 85 | + Bcc?: string |
| 86 | + ): Promise<SmtpSendResult> { |
| 87 | + try { |
| 88 | + return await sendEmail( |
| 89 | + { To, Subject, Text, Html, From, Cc, Bcc, Attachments: Attachments ?? [] }, |
| 90 | + context |
| 91 | + ); |
| 92 | + } catch (error) { |
| 93 | + if (error instanceof RuntimeError) { |
| 94 | + throw error; |
| 95 | + } |
| 96 | + if (error instanceof Error) { |
| 97 | + throw new RuntimeError("ERROR_SENDING_EMAIL", error.message); |
| 98 | + } |
| 99 | + throw new RuntimeError("ERROR_SENDING_EMAIL", "An error occurred while sending the email."); |
| 100 | + } |
| 101 | + } |
| 102 | +} |
0 commit comments