Amazon SES 发邮件, PHP 开发Amazon SES, AWS适用于 PHP 的开发工具包, Amazon SES 设置电子邮件, Amazon Simple Email Service

 

电子邮件格式和 Amazon SES

 

当客户端向 Amazon SES 提出请求时,Amazon SES 将构建符合 Internet 邮件格式规范 (RFC 5322)。一封电子邮件包含标头正文 和信封,如下所述。

  • 标头…包含路由说明和有关邮件的信息。例如,发件人的地址、收件人的地址、主题和日期。标头类似于普通信件顶部的信息,但前者可能包含许多其他类型的信息,如邮件的格式。
  • 正文—包含邮件本身的文本。
  • 信封 —包含 SMTP 会话期间电子邮件客户端与邮件服务器之间通信的实际路由信息。此电子邮件信封信息类似于普通信件信封上的信息。电子邮件信封的路由信息通常与电子邮件标头中的路由信息相同,但也不总是相同。例如,当您发送密件抄送 (BCC) 时,实际收件人地址 (源自信封) 与收件人的电子邮件客户端中显示的“收件人”地址 (源自标头) 不相同。

以下是电子邮件的简单示例。标头后跟一个空白行,然后是电子邮件的正文。信封未显示,因为它是在 SMTP 会话期间在客户端与邮件服务器之间通信的,而不是电子邮件本身的一部分。

Received: from abc.smtp-out.amazonses.com (123.45.67.89) by in.example.com (87.65.43.210); Fri, 17 Dec 2010 14:26:22
From: "Andrew" <andrew@example.com>;
To: "Bob" <bob@example.com>
Date: Fri, 17 Dec 2010 14:26:21 -0800
Subject: Hello
Message-ID: <61967230-7A45-4A9D-BEC9-87CBCF2211C9@example.com>
Accept-Language: en-US
Content-Language: en-US
Content-Type: text/plain; charset="us-ascii"
Content-Transfer-Encoding: quoted-printable
MIME-Version: 1.0

Hello, I hope you are having a good day.

-Andrew

 

以下部分回顾电子邮件标头和正文并确定您使用 Amazon SES 时需要提供的信息。

 

电子邮件标头

每封电子邮件都有一个标头。标头的每一行包含一个字段,后跟冒号,然后跟字段正文。当您在电子邮件客户端中阅读电子邮件时,电子邮件客户端通常会显示以下标头字段的值:

  • To—邮件收件人的电子邮件地址。
  • CC—邮件的抄送收件人的电子邮件地址。
  • From—发送电子邮件的电子邮件地址。
  • Subject—邮件主题的摘要。
  • Date—发送电子邮件的时间和日期。

有许多其他的提供路由信息和描述邮件内容的标头字段。电子邮件客户端通常不会向用户显示这些字段。有关 Amazon SES 接受的标头字段的完整列表,请参阅。标题字段。当您使用 Amazon SES 时,您特别需要了解 “From”、“Reply-To” 和 “Return-Path” 标头字段之间的差异。如前所述,“From”地址是邮件发件人的电子邮件地址,而“Reply-To”和“Return-Path”则如下所示:

  • Reply-To —回复将发送到的电子邮件地址。默认情况下,回复将发送到原始发件人的电子邮件地址。
  • Return-Path—退回邮件和投诉应发送到的电子邮件地址。“Return-Path”有时称为“envelope from”、“envelope sender”或“MAIL FROM”。
    注意

    当您使用 Amazon SES 时,我们建议您始终设置 “Return-Path” 参数,以便您可以在发生退回邮件时知道情况并采取纠正措施。

要轻松地将退回的邮件与其目标收件人进行匹配,您可以使用可变信封退回路径 (VERP)。利用 VERP,您为每个收件人设置不同的“Return-Path”,因此,如果有退回邮件,您会自动知道退回邮件的收件人,而不必打开退回邮件并进行分析。

 

电子邮件正文

电子邮件正文包含邮件的文本。正文可采用以下格式进行发送:

  • HTML—如果收件人的电子邮件客户端可解释 HTML,则正文可以包含格式化的文本和超链接
  • 纯文本—如果收件人的电子邮件客户端是基于文本的,则正文不得包含任何不可打印的字符。
  • HTML 和纯文本—当您使用这两种格式在一封邮件中发送相同内容时,收件人的电子邮件客户端会基于其功能决定显示哪种格式。

如果您要向大量收件人发送一封电子邮件,合理的做法是同时采用 HTML 和文本进行发送。有些收件人拥有支持 HTML 的电子邮件客户端,因此他们可以单击邮件中的嵌式超链接。使用基于文本的电子邮件客户端的收件人将需要您包含他们可以复制并使用 Web 浏览器打开的 URL。

