aboutsummaryrefslogtreecommitdiff
path: root/services/docker/mail-server/relay/aws/deliver.ts
blob: 793c85a1752c5e5a88d4a15a813a8a46c073d26c (plain)
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
import { SendEmailCommand, SESv2Client } from "@aws-sdk/client-sesv2";

import { AwsContext } from "./context.ts";
import {
  Mail,
  MailDeliverContext,
  MailDeliverer,
  MailDeliverReceiptResult,
} from "./mail.ts";
import { warn } from "../logger.ts";
import { log } from "node:console";

export class AwsMailDeliverer extends MailDeliverer {
  readonly name = "aws";
  readonly #ses;

  constructor(aws: AwsContext) {
    super();
    this.#ses = new SESv2Client(aws);
  }

  protected override async doDeliver(
    mail: Mail,
    context: MailDeliverContext,
  ): Promise<void> {
    log("Begin to call aws send-email api...");

    const result: MailDeliverReceiptResult = {
      kind: "done",
      message: "Success to call send-email api of aws.",
    };

    try {
      const sendCommand = new SendEmailCommand({
        Content: {
          Raw: { Data: mail.toUtf8Bytes() },
        },
      });

      const res = await this.#ses.send(sendCommand);
      if (res.MessageId == null) {
        warn("Aws send-email returns no message id.");
      }
      mail.awsMessageId = res.MessageId;
    } catch (cause) {
      result.kind = "fail";
      result.message = "An error was thrown when calling aws send-email." +
        cause;
      result.cause = cause;
    }
    context.result.set("*", result);
  }
}