使用 Nodemailer、Localstack 和 Node AWS 开发工具包发送电子邮件,我收到各种错误:
AWS_SES_ENDPOINT=本地主机:4566
Error: connect ECONNREFUSED 127.0.0.1:80
at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1161:16)
AWS_SES_ENDPOINT=127.0.0.1:4566
uncaughtException: Invalid URL
TypeError [ERR_INVALID_URL]: Invalid URL
at new NodeError (node:internal/errors:371:5)
at onParseError (node:internal/url:552:9)
at new URL (node:internal/url:628:5)
at parseUrl (/something/node_modules/@aws-sdk/url-parser/dist-cjs/index.js:6:60)
at resolveEndpointsConfig (/something/node_modules/@aws-sdk/config-resolver/dist-cjs/endpointsConfig/resolveEndpointsConfig.js
AWS_SES_ENDPOINT=http://localhost:4578 和 AWS_SES_ENDPOINT=http://127.0.0.1:4578
Error: read ECONNRESET
at TCP.onStreamRead (node:internal/stream_base_commons:220:20
AWS_SES_ENDPOINT=http://localhost:4566 和 AWS_SES_ENDPOINT=smtp://127.0.0.1:4566
UnknownError
at deserializeAws_querySendRawEmailCommandError (/something/node_modules/@aws-sdk/client-ses/dist-cjs/protocols/Aws_query.js:2779:24)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
at async /something/node_modules/@aws-sdk/middleware-serde/dist-cjs/deserializerMiddleware.js:7:24
at async /something/node_modules/@aws-sdk/middleware-signing/dist-cjs/middleware.js:11:20
at async StandardRetryStrategy.retry (/something/node_modules/@aws-sdk/middleware-retry/dist-cjs/StandardRetryStrategy.js:51:46)
at async /something/node_modules/@aws-sdk/middleware-logger/dist-cjs/loggerMiddleware.js:6:22
AWS_SES_ENDPOINT=smtp://127.0.0.1:4578
Error [TimeoutError]: socket hang up
at connResetException (node:internal/errors:691:14)
at Socket.socketOnEnd (node:_http_client:471:23)
at Socket.emit (node:events:402:35)
at endReadableNT (node:internal/streams/readable:1343:12)
at processTicksAndRejections (node:internal/process/task_queues:83:21) {
code: 'ECONNRESET',
节点邮件程序的SES设置如下:
const sesConfig: SESClientConfig = {
region: "us-east-1",
endpoint: AWS_SES_ENDPOINT,
};
const ses = new SES(sesConfig);
Localstack 是在 docker-compose.yml 中设置的,如下所示:
services:
qatool-localstack:
container_name: "my-localstack"
image: localstack/localstack
ports:
- "127.0.0.1:4566:4566"
- "127.0.0.1:4572:4572"
- "127.0.0.1:4578:4578"
environment:
- SERVICES=sqs:4566,s3:4572,ses:4578
- DEFAULT_REGION=us-east-1
- DATA_DIR=${TMPDIR:-/tmp/}localstack/data
- HOST_TMP_FOLDER=${TMPDIR:-/tmp/}localstack
volumes:
- "${TMPDIR:-/tmp}/localstack:/tmp/localstack"
- "/var/run/docker.sock:/var/run/docker.sock"
- './localstackSetup.sh:/docker-entrypoint-initaws.d/make-s3.sh'
Nodemailer 和 AWS SES 版本:
"@aws-sdk/client-ses": "^3.110.0",
"nodemailer": "^6.7.5",
的前两个错误localhost:4566
和127.0.0.1:4566
是因为不包括协议。您可以看到 AWS SES 未正确解析配置,例如:
ses.config.endpoint().then((val) => { console.log(JSON.stringify(val)); });
给:
{"hostname":"","protocol":"localhost:","path":"4566"}
其中本地主机是协议而不是主机名。
与端口 4578 的连接对我不起作用。
做完之后(从电子邮件替换):
aws ses verify-email-identity --email-address from-email@from.com --endpoint-url=http://localhost:4566
这些端点有效:
http://127.0.0.1:4566
http://localhost:4566
smtp://localhost:4566
smtp://127.0.0.1:4566
您可以通过转到以下位置来验证电子邮件是否已发送:
http://localhost:4566/_localstack/ses
Localstack不会在免费版本中发送实际的电子邮件。
您可以通过转到以下位置来验证 SES 是否正在运行:
http://localhost:4566/health