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

import { AwsContext } from "./context.ts";
import { Mail, MailDeliverer } from "../mail.ts";

export class AwsMailDeliverer extends MailDeliverer {
  private _ses;

  constructor(readonly aws: AwsContext) {
    super("aws");

    const { region, credentials } = aws;

    this._ses = new SESv2Client({ region, credentials });
  }

  protected override async doDeliver(mail: Mail): Promise<void> {
    let awsMessageId: string | undefined;

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

      const res = await this._ses.send(sendCommand);
      awsMessageId = res.MessageId;
    } catch (cause) {
      mail.throwDeliverError(
        this,
        "failed to call send-email api of aws.",
        cause,
      );
    }

    if (awsMessageId == null) {
      mail.setDelivered(this, new Error("No message id is returned from aws."));
    }
    mail.awsMessageId = awsMessageId ?? null;
  }
}