使用 AWS 开发工具包以编程方式通过 Amazon SES 发送电子邮件

创建共享凭证文件

以下过程演示如何在主目录中创建一个共享凭证文件。若要让软件开发工具包示例代码正常运行,您必须创建此文件。

  1. 在文本编辑器中,创建一个新文件。在此文件中,粘贴以下代码:
    [default]
    aws_access_key_id = YOUR_AWS_ACCESS_KEY_ID
    aws_secret_access_key = YOUR_AWS_SECRET_ACCESS_KEY
  2. 在刚刚创建的文本文件中,将YOUR_AWS_ACCESS_KEY替换为您的唯一 AWS 访问密钥 ID,并将其替换为YOUR_AWS_SECRET_ACCESS_KEY使用您的唯一 AWS 私有访问密钥。
  3. 保存文件。下表显示了您的操作系统的正确位置和文件名。
    如果您使用的是… 将文件另存为…
    Windows C:\Users\<yourUserName>\.aws\credentials
    Linux、macOS 或 Unix ~/.aws/credentials
    重要

    请勿在保存凭证文件时包含文件扩展名。

验证电子邮件地址

您可以使用 Amazon SES 控制台、VerifyEmailIdentityAmazon SES API v1 操作,或CreateEmailIdentityAmazon SES API v2 操作。为了增强功能,我们建议使用 Amazon SES API v2。

 

使用 Amazon SES 控制台来验证电子邮件地址

完成本节中的过程以使用 Amazon SES 控制台来验证电子邮件地址。

 

使用 Amazon SES 控制台来验证电子邮件地址

  1. 登录到。AWS Management Console并打开 Amazon SES 控制台https://console.aws.amazon.com/ses/
  2. 在控制台中,使用区域选择器来选择要验证其中的电子邮件地址的 AWS 区域,如下图所示。
    Amazon SES 发邮件, PHP 开发Amazon SES, AWS适用于 PHP 的开发工具包, Amazon SES 设置电子邮件, Amazon Simple Email Service
    Amazon SES 发邮件, PHP 开发Amazon SES, AWS适用于 PHP 的开发工具包, Amazon SES 设置电子邮件, Amazon Simple Email Service
    注意

    要验证在多个区域中使用的电子邮件地址,请为每个区域重复执行本节中的过程。

  3. 在导航窗格中的 Identity Management 下,选择 Email Addresses
  4. 选择 Verify a New Email Address
  5. 验证新电子邮件地址对话框中,在电子邮件地址字段中键入您的电子邮件地址,然后选择验证此电子邮件地址
  6. 检查您正在验证的电子邮件地址的收件箱。您将收到一条包含以下主题行的消息:“Amazon Web Services-区域中的电子邮件地址验证请求RegionName,” 其中,RegionName是您在第 2 步中选择的 AWS 区域的名称。单击消息中的链接。
    注意

    验证邮件中的链接将在发送该邮件 24 小时后过期。在您收到验证电子邮件 24 小时后,重复步骤 1–5 可收到具有有效链接的验证电子邮件。

  7. 在 Amazon SES 控制台中,身份管理中,选择电子邮件地址。在电子邮件地址列表中,找到您要验证的电子邮件地址。如果已验证此电子邮件地址,则状态列中的值为“已验证”。

 

使用 Amazon SES API 来验证电子邮件地址

您可以使用VerifyEmailIdentityAmazon SES API v1 操作或CreateEmailIdentityAmazon SES API v2 操作来创建新的电子邮件身份。为个人身份调用此操作的简单方法是使用 AWS CLI。

要在验证电子邮件身份时设置默认配置集,请参阅管理 Amazon SES 默认配置集

注意

在完成此部分中的过程之前,必须安装和配置 AWS CLI。有关更多信息,请参阅 AWS Command Line Interface 用户指南

 

使用 AWS CLI 验证电子邮件地址

  1. 在命令行中,输入以下命令来使用VerifyEmailIdentityAmazon SES API v1 操作:
    aws ses verify-email-identity --email-address sender@example.com

    在上述命令中,将 sender@example.com 替换为您要验证的电子邮件地址。

    在命令行中,输入以下命令来使用CreateEmailIdentityAmazon SES API v2 操作:

    aws sesv2 create-email-identity --email-identity sender@example.com

    在上述命令中,将 sender@example.com 替换为您要验证的电子邮件地址。

    如果该命令成功执行,它将退出并且不提供任何输出。

  2. 检查您正在验证的电子邮件地址的收件箱。您将收到一条包含以下主题行的消息:“Amazon Web Services-区域中的电子邮件地址验证请求RegionName,” 其中,RegionName是您尝试验证其中电子邮件地址的 AWS 区域的名称。打开该邮件,然后单击其中的链接。
    注意

    验证邮件中的链接将在发送该邮件 24 小时后过期。在您收到验证电子邮件 24 小时后,重复步骤 1–5 可收到具有有效链接的验证电子邮件。

  3. 在 Amazon SES 控制台中,身份管理中,选择电子邮件地址。在电子邮件地址列表中,找到您要验证的电子邮件地址。如果已验证此电子邮件地址,则状态列中的值为“已验证”。

