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
import { API_URL } from "https://esm.town/v/std/API_URL";
import { parseSendGridEmail } from "https://esm.town/v/stevekrouse/parseSendGridEmail?v=8";
import { parseSendGridEmails } from "https://esm.town/v/stevekrouse/parseSendGridEmails?v=10";
import process from "node:process";
export const email = async (data: {
to?: (IAddress | string)[] | IAddress | string;
from?: IAddress | string;
cc?: (IAddress | string)[] | IAddress | string;
bcc?: (IAddress | string)[] | IAddress | string;
subject?: string;
replyTo?: (IAddress | string)[] | IAddress | string;
html?: string;
text?: string;
attachments?: AttachmentData[];
}) => {
let result = await fetch(
`${API_URL}/v1/email`,
{
method: "POST",
body: JSON.stringify({
to: parseSendGridEmails(data.to),
from: data.from && parseSendGridEmail(data.from),
cc: parseSendGridEmails(data.cc),
bcc: parseSendGridEmails(data.bcc),
replyToList: parseSendGridEmails(data.replyTo),
subject: data.subject,
html: data.html,
text: data.text,
attachments: data.attachments,
}),
headers: {
authorization: "Bearer " + process.env.valtown,
},
},
).then(r => r.json());
if (result?.message !== "Email accepted to be sent") {
let message = result?.message ?? result?.error ?? JSON.stringify(result);
throw new Error("Val Town Email Error: " + message);
}
return result;
};
interface IAddress {
email: string;
name?: string;
}
interface AttachmentData {
content: string;
filename: string;
type?: string;
👆 This is a val. Vals are TypeScript snippets of code, written in the browser and run on our servers. Create scheduled functions, email yourself, and persist small pieces of data — all from the browser.
Comments
rodesp avatar

Is this still what we should be using to send emails?

I noticed the type says RPC (deprecated). That makes me think we should be using something else now but the docs still link here.

std avatar

Good catch! It should be a script type val. Fixed it!

rodesp avatar

Great, thank you!