如果所有域没有 IAM 策略设置,则无法使用 AWS SES 和 Rails' Action Mailer 发送电子邮件

Cannot send emails using AWS SES and Rails' Action Mailer without IAM Policy settings for all domains

我想在 Rails (v6) Ruby 中通过 Action Mailer 使用 AWS SES 发送电子邮件。 AWS 提供 aws-sdk-rails gem,使用 SES 可以轻松配置,但我意识到它需要可发送权限,例如 ses:SendEmail 到 SES 中的所有域。

# config/initializers/aws-sdk.rb
Aws.config[:credentials] = Aws::Credentials.new(ENV["AWS_ACCESS_KEY_ID"], ENV["AWS_SECRET_ACCESS_KEY"])

Aws::Rails.add_action_mailer_delivery_method(:aws_sdk, region: "us-east-1")
Rails.application.config.action_mailer.delivery_method = :aws_sdk
# app/mailers/application_mailer.rb
class ApplicationMailer < ActionMailer::Base
  default from: "info@example.com"
end

AWS IAM 用户具有以下策略,允许仅从 example.com 域发送电子邮件。

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Action": [
        "ses:SendEmail",
        "ses:SendRawEmail"
      ],
      "Resource": [
        "arn:aws:ses:us-east-1:xxxxxxxxxxxx:identity/example.com"
      ],
      "Effect": "Allow"
    }
  ]
}

但是我在工作人员发送邮件时收到如下错误。

ERROR: Processor failed: User `arn:aws:iam::xxxxxxxxxxxx:user/my-group/my-iam-user' is not authorized to perform `ses:SendRawEmail' on resource `arn:aws:ses:us-east-1:xxxxxxxxxxxx:identity/other-domain.com'

我觉得SDK默认会验证是否所有域都有可发送权限,但我找不到指定目标域。我该怎么办?

这是由于域在沙盒帐户中造成的。在 SES documentation 中,确定了以下状态:

  • 您只能将邮件发送到经过验证的电子邮件地址和域,或发送到 Amazon SES 邮箱模拟器。
  • 您只能从经过验证的电子邮件地址和域发送邮件。

要将电子邮件 发送到 您需要从域 out of sandbox mode.

移出的任何电子邮件地址