使用 SES for Ruby 的 Amazon SDK 範例 - AWS SDK 程式碼範例

文件 AWS SDK AWS 範例 SDK 儲存庫中有更多可用的 GitHub 範例。

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

使用 SES for Ruby 的 Amazon SDK 範例

下列程式碼範例示範如何搭配 Amazon AWS SDK for Ruby SES 使用 來執行動作和實作常見案例。

Actions 是大型程式的程式碼摘錄,必須在內容中執行。雖然動作會示範如何呼叫個別服務函數,但您可以在相關案例中查看內容中的動作。

每個範例都包含完整原始程式碼的連結,您可以在其中找到如何在內容中設定和執行程式碼的指示。

主題

動作

下列程式碼範例示範如何使用 GetIdentityVerificationAttributes

Ruby 的 SDK
注意

還有更多 on GitHub。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

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 puts email if status == 'Success' end

下列程式碼範例示範如何使用 ListIdentities

Ruby 的 SDK
注意

還有更多 on GitHub。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

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 puts email if status == 'Success' end
  • 如需 API 詳細資訊,請參閱 ListIdentities AWS SDK for Ruby 參考中的 API

下列程式碼範例示範如何使用 SendEmail

Ruby 的 SDK
注意

還有更多 on GitHub。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

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 => e puts "Email not sent. Error message: #{e}" end
  • 如需 API 詳細資訊,請參閱 SendEmail AWS SDK for Ruby 參考中的 API

下列程式碼範例示範如何使用 VerifyEmailIdentity

Ruby 的 SDK
注意

還有更多 on GitHub。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

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 => e puts "Email not sent. Error message: #{e}" end