簡體   English   中英

是否可以通過 AWS LightSail 實例上的 AWS SES 發送電子郵件?

[英]Is it possible to send emails via AWS SES on AWS LightSail instance?

我嘗試使用以下 Node.js JavaScript AWS SDK 代碼通過 AWS SES 在我的 LightSail 實例上發送電子郵件,但失敗並顯示以下錯誤消息。 email 發送代碼在我的開發計算機上運行良好。 (email發送碼來自https://docs.aws.amazon.com/sdk-for-javascript/v3/developer-guide/ses-examples-sending-email.html。

(代碼在這里)

import { readFile } from 'fs/promises';
import * as path from 'path';
import { SESClient } from "@aws-sdk/client-ses";

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

const awsConfigFileFullName = "aws_config.json";

let awsConfigFileFullPath = path.join(__dirname, awsConfigFileFullName);

const awsConfig = await readFile(awsConfigFileFullPath).then(json => JSON.parse(json)).catch(() => null);

aws_ses_client = new SESClient({ region: awsConfig.region, accessKeyId: awsConfig.accessKeyId, secretAccessKey: awsConfig.secretAccessKey });

const createSendEmailCommand = (toAddress, fromAddress, htmlContent, textContent, emailSubject) => {
  return new SendEmailCommand({
    Destination: {
      /* required */
      CcAddresses: [
        /* more items */
      ],
      ToAddresses: [
        toAddress,
        /* more To-email addresses */
      ],
    },
    Message: {
      /* required */
      Body: {
        /* required */
        Html: {
          Charset: "UTF-8",
          Data: htmlContent,
        },
        Text: {
          Charset: "UTF-8",
          Data: textContent,
        },
      },
      Subject: {
        Charset: "UTF-8",
        Data: emailSubject,
      },
    },
    Source: emailSenderName + '<' + fromAddress + '>',
    ReplyToAddresses: [
      /* more items */
    ],
  });
};

const sendEmailCommand = createSendEmailCommand(
recipientEmailAddress,
senderEmailAddress,
htmlEmailContent,
textEmailContent,
emailSubject
);

try {
await aws_ses_client.send(sendEmailCommand);
} catch (e) {
console.error("Failed to send email.", e);
}

(此處錯誤)

AccessDenied:用戶arn:aws:sts::(some number):assumed-role/AmazonLightsailInstanceRole/i-(some alphanumeric number)is not authorized to perform ses:SendEmail' 1:(一些數字):identity/(recipient email address)'

在網上對該問題進行了一些搜索后,認為錯誤是由端口 25 限制 Lightsail 實例限制 ( https://aws.amazon.com/premiumsupport/knowledge-center/lightsail-port-25-throttle/ ) 引起的,我向 AWS 發送了限制刪除請求,但請求被拒絕,告訴我“考慮調查簡單的 Email 服務”。 我發送了一條回復,詢問是否可以在 LightSail 實例上通過 AWS SES 發送電子郵件,但收到的回復是“我們無法批准您的請求”; 我覺得第二個回復要么是自動回復,要么甚至沒有被評論 email 的人仔細閱讀。

我的 LightSail 網絡服務器上安裝了一個多站點 WordPress,它可以使用 WP Mail SMTP 插件通過 AWS SES 發送電子郵件,使用 SMTP 端口 587 進行 TLS 加密。我認為這證明可以通過 AWS SES 在 LightSail 實例上發送電子郵件. 我的 WordPress 每天都會使用我的 AWS SES SMTP 憑證在我的 LightSail 網絡服務器上發送電子郵件; 因此,也許必須在 LightSail 實例 email 發送代碼中直接聯系 AWS SES SMTP 服務器來發送電子郵件,而不是在代碼中使用經過身份驗證的 SES 客戶端 object?

我在想,也許假定的角色 AmazonLightsailInstanceRole 不允許發送 AWS SES email。 我檢查了我的 AWS IAM web 控制台,沒有名為 AmazonLightsailInstanceRole 的角色; 看起來我無法修改代入角色 AmazonLightsailInstanceRole 的策略。

是否可以將 AWS SES 電子郵件發送權限授予代入角色 AmazonLightsailInstanceRole? 如果是這樣,如何?

是否可以在 AWS LightSail 實例上通過 AWS SES 發送電子郵件? 如果是這樣,如何在 Node.js JavaScript AWS SDK 代碼中完成? 任何驗證、參考和/或指示都會非常有幫助。

聽起來像是 IAM 問題 - 如果您不傳遞自己的憑據,則實例角色是后備選項。 代碼的第 6..8 行正在尋找一個文件 ( aws_config.json ),其中包含一個 API 密鑰和秘密以及一個默認區域,然后在第 9 行將這些值傳遞給SESClient 當您最終調用aws_ses_client.send(sendEmailCommand)時,它將使用 lightsail 提供的實例默認憑據,它無法訪問您的 aws 帳戶中的資源,如 SES。 檢查以下內容:

  • 文件aws_config.json是否存在?
  • 它是否包含您 AWS 賬戶中有效用戶的 api 密鑰/機密/區域?
  • 該用戶是否具有合適的 IAM 策略,包括ses:SendEmail和可能的其他 iam 權限?

僅供參考catch(() => null)可能隱藏了一個錯誤——你應該在你的catch()語句中使用 console.log 來寫一個錯誤(至少在你調試的時候)。

更新

在 EC2 實例上測試我的 Node.js Express web 應用程序時,為了解決我遇到的錯誤,我遇到了以下問題: 在 Node 中本地使用 dynamodb 時,“無法從任何提供商加載憑據”

因為我使用的是@aws-sdk/ses-client(版本 3),而不是版本 2,所以我應該使用以下內容:

aws_s3_client = new S3Client({ region: awsConfig.region, credentials: {accessKeyId: awsConfig.accessKeyId, secretAccessKey: awsConfig.secretAccessKey }});
aws_ses_client = new SESClient({ region: awsConfig.region, credentials: {accessKeyId: awsConfig.accessKeyId, secretAccessKey: awsConfig.secretAccessKey }});

使用上面的代碼啟用了我在 LightSail 實例上運行的 web 應用程序中的 AWS SES 和 S3。 所以,答案是肯定的,可以通過 AWS LightSail 實例上的 AWS SES 發送電子郵件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM