@sharadox/email-web-library 0.0.0-stage → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENCA.md +76 -0
- package/LICENSE.md +76 -0
- package/NOTICE.md +3 -0
- package/README.md +67 -2
- package/distribution/email-address/index.d.ts +4 -0
- package/distribution/email-address/index.d.ts.map +1 -0
- package/distribution/email-address/index.js +16 -0
- package/distribution/email-address/index.js.map +1 -0
- package/distribution/email-address/type.d.ts +6 -0
- package/distribution/email-address/type.d.ts.map +1 -0
- package/distribution/email-address/type.js +2 -0
- package/distribution/email-address/type.js.map +1 -0
- package/distribution/email-error/index.d.ts +9 -0
- package/distribution/email-error/index.d.ts.map +1 -0
- package/distribution/email-error/index.js +10 -0
- package/distribution/email-error/index.js.map +1 -0
- package/distribution/email-error/type.d.ts +8 -0
- package/distribution/email-error/type.d.ts.map +1 -0
- package/distribution/email-error/type.js +2 -0
- package/distribution/email-error/type.js.map +1 -0
- package/distribution/email-message/index.d.ts +11 -0
- package/distribution/email-message/index.d.ts.map +1 -0
- package/distribution/email-message/index.js +28 -0
- package/distribution/email-message/index.js.map +1 -0
- package/distribution/email-message/type.d.ts +14 -0
- package/distribution/email-message/type.d.ts.map +1 -0
- package/distribution/email-message/type.js +2 -0
- package/distribution/email-message/type.js.map +1 -0
- package/distribution/email-sender/index.d.ts +11 -0
- package/distribution/email-sender/index.d.ts.map +1 -0
- package/distribution/email-sender/index.js +75 -0
- package/distribution/email-sender/index.js.map +1 -0
- package/distribution/email-sender/type.d.ts +18 -0
- package/distribution/email-sender/type.d.ts.map +1 -0
- package/distribution/email-sender/type.js +2 -0
- package/distribution/email-sender/type.js.map +1 -0
- package/distribution/email-server-configuration/index.d.ts +9 -0
- package/distribution/email-server-configuration/index.d.ts.map +1 -0
- package/distribution/email-server-configuration/index.js +35 -0
- package/distribution/email-server-configuration/index.js.map +1 -0
- package/distribution/email-server-configuration/type.d.ts +19 -0
- package/distribution/email-server-configuration/type.d.ts.map +1 -0
- package/distribution/email-server-configuration/type.js +2 -0
- package/distribution/email-server-configuration/type.js.map +1 -0
- package/distribution/index.d.ts +23 -0
- package/distribution/index.d.ts.map +1 -0
- package/distribution/index.js +18 -0
- package/distribution/index.js.map +1 -0
- package/package.json +76 -3
package/LICENCA.md
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
# Licença Sharadox de Código Aberto
|
|
2
|
+
|
|
3
|
+
Versão 0.1.0
|
|
4
|
+
|
|
5
|
+
O texto em língua portuguesa é o único texto original e vinculativo desta licença. A tradução para inglês (`LICENSE.md`) é meramente informativa; em caso de divergência prevalece o texto em português.
|
|
6
|
+
|
|
7
|
+
## 1. Definições
|
|
8
|
+
|
|
9
|
+
Para efeitos desta licença:
|
|
10
|
+
|
|
11
|
+
1. **«Obra»** — o programa de computador, a sua documentação e quaisquer outros conteúdos disponibilizados pelo Licenciante ao abrigo desta licença, em código-fonte ou em qualquer outra forma.
|
|
12
|
+
2. **«Licenciante»** — o titular dos direitos de autor identificado no aviso de direitos de autor que acompanha a Obra.
|
|
13
|
+
3. **«Licenciado»** — qualquer pessoa, singular ou coletiva, que exerça os direitos concedidos por esta licença.
|
|
14
|
+
4. **«Obra Derivada»** — qualquer obra baseada na Obra ou que a incorpore, total ou parcialmente, com ou sem modificações, incluindo traduções, adaptações e programas que a integrem.
|
|
15
|
+
5. **«Aviso»** — o aviso de direitos de autor do Licenciante e a indicação de que a Obra está licenciada ao abrigo desta licença, incluindo a respetiva versão.
|
|
16
|
+
|
|
17
|
+
## 2. Concessão de direitos
|
|
18
|
+
|
|
19
|
+
1. Nos termos desta licença, o Licenciante concede ao Licenciado uma licença mundial, gratuita, não exclusiva e por todo o prazo de proteção dos direitos de autor para:
|
|
20
|
+
1. utilizar a Obra para qualquer finalidade, incluindo finalidades comerciais;
|
|
21
|
+
2. reproduzir a Obra, total ou parcialmente, por qualquer meio e sob qualquer forma;
|
|
22
|
+
3. modificar, traduzir, adaptar e transformar a Obra e criar Obras Derivadas;
|
|
23
|
+
4. distribuir, publicar, comunicar ao público, colocar à disposição, vender e sublicenciar a Obra e as Obras Derivadas.
|
|
24
|
+
2. Na medida em que o Licenciante seja titular de patentes necessariamente infringidas pela Obra tal como por si disponibilizada, concede igualmente ao Licenciado, nos mesmos termos, uma licença para utilizar essas patentes no exercício dos direitos previstos no número anterior.
|
|
25
|
+
3. Esta licença não transmite a titularidade de quaisquer direitos, que permanecem do Licenciante.
|
|
26
|
+
|
|
27
|
+
## 3. Condições
|
|
28
|
+
|
|
29
|
+
O exercício dos direitos concedidos fica sujeito às seguintes condições:
|
|
30
|
+
|
|
31
|
+
1. Todas as cópias da Obra, ou de partes substanciais da mesma, e todas as Obras Derivadas distribuídas devem incluir o Aviso e uma cópia desta licença ou uma ligação estável para o seu texto.
|
|
32
|
+
2. Quem distribuir uma versão modificada da Obra deve indicar de forma visível que a mesma foi modificada, não podendo apresentá-la como versão original do Licenciante.
|
|
33
|
+
3. O Licenciado pode licenciar as suas próprias modificações ao abrigo de outros termos, desde que a parte da Obra que delas faça parte continue sujeita às condições desta licença.
|
|
34
|
+
|
|
35
|
+
## 4. Nomes e marcas
|
|
36
|
+
|
|
37
|
+
Esta licença não concede qualquer direito sobre o nome, as marcas, os logótipos ou outros sinais distintivos do Licenciante, incluindo a designação «Sharadox», salvo na medida estritamente necessária para cumprir o dever de incluir o Aviso. É proibido utilizá-los para promover ou sugerir o apoio do Licenciante a Obras Derivadas ou a quaisquer produtos ou serviços sem autorização prévia e escrita.
|
|
38
|
+
|
|
39
|
+
## 5. Exclusão de garantias
|
|
40
|
+
|
|
41
|
+
A Obra é disponibilizada «tal como está» e «conforme disponível», sem qualquer garantia, expressa ou tácita, incluindo, designadamente, garantias de comerciabilidade, de adequação a um fim específico, de ausência de erros ou de não violação de direitos de terceiros, na medida máxima permitida pela lei aplicável.
|
|
42
|
+
|
|
43
|
+
## 6. Limitação de responsabilidade
|
|
44
|
+
|
|
45
|
+
1. Na medida máxima permitida pela lei aplicável, o Licenciante não é responsável por quaisquer danos, diretos ou indiretos, incluindo lucros cessantes, perda de dados ou interrupção de atividade, resultantes da utilização ou da impossibilidade de utilização da Obra, ainda que tenha sido avisado da possibilidade da sua ocorrência.
|
|
46
|
+
2. Nada nesta licença exclui ou limita a responsabilidade do Licenciante nos casos em que tal exclusão ou limitação não seja permitida por lei, designadamente por atos praticados com dolo ou culpa grave, nos termos do artigo 809.º e do n.º 2 do artigo 800.º do Código Civil, nem afeta os direitos que a lei imperativamente reconheça aos consumidores.
|
|
47
|
+
|
|
48
|
+
## 7. Cessação
|
|
49
|
+
|
|
50
|
+
1. Os direitos concedidos por esta licença cessam automaticamente se o Licenciado violar qualquer das suas condições.
|
|
51
|
+
2. Os direitos são repostos se a violação for sanada no prazo de 30 dias a contar do momento em que o Licenciado dela tome conhecimento, salvo se o Licenciante tiver entretanto comunicado por escrito a sua oposição.
|
|
52
|
+
3. A cessação não afeta as licenças validamente concedidas pelo Licenciado a terceiros que cumpram esta licença.
|
|
53
|
+
|
|
54
|
+
## 8. Lei aplicável e foro
|
|
55
|
+
|
|
56
|
+
1. Esta licença rege-se pela lei portuguesa, designadamente pelo Código do Direito de Autor e dos Direitos Conexos, aprovado pelo Decreto-Lei n.º 63/85, de 14 de março, e pelo Decreto-Lei n.º 252/94, de 20 de outubro, relativo à proteção jurídica dos programas de computador.
|
|
57
|
+
2. Para a resolução de quaisquer litígios emergentes desta licença é competente o Tribunal Judicial da Comarca de Faro, sem prejuízo das normas imperativas de competência aplicáveis, nomeadamente as que protegem os consumidores.
|
|
58
|
+
|
|
59
|
+
## 9. Versões da licença
|
|
60
|
+
|
|
61
|
+
O Licenciante pode publicar novas versões desta licença, identificadas por um número de versão. A Obra fica sujeita à versão indicada no Aviso. O Licenciado pode, se o Aviso o permitir, optar por qualquer versão posterior.
|
|
62
|
+
|
|
63
|
+
## 10. Disposições finais
|
|
64
|
+
|
|
65
|
+
1. Se alguma disposição desta licença for considerada inválida ou ineficaz, as restantes mantêm-se em vigor, devendo a disposição afetada ser interpretada de forma a aproximar-se, tanto quanto possível, da intenção original.
|
|
66
|
+
2. A não exigência, em qualquer momento, do cumprimento de uma disposição desta licença não constitui renúncia ao direito de o exigir posteriormente.
|
|
67
|
+
|
|
68
|
+
## Como aplicar esta licença
|
|
69
|
+
|
|
70
|
+
Inclua na Obra um ficheiro `LICENCA.md` com este texto, a tradução `LICENSE.md` e o seguinte aviso, preenchendo o ano e o titular:
|
|
71
|
+
|
|
72
|
+
```
|
|
73
|
+
Direitos de autor © <ano> <titular>.
|
|
74
|
+
Licenciado ao abrigo da Licença Sharadox de Código Aberto, versão 0.1.0.
|
|
75
|
+
https://github.com/Sharadox/sharadox-open-source-license
|
|
76
|
+
```
|
package/LICENSE.md
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
# Sharadox Open Source Licence
|
|
2
|
+
|
|
3
|
+
Version 0.1.0
|
|
4
|
+
|
|
5
|
+
This is an informative English translation. The Portuguese text (`LICENCA.md`) is the only original and binding text of this licence; if the two differ, the Portuguese text prevails.
|
|
6
|
+
|
|
7
|
+
## 1. Definitions
|
|
8
|
+
|
|
9
|
+
For the purposes of this licence:
|
|
10
|
+
|
|
11
|
+
1. **"Work"** — the computer program, its documentation and any other content made available by the Licensor under this licence, in source code or in any other form.
|
|
12
|
+
2. **"Licensor"** — the copyright holder identified in the copyright notice that accompanies the Work.
|
|
13
|
+
3. **"Licensee"** — any natural or legal person exercising the rights granted by this licence.
|
|
14
|
+
4. **"Derivative Work"** — any work based on or incorporating the Work, in whole or in part, with or without modifications, including translations, adaptations and programs that integrate it.
|
|
15
|
+
5. **"Notice"** — the Licensor's copyright notice and the statement that the Work is licensed under this licence, including its version.
|
|
16
|
+
|
|
17
|
+
## 2. Grant of rights
|
|
18
|
+
|
|
19
|
+
1. Under this licence, the Licensor grants the Licensee a worldwide, royalty-free, non-exclusive licence, for the whole term of copyright protection, to:
|
|
20
|
+
1. use the Work for any purpose, including commercial purposes;
|
|
21
|
+
2. reproduce the Work, in whole or in part, by any means and in any form;
|
|
22
|
+
3. modify, translate, adapt and transform the Work and create Derivative Works;
|
|
23
|
+
4. distribute, publish, communicate to the public, make available, sell and sublicense the Work and Derivative Works.
|
|
24
|
+
2. To the extent the Licensor holds patents necessarily infringed by the Work as made available by it, the Licensor also grants the Licensee, on the same terms, a licence to use those patents in exercising the rights in the previous paragraph.
|
|
25
|
+
3. This licence does not transfer ownership of any rights, which remain with the Licensor.
|
|
26
|
+
|
|
27
|
+
## 3. Conditions
|
|
28
|
+
|
|
29
|
+
The exercise of the rights granted is subject to the following conditions:
|
|
30
|
+
|
|
31
|
+
1. Every copy of the Work, or of substantial parts of it, and every distributed Derivative Work must include the Notice and a copy of this licence or a stable link to its text.
|
|
32
|
+
2. Anyone distributing a modified version of the Work must state visibly that it has been modified and may not present it as the Licensor's original version.
|
|
33
|
+
3. The Licensee may license their own modifications under other terms, provided the part of the Work included in them remains subject to the conditions of this licence.
|
|
34
|
+
|
|
35
|
+
## 4. Names and trademarks
|
|
36
|
+
|
|
37
|
+
This licence grants no rights to the Licensor's name, trademarks, logos or other distinctive signs, including the name "Sharadox", except as strictly necessary to comply with the duty to include the Notice. Using them to promote or suggest the Licensor's endorsement of Derivative Works or of any products or services without prior written permission is prohibited.
|
|
38
|
+
|
|
39
|
+
## 5. Disclaimer of warranties
|
|
40
|
+
|
|
41
|
+
The Work is provided "as is" and "as available", without any warranty, express or implied, including in particular warranties of merchantability, fitness for a particular purpose, absence of errors or non-infringement of third-party rights, to the maximum extent permitted by applicable law.
|
|
42
|
+
|
|
43
|
+
## 6. Limitation of liability
|
|
44
|
+
|
|
45
|
+
1. To the maximum extent permitted by applicable law, the Licensor is not liable for any damages, direct or indirect, including loss of profits, loss of data or business interruption, arising from the use of or inability to use the Work, even if advised of the possibility of such damages.
|
|
46
|
+
2. Nothing in this licence excludes or limits the Licensor's liability where such exclusion or limitation is not permitted by law, in particular for acts committed with intent or gross negligence, under article 809 and article 800(2) of the Portuguese Civil Code, nor does it affect rights that the law mandatorily grants to consumers.
|
|
47
|
+
|
|
48
|
+
## 7. Termination
|
|
49
|
+
|
|
50
|
+
1. The rights granted by this licence terminate automatically if the Licensee breaches any of its conditions.
|
|
51
|
+
2. The rights are reinstated if the breach is remedied within 30 days of the Licensee becoming aware of it, unless the Licensor has meanwhile objected in writing.
|
|
52
|
+
3. Termination does not affect licences validly granted by the Licensee to third parties who comply with this licence.
|
|
53
|
+
|
|
54
|
+
## 8. Governing law and jurisdiction
|
|
55
|
+
|
|
56
|
+
1. This licence is governed by Portuguese law, in particular the Code of Copyright and Related Rights, approved by Decree-Law no. 63/85 of 14 March, and Decree-Law no. 252/94 of 20 October on the legal protection of computer programs.
|
|
57
|
+
2. The Judicial Court of the District of Faro has jurisdiction over any dispute arising from this licence, without prejudice to applicable mandatory rules of jurisdiction, in particular those protecting consumers.
|
|
58
|
+
|
|
59
|
+
## 9. Licence versions
|
|
60
|
+
|
|
61
|
+
The Licensor may publish new versions of this licence, identified by a version number. The Work is subject to the version stated in the Notice. If the Notice allows it, the Licensee may choose any later version.
|
|
62
|
+
|
|
63
|
+
## 10. Final provisions
|
|
64
|
+
|
|
65
|
+
1. If any provision of this licence is held invalid or unenforceable, the remaining provisions stay in force, and the affected provision shall be interpreted as closely as possible to its original intent.
|
|
66
|
+
2. Failure to enforce any provision of this licence at any time does not waive the right to enforce it later.
|
|
67
|
+
|
|
68
|
+
## How to apply this licence
|
|
69
|
+
|
|
70
|
+
Include in the Work a `LICENCA.md` file with the Portuguese text, this translation as `LICENSE.md`, and the following notice, filling in the year and holder:
|
|
71
|
+
|
|
72
|
+
```
|
|
73
|
+
Direitos de autor © <ano> <titular>.
|
|
74
|
+
Licenciado ao abrigo da Licença Sharadox de Código Aberto, versão 0.1.0.
|
|
75
|
+
https://github.com/Sharadox/sharadox-open-source-license
|
|
76
|
+
```
|
package/NOTICE.md
ADDED
package/README.md
CHANGED
|
@@ -1,3 +1,68 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @sharadox/email-web-library
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Send email from web applications — contact forms, notifications, receipts — through **any SMTP server**: your existing mailbox provider (Proton, Google Workspace, Microsoft 365, Fastmail…) or a sending service (Amazon SES, Postmark, Resend…). No provider SDK, no cloud account; switching provider is a change of four environment variables.
|
|
4
|
+
|
|
5
|
+
- Checks every message before it leaves: valid addresses, a one-line subject, a body — so form input can't inject extra headers or recipients.
|
|
6
|
+
- One error type, `EmailError`, whose `code` says whose problem it is: `configuration`, `invalid-message` or `delivery`.
|
|
7
|
+
- One short connection per message, which suits serverless functions (Next.js server actions, Vercel functions).
|
|
8
|
+
- Typed, tested against a real SMTP server, ESM only. The SMTP protocol work is done by [Nodemailer](https://nodemailer.com).
|
|
9
|
+
|
|
10
|
+
## Install
|
|
11
|
+
|
|
12
|
+
```sh
|
|
13
|
+
pnpm add @sharadox/email-web-library
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
## Use
|
|
17
|
+
|
|
18
|
+
Set the server in the environment:
|
|
19
|
+
|
|
20
|
+
| Variable | Example | Notes |
|
|
21
|
+
| ----------------------- | ------------------------- | ----------------------------------------------------------------------------------------------- |
|
|
22
|
+
| `EMAIL_SERVER_HOST` | `smtp.protonmail.ch` | |
|
|
23
|
+
| `EMAIL_SERVER_PORT` | `587` | Optional, 587 by default |
|
|
24
|
+
| `EMAIL_SERVER_USERNAME` | `website@example.com` | |
|
|
25
|
+
| `EMAIL_SERVER_PASSWORD` | an SMTP token or password | Keep it in your host's secret store, never in the repository |
|
|
26
|
+
| `EMAIL_SERVER_SECURITY` | `starttls` | Optional: `starttls` (default), `tls` (default on port 465) or `none` (local test servers only) |
|
|
27
|
+
|
|
28
|
+
```ts
|
|
29
|
+
"use server";
|
|
30
|
+
import { createEmailSender, EmailError, readEmailServerConfiguration } from "@sharadox/email-web-library";
|
|
31
|
+
|
|
32
|
+
export async function sendContact(name: string, email: string, text: string) {
|
|
33
|
+
try {
|
|
34
|
+
const sender = createEmailSender(readEmailServerConfiguration());
|
|
35
|
+
await sender.send({
|
|
36
|
+
from: { name: "Website", address: "website@example.com" }, // usually must be the login address or an alias
|
|
37
|
+
to: "inbox@example.com",
|
|
38
|
+
replyTo: { name, address: email }, // replying goes straight to the visitor
|
|
39
|
+
subject: `New message from ${name}`,
|
|
40
|
+
text,
|
|
41
|
+
});
|
|
42
|
+
return { sent: true };
|
|
43
|
+
} catch (error) {
|
|
44
|
+
if (error instanceof EmailError && error.code === "invalid-message")
|
|
45
|
+
return { sent: false, reason: "check-the-form" };
|
|
46
|
+
console.error(error); // configuration or delivery: tell the visitor to write directly
|
|
47
|
+
return { sent: false, reason: "unavailable" };
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
`sender.verify()` logs in without sending, handy for a health check. `checkEmailMessage` and `checkEmailAddress` are exported for validating input before you get as far as sending.
|
|
53
|
+
|
|
54
|
+
## Development
|
|
55
|
+
|
|
56
|
+
Built on the Sharadox engineering standard: TypeScript in `source/` (one thing per folder: `index.ts`, `type.ts`, `unit-test.ts`, `benchmark-test.ts`), Turborepo, ESLint, Prettier, Vitest, TypeDoc, Husky.
|
|
57
|
+
|
|
58
|
+
```sh
|
|
59
|
+
pnpm install
|
|
60
|
+
pnpm verify # format, lint, types, unit and benchmark tests, build
|
|
61
|
+
pnpm documentation # API reference in documentation/
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
Branches: `index` is released, `preview` gathers finished work, and each change lives on `<feature>.preview`. Releases are a `v<version>` tag on `index`; the publish workflow releases to npm with provenance, using the organisation's `NPM_TOKEN`.
|
|
65
|
+
|
|
66
|
+
## License
|
|
67
|
+
|
|
68
|
+
Licença Sharadox de Código Aberto, versão 0.1.0 ([português](LICENCA.md), the binding text · [English translation](LICENSE.md)). Versions up to 0.1.0 were published under the MIT licence.
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { EmailAddress } from "./type.ts";
|
|
2
|
+
/** Checks an address (and its display name) and returns it unchanged; throws `invalid-message` otherwise. */
|
|
3
|
+
export declare function checkEmailAddress(value: EmailAddress, field: string): EmailAddress;
|
|
4
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../source/email-address/index.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,WAAW,CAAC;AAM9C,6GAA6G;AAC7G,wBAAgB,iBAAiB,CAAC,KAAK,EAAE,YAAY,EAAE,KAAK,EAAE,MAAM,GAAG,YAAY,CASlF"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { EmailError } from "../email-error/index.js";
|
|
2
|
+
/** One address: a local part, an @, and a domain with at least one dot; no spaces, angle brackets or line breaks. */
|
|
3
|
+
const ADDRESS = /^[^\s@<>()[\]\\,;:"]+@[^\s@<>()[\]\\,;:"]+\.[^\s@<>()[\]\\,;:"]+$/;
|
|
4
|
+
const LINE_BREAK = /[\r\n]/;
|
|
5
|
+
/** Checks an address (and its display name) and returns it unchanged; throws `invalid-message` otherwise. */
|
|
6
|
+
export function checkEmailAddress(value, field) {
|
|
7
|
+
const address = typeof value === "string" ? value : value.address;
|
|
8
|
+
if (address.length > 254 || !ADDRESS.test(address)) {
|
|
9
|
+
throw new EmailError("invalid-message", `${field} is not a valid email address`);
|
|
10
|
+
}
|
|
11
|
+
if (typeof value !== "string" && (LINE_BREAK.test(value.name) || value.name.length > 200)) {
|
|
12
|
+
throw new EmailError("invalid-message", `${field} has an invalid display name`);
|
|
13
|
+
}
|
|
14
|
+
return value;
|
|
15
|
+
}
|
|
16
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../source/email-address/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,yBAAyB,CAAC;AAGrD,qHAAqH;AACrH,MAAM,OAAO,GAAG,mEAAmE,CAAC;AACpF,MAAM,UAAU,GAAG,QAAQ,CAAC;AAE5B,6GAA6G;AAC7G,MAAM,UAAU,iBAAiB,CAAC,KAAmB,EAAE,KAAa;IAClE,MAAM,OAAO,GAAG,OAAO,KAAK,KAAK,QAAQ,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC;IAClE,IAAI,OAAO,CAAC,MAAM,GAAG,GAAG,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,OAAO,CAAC,EAAE,CAAC;QACnD,MAAM,IAAI,UAAU,CAAC,iBAAiB,EAAE,GAAG,KAAK,+BAA+B,CAAC,CAAC;IACnF,CAAC;IACD,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,CAAC,UAAU,CAAC,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,IAAI,KAAK,CAAC,IAAI,CAAC,MAAM,GAAG,GAAG,CAAC,EAAE,CAAC;QAC1F,MAAM,IAAI,UAAU,CAAC,iBAAiB,EAAE,GAAG,KAAK,8BAA8B,CAAC,CAAC;IAClF,CAAC;IACD,OAAO,KAAK,CAAC;AACf,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"type.d.ts","sourceRoot":"","sources":["../../source/email-address/type.ts"],"names":[],"mappings":"AAAA,0EAA0E;AAC1E,MAAM,MAAM,YAAY,GAAG,MAAM,GAAG;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,OAAO,EAAE,MAAM,CAAA;CAAE,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"type.js","sourceRoot":"","sources":["../../source/email-address/type.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { EmailErrorCode } from "./type.ts";
|
|
2
|
+
/** The only error this library throws; `code` says whose problem it is. */
|
|
3
|
+
export declare class EmailError extends Error {
|
|
4
|
+
readonly code: EmailErrorCode;
|
|
5
|
+
constructor(code: EmailErrorCode, message: string, options?: {
|
|
6
|
+
cause?: unknown;
|
|
7
|
+
});
|
|
8
|
+
}
|
|
9
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../source/email-error/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,WAAW,CAAC;AAEhD,2EAA2E;AAC3E,qBAAa,UAAW,SAAQ,KAAK;IACnC,QAAQ,CAAC,IAAI,EAAE,cAAc,CAAC;gBAElB,IAAI,EAAE,cAAc,EAAE,OAAO,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE;QAAE,KAAK,CAAC,EAAE,OAAO,CAAA;KAAE;CAKjF"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/** The only error this library throws; `code` says whose problem it is. */
|
|
2
|
+
export class EmailError extends Error {
|
|
3
|
+
code;
|
|
4
|
+
constructor(code, message, options) {
|
|
5
|
+
super(message, options);
|
|
6
|
+
this.name = "EmailError";
|
|
7
|
+
this.code = code;
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../source/email-error/index.ts"],"names":[],"mappings":"AAEA,2EAA2E;AAC3E,MAAM,OAAO,UAAW,SAAQ,KAAK;IAC1B,IAAI,CAAiB;IAE9B,YAAY,IAAoB,EAAE,OAAe,EAAE,OAA6B;QAC9E,KAAK,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;QACxB,IAAI,CAAC,IAAI,GAAG,YAAY,CAAC;QACzB,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;IACnB,CAAC;CACF"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Why sending failed:
|
|
3
|
+
* - `configuration`: the email server settings are missing or invalid (fix the environment).
|
|
4
|
+
* - `invalid-message`: the message itself is wrong, for example a malformed address (fix the input).
|
|
5
|
+
* - `delivery`: the server could not be reached, refused the login or rejected the message (retry or alert).
|
|
6
|
+
*/
|
|
7
|
+
export type EmailErrorCode = "configuration" | "invalid-message" | "delivery";
|
|
8
|
+
//# sourceMappingURL=type.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"type.d.ts","sourceRoot":"","sources":["../../source/email-error/type.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,MAAM,MAAM,cAAc,GAAG,eAAe,GAAG,iBAAiB,GAAG,UAAU,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"type.js","sourceRoot":"","sources":["../../source/email-error/type.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { EmailMessage } from "./type.ts";
|
|
2
|
+
/** Longest subject accepted, in characters. */
|
|
3
|
+
export declare const SUBJECT_LIMIT = 300;
|
|
4
|
+
/** Most recipients accepted in one message. */
|
|
5
|
+
export declare const RECIPIENT_LIMIT = 50;
|
|
6
|
+
/**
|
|
7
|
+
* Checks a message before it reaches the server: valid addresses, a one-line subject (no header injection), at
|
|
8
|
+
* least one recipient and a body. Returns the message unchanged; throws `invalid-message` otherwise.
|
|
9
|
+
*/
|
|
10
|
+
export declare function checkEmailMessage(message: EmailMessage): EmailMessage;
|
|
11
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../source/email-message/index.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,WAAW,CAAC;AAE9C,+CAA+C;AAC/C,eAAO,MAAM,aAAa,MAAM,CAAC;AACjC,+CAA+C;AAC/C,eAAO,MAAM,eAAe,KAAK,CAAC;AAElC;;;GAGG;AACH,wBAAgB,iBAAiB,CAAC,OAAO,EAAE,YAAY,GAAG,YAAY,CAerE"}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { checkEmailAddress } from "../email-address/index.js";
|
|
2
|
+
import { EmailError } from "../email-error/index.js";
|
|
3
|
+
/** Longest subject accepted, in characters. */
|
|
4
|
+
export const SUBJECT_LIMIT = 300;
|
|
5
|
+
/** Most recipients accepted in one message. */
|
|
6
|
+
export const RECIPIENT_LIMIT = 50;
|
|
7
|
+
/**
|
|
8
|
+
* Checks a message before it reaches the server: valid addresses, a one-line subject (no header injection), at
|
|
9
|
+
* least one recipient and a body. Returns the message unchanged; throws `invalid-message` otherwise.
|
|
10
|
+
*/
|
|
11
|
+
export function checkEmailMessage(message) {
|
|
12
|
+
checkEmailAddress(message.from, "from");
|
|
13
|
+
const recipients = Array.isArray(message.to) ? message.to : [message.to];
|
|
14
|
+
if (recipients.length === 0 || recipients.length > RECIPIENT_LIMIT) {
|
|
15
|
+
throw new EmailError("invalid-message", `to needs between 1 and ${RECIPIENT_LIMIT} recipients`);
|
|
16
|
+
}
|
|
17
|
+
recipients.forEach((recipient, index) => checkEmailAddress(recipient, `to[${index}]`));
|
|
18
|
+
if (message.replyTo !== undefined)
|
|
19
|
+
checkEmailAddress(message.replyTo, "replyTo");
|
|
20
|
+
if (!message.subject.trim() || /[\r\n]/.test(message.subject) || message.subject.length > SUBJECT_LIMIT) {
|
|
21
|
+
throw new EmailError("invalid-message", `subject must be one line of 1 to ${SUBJECT_LIMIT} characters`);
|
|
22
|
+
}
|
|
23
|
+
if (!message.text?.trim() && !message.html?.trim()) {
|
|
24
|
+
throw new EmailError("invalid-message", "the message needs a text or html body");
|
|
25
|
+
}
|
|
26
|
+
return message;
|
|
27
|
+
}
|
|
28
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../source/email-message/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,2BAA2B,CAAC;AAC9D,OAAO,EAAE,UAAU,EAAE,MAAM,yBAAyB,CAAC;AAGrD,+CAA+C;AAC/C,MAAM,CAAC,MAAM,aAAa,GAAG,GAAG,CAAC;AACjC,+CAA+C;AAC/C,MAAM,CAAC,MAAM,eAAe,GAAG,EAAE,CAAC;AAElC;;;GAGG;AACH,MAAM,UAAU,iBAAiB,CAAC,OAAqB;IACrD,iBAAiB,CAAC,OAAO,CAAC,IAAI,EAAE,MAAM,CAAC,CAAC;IACxC,MAAM,UAAU,GAAG,KAAK,CAAC,OAAO,CAAC,OAAO,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,EAAE,CAAC,CAAC;IACzE,IAAI,UAAU,CAAC,MAAM,KAAK,CAAC,IAAI,UAAU,CAAC,MAAM,GAAG,eAAe,EAAE,CAAC;QACnE,MAAM,IAAI,UAAU,CAAC,iBAAiB,EAAE,0BAA0B,eAAe,aAAa,CAAC,CAAC;IAClG,CAAC;IACD,UAAU,CAAC,OAAO,CAAC,CAAC,SAAS,EAAE,KAAK,EAAE,EAAE,CAAC,iBAAiB,CAAC,SAAS,EAAE,MAAM,KAAK,GAAG,CAAC,CAAC,CAAC;IACvF,IAAI,OAAO,CAAC,OAAO,KAAK,SAAS;QAAE,iBAAiB,CAAC,OAAO,CAAC,OAAO,EAAE,SAAS,CAAC,CAAC;IACjF,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC,IAAI,EAAE,IAAI,QAAQ,CAAC,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC,IAAI,OAAO,CAAC,OAAO,CAAC,MAAM,GAAG,aAAa,EAAE,CAAC;QACxG,MAAM,IAAI,UAAU,CAAC,iBAAiB,EAAE,oCAAoC,aAAa,aAAa,CAAC,CAAC;IAC1G,CAAC;IACD,IAAI,CAAC,OAAO,CAAC,IAAI,EAAE,IAAI,EAAE,IAAI,CAAC,OAAO,CAAC,IAAI,EAAE,IAAI,EAAE,EAAE,CAAC;QACnD,MAAM,IAAI,UAAU,CAAC,iBAAiB,EAAE,uCAAuC,CAAC,CAAC;IACnF,CAAC;IACD,OAAO,OAAO,CAAC;AACjB,CAAC"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { EmailAddress } from "../email-address/type.ts";
|
|
2
|
+
/** An email to send. Give `text`, `html` or both (text is the fallback for clients that don't show html). */
|
|
3
|
+
export interface EmailMessage {
|
|
4
|
+
/** Sender. Most servers only accept the address you log in with (or an alias of it). */
|
|
5
|
+
from: EmailAddress;
|
|
6
|
+
/** One or more recipients. */
|
|
7
|
+
to: EmailAddress | EmailAddress[];
|
|
8
|
+
/** Where replies go, for example the visitor who filled a contact form. */
|
|
9
|
+
replyTo?: EmailAddress;
|
|
10
|
+
subject: string;
|
|
11
|
+
text?: string;
|
|
12
|
+
html?: string;
|
|
13
|
+
}
|
|
14
|
+
//# sourceMappingURL=type.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"type.d.ts","sourceRoot":"","sources":["../../source/email-message/type.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,0BAA0B,CAAC;AAE7D,6GAA6G;AAC7G,MAAM,WAAW,YAAY;IAC3B,wFAAwF;IACxF,IAAI,EAAE,YAAY,CAAC;IACnB,8BAA8B;IAC9B,EAAE,EAAE,YAAY,GAAG,YAAY,EAAE,CAAC;IAClC,2EAA2E;IAC3E,OAAO,CAAC,EAAE,YAAY,CAAC;IACvB,OAAO,EAAE,MAAM,CAAC;IAChB,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,IAAI,CAAC,EAAE,MAAM,CAAC;CACf"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"type.js","sourceRoot":"","sources":["../../source/email-message/type.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { EmailServerConfiguration } from "../email-server-configuration/type.ts";
|
|
2
|
+
import type { EmailSender } from "./type.ts";
|
|
3
|
+
/**
|
|
4
|
+
* Creates a sender for one SMTP server. Each `send` opens its own short connection, which suits serverless
|
|
5
|
+
* functions (nothing to close, nothing kept between invocations).
|
|
6
|
+
*
|
|
7
|
+
* The SMTP protocol work (STARTTLS, login, MIME, encoding of non-English text) is done by Nodemailer; this library
|
|
8
|
+
* adds the configuration, the message checks and one error type.
|
|
9
|
+
*/
|
|
10
|
+
export declare function createEmailSender(configuration: EmailServerConfiguration): EmailSender;
|
|
11
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../source/email-sender/index.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,wBAAwB,EAAE,MAAM,uCAAuC,CAAC;AACtF,OAAO,KAAK,EAAgB,WAAW,EAAE,MAAM,WAAW,CAAC;AAI3D;;;;;;GAMG;AACH,wBAAgB,iBAAiB,CAAC,aAAa,EAAE,wBAAwB,GAAG,WAAW,CAuDtF"}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { createTransport } from "nodemailer";
|
|
2
|
+
import { EmailError } from "../email-error/index.js";
|
|
3
|
+
import { checkEmailMessage } from "../email-message/index.js";
|
|
4
|
+
const addressText = (entry) => (typeof entry === "string" ? entry : entry.address);
|
|
5
|
+
/**
|
|
6
|
+
* Creates a sender for one SMTP server. Each `send` opens its own short connection, which suits serverless
|
|
7
|
+
* functions (nothing to close, nothing kept between invocations).
|
|
8
|
+
*
|
|
9
|
+
* The SMTP protocol work (STARTTLS, login, MIME, encoding of non-English text) is done by Nodemailer; this library
|
|
10
|
+
* adds the configuration, the message checks and one error type.
|
|
11
|
+
*/
|
|
12
|
+
export function createEmailSender(configuration) {
|
|
13
|
+
const security = configuration.security ?? (configuration.port === 465 ? "tls" : "starttls");
|
|
14
|
+
const timeout = configuration.timeout ?? 10_000;
|
|
15
|
+
const transport = createTransport({
|
|
16
|
+
host: configuration.host,
|
|
17
|
+
port: configuration.port,
|
|
18
|
+
secure: security === "tls",
|
|
19
|
+
requireTLS: security === "starttls",
|
|
20
|
+
ignoreTLS: security === "none",
|
|
21
|
+
auth: { user: configuration.username, pass: configuration.password },
|
|
22
|
+
connectionTimeout: timeout,
|
|
23
|
+
greetingTimeout: timeout,
|
|
24
|
+
socketTimeout: timeout * 2,
|
|
25
|
+
});
|
|
26
|
+
return {
|
|
27
|
+
async send(message) {
|
|
28
|
+
checkEmailMessage(message);
|
|
29
|
+
try {
|
|
30
|
+
const info = await transport.sendMail({
|
|
31
|
+
from: message.from,
|
|
32
|
+
to: message.to,
|
|
33
|
+
replyTo: message.replyTo,
|
|
34
|
+
subject: message.subject,
|
|
35
|
+
text: message.text,
|
|
36
|
+
html: message.html,
|
|
37
|
+
disableFileAccess: true,
|
|
38
|
+
disableUrlAccess: true,
|
|
39
|
+
});
|
|
40
|
+
const receipt = {
|
|
41
|
+
messageId: info.messageId,
|
|
42
|
+
accepted: info.accepted.map(addressText),
|
|
43
|
+
rejected: info.rejected.map(addressText),
|
|
44
|
+
};
|
|
45
|
+
if (receipt.accepted.length === 0) {
|
|
46
|
+
throw new EmailError("delivery", `the server refused every recipient (${receipt.rejected.join(", ")})`);
|
|
47
|
+
}
|
|
48
|
+
return receipt;
|
|
49
|
+
}
|
|
50
|
+
catch (error) {
|
|
51
|
+
if (error instanceof EmailError)
|
|
52
|
+
throw error;
|
|
53
|
+
throw new EmailError("delivery", `sending through ${configuration.host} failed: ${describe(error)}`, {
|
|
54
|
+
cause: error,
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
},
|
|
58
|
+
async verify() {
|
|
59
|
+
try {
|
|
60
|
+
await transport.verify();
|
|
61
|
+
}
|
|
62
|
+
catch (error) {
|
|
63
|
+
throw new EmailError("delivery", `logging in to ${configuration.host} failed: ${describe(error)}`, {
|
|
64
|
+
cause: error,
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
},
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
/** A short reason without the password or message content. */
|
|
71
|
+
function describe(error) {
|
|
72
|
+
const { code, responseCode, message } = (error ?? {});
|
|
73
|
+
return [code, responseCode, message?.split("\n")[0]].filter(Boolean).join(" ") || "unknown error";
|
|
74
|
+
}
|
|
75
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../source/email-sender/index.ts"],"names":[],"mappings":"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"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { EmailMessage } from "../email-message/type.ts";
|
|
2
|
+
/** What the server said when it took the message. */
|
|
3
|
+
export interface EmailReceipt {
|
|
4
|
+
/** The Message-ID header, useful to find the message in logs. */
|
|
5
|
+
messageId: string;
|
|
6
|
+
/** Recipients the server accepted. */
|
|
7
|
+
accepted: string[];
|
|
8
|
+
/** Recipients the server refused (the message still went to the accepted ones). */
|
|
9
|
+
rejected: string[];
|
|
10
|
+
}
|
|
11
|
+
/** Sends email through one SMTP server. */
|
|
12
|
+
export interface EmailSender {
|
|
13
|
+
/** Checks the message, then sends it. Throws `EmailError` with code `invalid-message` or `delivery`. */
|
|
14
|
+
send(message: EmailMessage): Promise<EmailReceipt>;
|
|
15
|
+
/** Connects and logs in without sending, to check the configuration. Throws `EmailError` (`delivery`). */
|
|
16
|
+
verify(): Promise<void>;
|
|
17
|
+
}
|
|
18
|
+
//# sourceMappingURL=type.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"type.d.ts","sourceRoot":"","sources":["../../source/email-sender/type.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,0BAA0B,CAAC;AAE7D,qDAAqD;AACrD,MAAM,WAAW,YAAY;IAC3B,iEAAiE;IACjE,SAAS,EAAE,MAAM,CAAC;IAClB,sCAAsC;IACtC,QAAQ,EAAE,MAAM,EAAE,CAAC;IACnB,mFAAmF;IACnF,QAAQ,EAAE,MAAM,EAAE,CAAC;CACpB;AAED,2CAA2C;AAC3C,MAAM,WAAW,WAAW;IAC1B,wGAAwG;IACxG,IAAI,CAAC,OAAO,EAAE,YAAY,GAAG,OAAO,CAAC,YAAY,CAAC,CAAC;IACnD,0GAA0G;IAC1G,MAAM,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;CACzB"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"type.js","sourceRoot":"","sources":["../../source/email-sender/type.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { EmailServerConfiguration } from "./type.ts";
|
|
2
|
+
/**
|
|
3
|
+
* Reads the email server settings from environment variables:
|
|
4
|
+
* `EMAIL_SERVER_HOST`, `EMAIL_SERVER_PORT` (default 587), `EMAIL_SERVER_USERNAME`, `EMAIL_SERVER_PASSWORD` and the
|
|
5
|
+
* optional `EMAIL_SERVER_SECURITY` (`starttls`, `tls` or `none`). Pass another `prefix` to keep several servers.
|
|
6
|
+
* Throws `configuration` naming every missing or invalid variable.
|
|
7
|
+
*/
|
|
8
|
+
export declare function readEmailServerConfiguration(environment?: Record<string, string | undefined>, prefix?: string): EmailServerConfiguration;
|
|
9
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../source/email-server-configuration/index.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,wBAAwB,EAAuB,MAAM,WAAW,CAAC;AAI/E;;;;;GAKG;AACH,wBAAgB,4BAA4B,CAC1C,WAAW,GAAE,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,CAAe,EAC7D,MAAM,SAAkB,GACvB,wBAAwB,CAwB1B"}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { EmailError } from "../email-error/index.js";
|
|
2
|
+
const SECURITIES = ["starttls", "tls", "none"];
|
|
3
|
+
/**
|
|
4
|
+
* Reads the email server settings from environment variables:
|
|
5
|
+
* `EMAIL_SERVER_HOST`, `EMAIL_SERVER_PORT` (default 587), `EMAIL_SERVER_USERNAME`, `EMAIL_SERVER_PASSWORD` and the
|
|
6
|
+
* optional `EMAIL_SERVER_SECURITY` (`starttls`, `tls` or `none`). Pass another `prefix` to keep several servers.
|
|
7
|
+
* Throws `configuration` naming every missing or invalid variable.
|
|
8
|
+
*/
|
|
9
|
+
export function readEmailServerConfiguration(environment = process.env, prefix = "EMAIL_SERVER_") {
|
|
10
|
+
const read = (name) => environment[`${prefix}${name}`]?.trim() || undefined;
|
|
11
|
+
const host = read("HOST");
|
|
12
|
+
const username = read("USERNAME");
|
|
13
|
+
const password = read("PASSWORD");
|
|
14
|
+
const port = Number(read("PORT") ?? 587);
|
|
15
|
+
const security = read("SECURITY");
|
|
16
|
+
const problems = [
|
|
17
|
+
...["HOST", "USERNAME", "PASSWORD"]
|
|
18
|
+
.filter((name) => !read(name))
|
|
19
|
+
.map((name) => `${prefix}${name} is missing`),
|
|
20
|
+
...(Number.isInteger(port) && port > 0 && port < 65536 ? [] : [`${prefix}PORT must be a port number`]),
|
|
21
|
+
...(security === undefined || SECURITIES.includes(security)
|
|
22
|
+
? []
|
|
23
|
+
: [`${prefix}SECURITY must be one of ${SECURITIES.join(", ")}`]),
|
|
24
|
+
];
|
|
25
|
+
if (problems.length)
|
|
26
|
+
throw new EmailError("configuration", problems.join("; "));
|
|
27
|
+
return {
|
|
28
|
+
host: host,
|
|
29
|
+
port,
|
|
30
|
+
username: username,
|
|
31
|
+
password: password,
|
|
32
|
+
...(security ? { security: security } : {}),
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../source/email-server-configuration/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,yBAAyB,CAAC;AAGrD,MAAM,UAAU,GAAmC,CAAC,UAAU,EAAE,KAAK,EAAE,MAAM,CAAC,CAAC;AAE/E;;;;;GAKG;AACH,MAAM,UAAU,4BAA4B,CAC1C,cAAkD,OAAO,CAAC,GAAG,EAC7D,MAAM,GAAG,eAAe;IAExB,MAAM,IAAI,GAAG,CAAC,IAAY,EAAE,EAAE,CAAC,WAAW,CAAC,GAAG,MAAM,GAAG,IAAI,EAAE,CAAC,EAAE,IAAI,EAAE,IAAI,SAAS,CAAC;IACpF,MAAM,IAAI,GAAG,IAAI,CAAC,MAAM,CAAC,CAAC;IAC1B,MAAM,QAAQ,GAAG,IAAI,CAAC,UAAU,CAAC,CAAC;IAClC,MAAM,QAAQ,GAAG,IAAI,CAAC,UAAU,CAAC,CAAC;IAClC,MAAM,IAAI,GAAG,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,IAAI,GAAG,CAAC,CAAC;IACzC,MAAM,QAAQ,GAAG,IAAI,CAAC,UAAU,CAAC,CAAC;IAClC,MAAM,QAAQ,GAAG;QACf,GAAI,CAAC,MAAM,EAAE,UAAU,EAAE,UAAU,CAAW;aAC3C,MAAM,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;aAC7B,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,GAAG,MAAM,GAAG,IAAI,aAAa,CAAC;QAC/C,GAAG,CAAC,MAAM,CAAC,SAAS,CAAC,IAAI,CAAC,IAAI,IAAI,GAAG,CAAC,IAAI,IAAI,GAAG,KAAK,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,GAAG,MAAM,4BAA4B,CAAC,CAAC;QACtG,GAAG,CAAC,QAAQ,KAAK,SAAS,IAAI,UAAU,CAAC,QAAQ,CAAC,QAA+B,CAAC;YAChF,CAAC,CAAC,EAAE;YACJ,CAAC,CAAC,CAAC,GAAG,MAAM,2BAA2B,UAAU,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;KACnE,CAAC;IACF,IAAI,QAAQ,CAAC,MAAM;QAAE,MAAM,IAAI,UAAU,CAAC,eAAe,EAAE,QAAQ,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC;IAChF,OAAO;QACL,IAAI,EAAE,IAAK;QACX,IAAI;QACJ,QAAQ,EAAE,QAAS;QACnB,QAAQ,EAAE,QAAS;QACnB,GAAG,CAAC,QAAQ,CAAC,CAAC,CAAC,EAAE,QAAQ,EAAE,QAA+B,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;KACnE,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* How the connection is protected:
|
|
3
|
+
* - `starttls`: connect in plain text, then upgrade with STARTTLS, which is required (port 587, the default).
|
|
4
|
+
* - `tls`: TLS from the first byte (port 465).
|
|
5
|
+
* - `none`: no encryption, only for a local test server.
|
|
6
|
+
*/
|
|
7
|
+
export type EmailServerSecurity = "starttls" | "tls" | "none";
|
|
8
|
+
/** Where and how to log in to the SMTP server that sends the email. */
|
|
9
|
+
export interface EmailServerConfiguration {
|
|
10
|
+
host: string;
|
|
11
|
+
port: number;
|
|
12
|
+
username: string;
|
|
13
|
+
password: string;
|
|
14
|
+
/** Defaults to `tls` on port 465 and `starttls` everywhere else. */
|
|
15
|
+
security?: EmailServerSecurity;
|
|
16
|
+
/** Milliseconds to wait for the server before failing with `delivery`. Defaults to 10 000. */
|
|
17
|
+
timeout?: number;
|
|
18
|
+
}
|
|
19
|
+
//# sourceMappingURL=type.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"type.d.ts","sourceRoot":"","sources":["../../source/email-server-configuration/type.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,MAAM,MAAM,mBAAmB,GAAG,UAAU,GAAG,KAAK,GAAG,MAAM,CAAC;AAE9D,uEAAuE;AACvE,MAAM,WAAW,wBAAwB;IACvC,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;IACb,QAAQ,EAAE,MAAM,CAAC;IACjB,QAAQ,EAAE,MAAM,CAAC;IACjB,oEAAoE;IACpE,QAAQ,CAAC,EAAE,mBAAmB,CAAC;IAC/B,8FAA8F;IAC9F,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"type.js","sourceRoot":"","sources":["../../source/email-server-configuration/type.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Send email from web applications through any SMTP server.
|
|
3
|
+
*
|
|
4
|
+
* ```ts
|
|
5
|
+
* import { createEmailSender, readEmailServerConfiguration } from "@sharadox/email-web-library";
|
|
6
|
+
*
|
|
7
|
+
* const sender = createEmailSender(readEmailServerConfiguration());
|
|
8
|
+
* await sender.send({ from: "website@example.com", to: "inbox@example.com", subject: "Hello", text: "Hi!" });
|
|
9
|
+
* ```
|
|
10
|
+
*
|
|
11
|
+
* @packageDocumentation
|
|
12
|
+
*/
|
|
13
|
+
export { checkEmailAddress } from "./email-address/index.ts";
|
|
14
|
+
export type { EmailAddress } from "./email-address/type.ts";
|
|
15
|
+
export { EmailError } from "./email-error/index.ts";
|
|
16
|
+
export type { EmailErrorCode } from "./email-error/type.ts";
|
|
17
|
+
export { checkEmailMessage, RECIPIENT_LIMIT, SUBJECT_LIMIT } from "./email-message/index.ts";
|
|
18
|
+
export type { EmailMessage } from "./email-message/type.ts";
|
|
19
|
+
export { createEmailSender } from "./email-sender/index.ts";
|
|
20
|
+
export type { EmailReceipt, EmailSender } from "./email-sender/type.ts";
|
|
21
|
+
export { readEmailServerConfiguration } from "./email-server-configuration/index.ts";
|
|
22
|
+
export type { EmailServerConfiguration, EmailServerSecurity } from "./email-server-configuration/type.ts";
|
|
23
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../source/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AACH,OAAO,EAAE,iBAAiB,EAAE,MAAM,0BAA0B,CAAC;AAC7D,YAAY,EAAE,YAAY,EAAE,MAAM,yBAAyB,CAAC;AAC5D,OAAO,EAAE,UAAU,EAAE,MAAM,wBAAwB,CAAC;AACpD,YAAY,EAAE,cAAc,EAAE,MAAM,uBAAuB,CAAC;AAC5D,OAAO,EAAE,iBAAiB,EAAE,eAAe,EAAE,aAAa,EAAE,MAAM,0BAA0B,CAAC;AAC7F,YAAY,EAAE,YAAY,EAAE,MAAM,yBAAyB,CAAC;AAC5D,OAAO,EAAE,iBAAiB,EAAE,MAAM,yBAAyB,CAAC;AAC5D,YAAY,EAAE,YAAY,EAAE,WAAW,EAAE,MAAM,wBAAwB,CAAC;AACxE,OAAO,EAAE,4BAA4B,EAAE,MAAM,uCAAuC,CAAC;AACrF,YAAY,EAAE,wBAAwB,EAAE,mBAAmB,EAAE,MAAM,sCAAsC,CAAC"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Send email from web applications through any SMTP server.
|
|
3
|
+
*
|
|
4
|
+
* ```ts
|
|
5
|
+
* import { createEmailSender, readEmailServerConfiguration } from "@sharadox/email-web-library";
|
|
6
|
+
*
|
|
7
|
+
* const sender = createEmailSender(readEmailServerConfiguration());
|
|
8
|
+
* await sender.send({ from: "website@example.com", to: "inbox@example.com", subject: "Hello", text: "Hi!" });
|
|
9
|
+
* ```
|
|
10
|
+
*
|
|
11
|
+
* @packageDocumentation
|
|
12
|
+
*/
|
|
13
|
+
export { checkEmailAddress } from "./email-address/index.js";
|
|
14
|
+
export { EmailError } from "./email-error/index.js";
|
|
15
|
+
export { checkEmailMessage, RECIPIENT_LIMIT, SUBJECT_LIMIT } from "./email-message/index.js";
|
|
16
|
+
export { createEmailSender } from "./email-sender/index.js";
|
|
17
|
+
export { readEmailServerConfiguration } from "./email-server-configuration/index.js";
|
|
18
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../source/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AACH,OAAO,EAAE,iBAAiB,EAAE,MAAM,0BAA0B,CAAC;AAE7D,OAAO,EAAE,UAAU,EAAE,MAAM,wBAAwB,CAAC;AAEpD,OAAO,EAAE,iBAAiB,EAAE,eAAe,EAAE,aAAa,EAAE,MAAM,0BAA0B,CAAC;AAE7F,OAAO,EAAE,iBAAiB,EAAE,MAAM,yBAAyB,CAAC;AAE5D,OAAO,EAAE,4BAA4B,EAAE,MAAM,uCAAuC,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,79 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sharadox/email-web-library",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "Send email from web applications through any SMTP server, with message checks and clear errors.",
|
|
5
|
+
"license": "SEE LICENSE IN LICENCA.md",
|
|
6
|
+
"author": "Sharadox <email@sharadox.com>",
|
|
7
|
+
"homepage": "https://github.com/Sharadox/email-web-library#readme",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "git+https://github.com/Sharadox/email-web-library.git"
|
|
11
|
+
},
|
|
12
|
+
"bugs": "https://github.com/Sharadox/email-web-library/issues",
|
|
13
|
+
"keywords": [
|
|
14
|
+
"email",
|
|
15
|
+
"smtp",
|
|
16
|
+
"nextjs",
|
|
17
|
+
"server-actions",
|
|
18
|
+
"contact-form"
|
|
19
|
+
],
|
|
20
|
+
"type": "module",
|
|
21
|
+
"exports": {
|
|
22
|
+
".": {
|
|
23
|
+
"types": "./distribution/index.d.ts",
|
|
24
|
+
"default": "./distribution/index.js"
|
|
25
|
+
}
|
|
26
|
+
},
|
|
27
|
+
"files": [
|
|
28
|
+
"distribution",
|
|
29
|
+
"LICENCA.md",
|
|
30
|
+
"LICENSE.md",
|
|
31
|
+
"NOTICE.md"
|
|
32
|
+
],
|
|
33
|
+
"sideEffects": false,
|
|
34
|
+
"engines": {
|
|
35
|
+
"node": ">=20"
|
|
36
|
+
},
|
|
37
|
+
"publishConfig": {
|
|
38
|
+
"access": "public",
|
|
39
|
+
"provenance": true
|
|
40
|
+
},
|
|
41
|
+
"lint-staged": {
|
|
42
|
+
"*.{ts,js,mjs,cjs}": [
|
|
43
|
+
"prettier --write",
|
|
44
|
+
"eslint --fix --max-warnings=0"
|
|
45
|
+
],
|
|
46
|
+
"*.{json,md,yml,yaml}": "prettier --write"
|
|
47
|
+
},
|
|
48
|
+
"dependencies": {
|
|
49
|
+
"nodemailer": "^10.1.4"
|
|
50
|
+
},
|
|
51
|
+
"devDependencies": {
|
|
52
|
+
"@eslint/js": "^9.39.5",
|
|
53
|
+
"@types/node": "^24.19.2",
|
|
54
|
+
"@types/nodemailer": "^8.0.2",
|
|
55
|
+
"@types/smtp-server": "^3.5.13",
|
|
56
|
+
"eslint": "9.39.5",
|
|
57
|
+
"eslint-config-prettier": "^10.1.8",
|
|
58
|
+
"husky": "^9.1.7",
|
|
59
|
+
"lint-staged": "^17.6.0",
|
|
60
|
+
"prettier": "^3.9.10",
|
|
61
|
+
"smtp-server": "^3.21.3",
|
|
62
|
+
"turbo": "^2.11.10",
|
|
63
|
+
"typedoc": "^0.28.20",
|
|
64
|
+
"typescript": "6.0.3",
|
|
65
|
+
"typescript-eslint": "^8.72.1",
|
|
66
|
+
"vitest": "^5.1.0"
|
|
67
|
+
},
|
|
68
|
+
"scripts": {
|
|
69
|
+
"build": "tsc -p tsconfig.build.json",
|
|
70
|
+
"lint": "eslint --max-warnings=0 .",
|
|
71
|
+
"format": "prettier --write .",
|
|
72
|
+
"format:check": "prettier --check .",
|
|
73
|
+
"type-check": "tsc --noEmit",
|
|
74
|
+
"test:unit": "vitest run",
|
|
75
|
+
"test:benchmark": "vitest bench --run --passWithNoTests",
|
|
76
|
+
"documentation": "typedoc",
|
|
77
|
+
"verify": "turbo run format:check lint type-check test:unit test:benchmark build"
|
|
78
|
+
}
|
|
6
79
|
}
|