有关可用于在单个操作中验证多个电子邮件身份的脚本,请参阅验证多个电子邮件地址

在 Amazon SES 中删除电子邮件身份

如果您不再需要使用已验证的电子邮件地址,可以使用 Amazon SES 控制台或DeleteIdentityAPI 操作。

警告

此操作无法撤消。但是,您可以为之前删除的身份重复执行验证过程。

 

在 Amazon SES 中删除电子邮件身份

 

删除已验证的电子邮件地址

  1. 登录到。AWS Management Console并打开 Amazon SES 控制台https://console.aws.amazon.com/ses/
  2. 在控制台中,使用区域选择器来选择要删除其中的电子邮件身份的 AWS 区域,如下图所示。
    Amazon SES 发邮件, PHP 开发Amazon SES, AWS适用于 PHP 的开发工具包, Amazon SES 设置电子邮件, Amazon Simple Email Service
    Amazon SES 发邮件, PHP 开发Amazon SES, AWS适用于 PHP 的开发工具包, Amazon SES 设置电子邮件, Amazon Simple Email Service
    注意

    这些过程仅删除所选 AWS 区域中的电子邮件地址。要删除在多个区域中验证的电子邮件地址,请为每个区域重复执行本节中的过程。

  3. 选择要删除的每个电子邮件地址,然后选择删除

 

使用 Amazon SES API 删除电子邮件身份

使用 DeleteIdentity API 操作来删除电子邮件地址和域身份。

要使用 AWS CLI 删除身份,请在命令行键入以下命令:aws ses delete-identity –identity “sender@example.com

将上述命令中的 sender@example.com 替换为您要删除的身份。

 

重要

可使用共享凭证文件来传递 AWS 钥匙 ID 和秘密钥钥钥钥钥钥钥钥钥钥钥钥钥钥钥钥钥钥钥 作为使用共享凭证文件的替代方案 AWS 您可以通过设置两个环境变量(AWS_ACCESS_KEY_IDAWS_SECRET_ACCESS_KEY,分别)。除非您使用下列方法之一指定凭证,否则此示例无法正常运行。

 

Python 实例:

import boto3
from botocore.exceptions import ClientError

# Replace sender@example.com with your "From" address.
# This address must be verified with Amazon SES.
SENDER = "Sender Name <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. If you do not want to use a configuration
# set, comment the following variable, and the 
# ConfigurationSetName=CONFIGURATION_SET argument below.
CONFIGURATION_SET = "ConfigSet"

# If necessary, replace us-west-2 with the AWS Region you're using for Amazon SES.
AWS_REGION = "us-west-2"

# The subject line for the email.
SUBJECT = "Amazon SES Test (SDK for Python)"

# The email body for recipients with non-HTML email clients.
BODY_TEXT = ("Amazon SES Test (Python)\r\n"
             "This email was sent with Amazon SES using the "
             "AWS SDK for Python (Boto)."
            )
            
# The HTML body of the email.
BODY_HTML = """<html>
<head></head>
<body>
  <h1>Amazon SES Test (SDK for Python)</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-python/'>
      AWS SDK for Python (Boto)</a>.</p>
</body>
</html>
            """            

# The character encoding for the email.
CHARSET = "UTF-8"

# Create a new SES resource and specify a region.
client = boto3.client('ses',region_name=AWS_REGION)

# Try to send the email.
try:
    #Provide the contents of the email.
    response = client.send_email(
        Destination={
            'ToAddresses': [
                RECIPIENT,
            ],
        },
        Message={
            'Body': {
                'Html': {
                    'Charset': CHARSET,
                    'Data': BODY_HTML,
                },
                'Text': {
                    'Charset': CHARSET,
                    'Data': BODY_TEXT,
                },
            },
            'Subject': {
                'Charset': CHARSET,
                'Data': SUBJECT,
            },
        },
        Source=SENDER,
        # If you are not using a configuration set, comment or delete the
        # following line
        ConfigurationSetName=CONFIGURATION_SET,
    )
# Display an error if something goes wrong.	
except ClientError as e:
    print(e.response['Error']['Message'])
else:
    print("Email sent! Message ID:"),
    print(response['MessageId'])

 

JavaScript 实例:

'use strict';

