使用适用于 Ruby 的 SDK 的 Amazon SES 示例 - Amazon 适用于 Ruby 的 SDK
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

使用适用于 Ruby 的 SDK 的 Amazon SES 示例

以下代码示例向您展示了如何在 Amazon SES 中使用来执行操作和实现常见场景。 Amazon SDK for Ruby

操作是大型程序的代码摘录,必须在上下文中运行。您可以通过操作了解如何调用单个服务函数,还可以通过函数相关场景和跨服务示例的上下文查看操作。

场景 是展示如何通过在同一服务中调用多个函数来完成特定任务的代码示例。

每个示例都包含一个指向的链接 GitHub,您可以在其中找到有关如何在上下文中设置和运行代码的说明。

主题

操作

以下代码示例演示了如何使用 GetIdentityVerificationAttributes

适用于 Ruby 的 SDK
注意

还有更多相关信息 GitHub。在 Amazon 代码示例存储库中查找完整示例,了解如何进行设置和运行。

require "aws-sdk-ses" # v2: require 'aws-sdk' # Create client in us-west-2 region # Replace us-west-2 with the AWS Region you're using for Amazon SES. client = Aws::SES::Client.new(region: "us-west-2") # Get up to 1000 identities ids = client.list_identities({ identity_type: "EmailAddress" }) ids.identities.each do |email| attrs = client.get_identity_verification_attributes({ identities: [email] }) status = attrs.verification_attributes[email].verification_status # Display email addresses that have been verified if status == "Success" puts email end end

以下代码示例演示了如何使用 ListIdentities

适用于 Ruby 的 SDK
注意

还有更多相关信息 GitHub。在 Amazon 代码示例存储库中查找完整示例,了解如何进行设置和运行。

require "aws-sdk-ses" # v2: require 'aws-sdk' # Create client in us-west-2 region # Replace us-west-2 with the AWS Region you're using for Amazon SES. client = Aws::SES::Client.new(region: "us-west-2") # Get up to 1000 identities ids = client.list_identities({ identity_type: "EmailAddress" }) ids.identities.each do |email| attrs = client.get_identity_verification_attributes({ identities: [email] }) status = attrs.verification_attributes[email].verification_status # Display email addresses that have been verified if status == "Success" puts email end end
  • 有关 API 的详细信息,请参阅 Amazon SDK for Ruby API 参考ListIdentities中的。

以下代码示例演示了如何使用 SendEmail

适用于 Ruby 的 SDK
注意

还有更多相关信息 GitHub。在 Amazon 代码示例存储库中查找完整示例,了解如何进行设置和运行。

require "aws-sdk-ses" # v2: require 'aws-sdk' # Replace sender@example.com with your "From" address. # This address must be verified with Amazon SES. sender = "sender@example.com" # Replace recipient@example.com with a "To" address. If your account # is still in the sandbox, this address must be verified. recipient = "recipient@example.com" # Specify a configuration set. To use a configuration # set, uncomment the next line and line 74. # configsetname = "ConfigSet" # The subject line for the email. subject = "Amazon SES test (AWS SDK for Ruby)" # The HTML body of the email. htmlbody = "<h1>Amazon SES test (AWS SDK for Ruby)</h1>"\ '<p>This email was sent with <a href="https://aws.amazon.com/ses/">'\ 'Amazon SES</a> using the <a href="https://aws.amazon.com/sdk-for-ruby/">'\ "AWS SDK for Ruby</a>." # The email body for recipients with non-HTML email clients. textbody = "This email was sent with Amazon SES using the AWS SDK for Ruby." # Specify the text encoding scheme. encoding = "UTF-8" # Create a new SES client in the us-west-2 region. # Replace us-west-2 with the AWS Region you're using for Amazon SES. ses = Aws::SES::Client.new(region: "us-west-2") # Try to send the email. begin # Provide the contents of the email. ses.send_email( destination: { to_addresses: [ recipient ] }, message: { body: { html: { charset: encoding, data: htmlbody }, text: { charset: encoding, data: textbody } }, subject: { charset: encoding, data: subject } }, source: sender, # Uncomment the following line to use a configuration set. # configuration_set_name: configsetname, ) puts "Email sent to " + recipient # If something goes wrong, display an error message. rescue Aws::SES::Errors::ServiceError => error puts "Email not sent. Error message: #{error}" end
  • 有关 API 的详细信息,请参阅 Amazon SDK for Ruby API 参考SendEmail中的。

以下代码示例演示了如何使用 VerifyEmailIdentity

适用于 Ruby 的 SDK
注意

还有更多相关信息 GitHub。在 Amazon 代码示例存储库中查找完整示例,了解如何进行设置和运行。

require "aws-sdk-ses" # v2: require 'aws-sdk' # Replace recipient@example.com with a "To" address. recipient = "recipient@example.com" # Create a new SES resource in the us-west-2 region. # Replace us-west-2 with the AWS Region you're using for Amazon SES. ses = Aws::SES::Client.new(region: "us-west-2") # Try to verify email address. begin ses.verify_email_identity({ email_address: recipient }) puts "Email sent to " + recipient # If something goes wrong, display an error message. rescue Aws::SES::Errors::ServiceError => error puts "Email not sent. Error message: #{error}" end
  • 有关 API 的详细信息,请参阅 Amazon SDK for Ruby API 参考VerifyEmailIdentity中的。