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