-
Notifications
You must be signed in to change notification settings - Fork 0
/
smtp.ts
58 lines (52 loc) · 1.38 KB
/
smtp.ts
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
import {createTransport} from "../../deps.deno_ext.ts";
import {type DataMap} from "../pure/minipack.ts";
/**
* E-MAIL message.
*/
export interface MailMessage{
from: string;
to: string[];
title: string;
body: string;
cc?: string[];
bcc?: string[];
files?: DataMap[];
}
/**
* Send E-MAIL using SMTP.
* @see https://www.npmjs.com/package/nodemailer
* @example
* ```ts
* await smtpSend("smtp://smtp.example.com:25", {
* from: "mike@example.com",
* to: ["john@example.com"],
* title: "test",
* body: "test mail."
* });
* ```
*/
export async function smtpSend(path:string, message:MailMessage):Promise<void>{
const {protocol, hostname, port, username, password} = new URL(path);
if(protocol !== "smtp:" && protocol !== "smtps:"){
throw new Error(protocol);
}
const smtp = createTransport({
host: hostname,
port: Number(port),
secure: protocol === "smtps:",
auth: username ? {
user: decodeURIComponent(username),
pass: decodeURIComponent(password),
} : undefined
});
await smtp.sendMail({
from: message.from,
to: message.to,
cc: message.cc,
attachments: message.files?.map(({name, body}) => ({filename: name, content: body})),
bcc: message.bcc,
subject: message.title,
text: message.body
});
smtp.close();
}