在 typescript 中使用SES发送电子邮件

mv1qrgav  于 2023-03-19  发布在  TypeScript
关注(0)|答案(3)|浏览(118)

我正在尝试使用ses在typescript中设置电子邮件服务。我遵循了这个示例https://github.com/awsdocs/aws-doc-sdk-examples/blob/master/javascript/example_code/ses/ses_sendemail.js,但我得到
AWS.ses 不是构造函数。
下面是我的emailService类

const AWS = require("aws-sdk");
const loggerService: LoggerServiceInterface = new LoggerService();

AWS.config.update({region: "us-east-1"});

export class EmailService {

sendEmail(recipient: string): Promise<boolean> {

 const params = {
  Destination: { /* required */
    CcAddresses: [
      "abcd@gmail.com",
      /* more items */
    ],
    ToAddresses: [
      "abcd@gmail.com",
      /* more items */
    ]
  },
  Message: { /* required */
    Body: { /* required */
      Html: {
       Charset: "UTF-8",
       Data: "HTML_FORMAT_BODY"
      },
      Text: {
       Charset: "UTF-8",
       Data: "TEXT_FORMAT_BODY"
      }
     },
     Subject: {
      Charset: "UTF-8",
      Data: "Test email"
     }
    },
  Source: "xyxy@gmail.com",
  ReplyToAddresses: [
     "xyxy@gmail.com",
  ],
};

const sendPromise = new AWS.SES({apiVersion: "2010-12-01"}).sendEmail(params).promise();

sendPromise.then(function(data) {
  console.log(data.MessageId);
}).catch(
  function(err) {
  console.error(err, err.stack);
});

return Promise.resolve(true);
}
}

我在一个sanbox环境中,这两封邮件都经过了验证。我是不是漏掉了什么?

bvhaajcl

bvhaajcl1#

这对我有用

import { SES, AWSError } from 'aws-sdk';
import { SendEmailRequest, SendEmailResponse } from 'aws-sdk/clients/ses';

const ses = new SES();

const params: SendEmailRequest = {
    Source: sender,
    Destination: {
        ToAddresses: [
            recipient
        ]
    },
    Message: {
        Subject: {
            Data: subject,
            Charset: charset
        },
        Body: {
            Text: {
                Data: body,
                Charset: charset
            },
            Html: {
                Data: html,
                Charset: charset
            }
        }
    }
}

ses.sendEmail(params, (err: AWSError, data: SendEmailResponse) => {
  if (err) console.log(err, err.stack);
    else console.log(data);
});
bd1hkmkf

bd1hkmkf2#

使用异步lambda函数和AWS-SDK SES with promises

import { Handler } from 'aws-lambda';
import { SES } from 'aws-sdk';

type Event = {
  ...
};

export const handler: Handler<Event> = async (event: Event) => {
  try {
    const params: SendEmailRequest = {
      ...
    }
    var result = await ses.sendEmail(params).promise();
  } catch (exception) { ...
  }
};
xvw2m8pv

xvw2m8pv3#

使用AWS SDK v-3:

const sesClient = new SESClient({ region: REGION });
    const paramsForEmail = {
        Destination: {
            ToAddresses: [email],
        },
        Message: {
            Body: {
                Html: {
                    Charset: "UTF-8",
                    Data: `
                    <p>              
                      Hello world!
                    </p>
                    `
                }
            },
            Subject: { Data: "Hello World" },
        },
        Source: EMAIL_FROM,
        ReturnPath: EMAIL_RETURN,
    };
    const resultEmail = await sesClient.send( new SendEmailCommand(paramsForEmail) );
    sesClient.destroy();

相关问题