var aws = require('aws-sdk');

// Provide the full path to your config.json file. 
aws.config.loadFromPath('./config.json');

// Replace sender@example.com with your "From" address.
// This address must be verified with Amazon SES.
const sender = "Sender Name <sender@example.com>";

// Replace recipient@example.com with a "To" address. If your account 
// is still in the sandbox, this address must be verified.
const recipient = "recipient@example.com";

// Specify a configuration set. If you do not want to use a configuration
// set, comment the following variable, and the 
// ConfigurationSetName : configuration_set argument below.
const configuration_set = "ConfigSet";

// The subject line for the email.
const subject = "Amazon SES Test (AWS SDK for JavaScript in Node.js)";

// The email body for recipients with non-HTML email clients.
const body_text = "Amazon SES Test (SDK for JavaScript in Node.js)\r\n"
                + "This email was sent with Amazon SES using the "
                + "AWS SDK for JavaScript in Node.js.";
            
// The HTML body of the email.
const body_html = `<html>
<head></head>
<body>
  <h1>Amazon SES Test (SDK for JavaScript in Node.js)</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-node-js/'>
      AWS SDK for JavaScript in Node.js</a>.</p>
</body>
</html>`;

// The character encoding for the email.
const charset = "UTF-8";

// Create a new SES object. 
var ses = new aws.SES();

// Specify the parameters to pass to the API.
var params = { 
  Source: sender, 
  Destination: { 
    ToAddresses: [
      recipient 
    ],
  },
  Message: {
    Subject: {
      Data: subject,
      Charset: charset
    },
    Body: {
      Text: {
        Data: body_text,
        Charset: charset 
      },
      Html: {
        Data: body_html,
        Charset: charset
      }
    }
  },
  ConfigurationSetName: configuration_set
};

//Try to send the email.
ses.sendEmail(params, function(err, data) {
  // If something goes wrong, print an error message.
  if(err) {
    console.log(err.message);
  } else {
    console.log("Email sent! Message ID: ", data.MessageId);
  }
});

 

PHP 实例:

<?php

// If necessary, modify the path in the require statement below to refer to the 
// location of your Composer autoload.php file.
require 'vendor/autoload.php';

use Aws\Ses\SesClient;
use Aws\Exception\AwsException;

// Create an SesClient. Change the value of the region parameter if you're 
// using an AWS Region other than US West (Oregon). Change the value of the
// profile parameter if you want to use a profile in your credentials file
// other than the default.
$SesClient = new SesClient([
    'profile' => 'default',
    'version' => '2010-12-01',
    'region'  => 'us-west-2'
]);

// Replace sender@example.com with your "From" address.
// This address must be verified with Amazon SES.
$sender_email = 'sender@example.com';

// Replace these sample addresses with the addresses of your recipients. If
// your account is still in the sandbox, these addresses must be verified.
$recipient_emails = ['recipient1@example.com','recipient2@example.com'];

// Specify a configuration set. If you do not want to use a configuration
// set, comment the following variable, and the
// 'ConfigurationSetName' => $configuration_set argument below.
$configuration_set = 'ConfigSet';

$subject = 'Amazon SES test (AWS SDK for PHP)';
$plaintext_body = 'This email was sent with Amazon SES using the AWS SDK for PHP.' ;
$html_body =  '<h1>AWS Amazon Simple Email Service Test Email</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-php/">'.
              'AWS SDK for PHP</a>.</p>';
$char_set = 'UTF-8';

try {
    $result = $SesClient->sendEmail([
        'Destination' => [
            'ToAddresses' => $recipient_emails,
        ],
        'ReplyToAddresses' => [$sender_email],
        'Source' => $sender_email,
        'Message' => [
          'Body' => [
              'Html' => [
                  'Charset' => $char_set,
                  'Data' => $html_body,
              ],
              'Text' => [
                  'Charset' => $char_set,
                  'Data' => $plaintext_body,
              ],
          ],
          'Subject' => [
              'Charset' => $char_set,
              'Data' => $subject,
          ],
        ],
        // If you aren't using a configuration set, comment or delete the
        // following line
        'ConfigurationSetName' => $configuration_set,
    ]);
    $messageId = $result['MessageId'];
    echo("Email sent! Message ID: $messageId"."\n");
} catch (AwsException $e) {
    // output error message if fails
    echo $e->getMessage();
    echo("The email was not sent. Error message: ".$e->getAwsErrorMessage()."\n");
    echo "\n";
}

 

更多参考:

 

本文:Amazon SES 发邮件, PHP 开发Amazon SES, AWS适用于 PHP 的开发工具包, Amazon SES 设置电子邮件, Amazon Simple Email Service

Loading

Add a Comment

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.