亚马逊ses如何发qq_亚马逊ses发送电子邮件

亚马逊ses如何发qq

SES (Simple Email Service) is, as the name suggests, a very simple service to enable programmatic access to sending and receiving emails. Here is a simple step by step guide to do that in Python — using the Console, AWS CLI, AWS Boto3, as well as a simple Vanila Python script.

顾名思义,SES(简单电子邮件服务)是一种非常简单的服务,可通过编程方式访问发送和接收电子邮件。 这是使用Python进行操作的简单分步指南-使用控制台,AWS CLI,AWS Boto3以及简单的Vanila Python脚本。

Choose the one you like and add it to your own application.

选择您喜欢的一个并将其添加到您自己的应用程序中。

先决条件 (Prerequisites)

Of course, the first and foremost requirement is, an AWS account. If you are reading this blog, I am sure you already have it. If not, please go ahead and create one. It is absolutely required for any developer today.

当然,第一个也是最重要的要求是一个AWS账户。 如果您正在阅读此博客,我相信您已经拥有它。 如果没有,请继续创建一个。 今天,任何开发人员都绝对需要它。

Second, you need an email address — different from Gmail. Google does not like us using AWS to generate mails on its behalf. Those mails usually land in spam, or worse, show an ugly security warning. But if you are doing this only for the test, you should not have a problem with that.

其次,您需要一个电子邮件地址-与Gmail不同。 Google不喜欢我们使用AWS代表它生成邮件。 这些邮件通常以垃圾邮件的形式发送,或更糟糕的是,会显示难看的安全警告。 但是,如果您只是为了测试而这样做,那么您应该不会有任何问题。

Below, I will demonstrate everything using the AWS Web Console, as well as AWS Command Line Interface and the Python Boto3 API. The Web Console does not require any installation. But if you are interested in CLI or Boto3 — I strongly recommend doing that, you should get them installed. Check out these links to do that: CLI, Boto3

下面,我将使用AWS Web Console,AWS Command Line Interface和Python Boto3 API演示一切。 Web控制台不需要任何安装。 但是,如果您对CLI或Boto3感兴趣-我强烈建议您这样做,则应该安装它们。 请查看以下链接以执行此操作: CLIBoto3

验证电子邮件地址 (Verify Email Addresses)

SES takes all precautions to make sure you do not misuse it to spam others. Before you send an email to or from an account using the AWS, you need to “verify” those email addresses. Let us start with the AWS Console

SES会采取所有预防措施,以确保您不会滥用它来向其他人发送垃圾邮件。 在使用AWS向账户发送电子邮件或从账户发送电子邮件之前,您需要“验证”这些电子邮件地址。 让我们从AWS控制台开始

AWS控制台(AWS Console)

Login to your AWS account from a web browser, and among the list of services, choose the Simple Email Service (SES). Click on that and you will be taken to the SES Home. In the panel on the left, look for Email Addresses. Click on that. There, you will see a button to “Verify a New Email Address”.

从Web浏览器登录到您的AWS账户,然后在服务列表中,选择简单电子邮件服务(SES)。 单击该按钮,您将被带到SES主页。 在左侧面板中,查找“电子邮件地址”。 点击那个。 在那里,您将看到一个“验证新电子邮件地址”按钮。

There you can provide an email ID that you own, and click to Verify. That will generate a simple email to your address. You just have to click a link in that mail and you are done. Similarly, you can verify all the Email ID’s that you want to. When you refresh the page, you will see those Email ID’s in the list.

您可以在此处提供您拥有的电子邮件ID,然后单击“验证”。 这将生成一封简单的电子邮件到您的地址。 您只需要单击该邮件中的链接即可。 同样,您可以验证所需的所有电子邮件ID。 刷新页面时,您将在列表中看到那些电子邮件ID。

AWS CLI (AWS CLI)

On the CLI, things are absolutely simple. You just need to run a single command and that completes the task for you.

在CLI上,事情绝对简单。 您只需要运行一个命令即可为您完成任务。

aws ses verify-email-identity --email-address name@domain.com

That is all, it will generate the verification mail to your email id and you can click the link to complete the process.

就是这样,它将生成验证邮件到您的电子邮件ID,您可以单击链接来完成该过程。

Boto3 (Boto3)

You can also do it in Python — using the Boto3

您也可以在Python中使用Boto3

import boto3
client = boto3.client('ses')
response = client.verify_email_identity(
    EmailAddress='name@domain.com'
)

Simple again. You create an SES client and invoke the verify_email_identity() method on it. It will generate the same email that can allow you to click a link and verify the email account.

再次简单。 您创建一个SES客户端,并在其上调用verify_email_identity()方法。 它将生成相同的电子邮件,使您可以单击链接并验证电子邮件帐户。

发送电子邮件 (Send Email)

Now sending an email is simple on either of these interfaces. If you like the Web UI you can do it on the Console, else you can do it on the CLI and also on Boto3.

现在,在这两个界面中的任何一个上发送电子邮件都很简单。 如果您喜欢Web UI,则可以在控制台上进行操作,否则可以在CLI和Boto3上进行操作。

AWS ensures that you do not misuse the service to generate spam. For this, you are not allowed to generate an email to or from an address that you have not yet verified as your own. You can raise a request to Exit Sandbox. If this is approved, you will be able to send to any Email ID. But, the From is always restricted to the one you have verified.

AWS确保您不会滥用服务生成垃圾邮件。 为此,不允许您向尚未验证为自己的地址生成电子邮件或从该地址生成电子邮件。 您可以提出退出“沙箱”的请求。 如果这被批准,您将能够发送到任何电子邮件ID。 但是,“发件人”始终仅限于您已验证的发件人。

The From address is frozen — the one you had selected. You can specify your To address. If you have not requested AWS in the SES Sandbox

发件人地址已冻结-您选择的地址。 您可以指定您的收件人地址。 如果您尚未在SES沙箱中请求AWS

AWS控制台 (AWS Console)

On the same page in the AWS Console, Email Addresses, select any email address from the list.

在AWS控制台的同一页面上,电子邮件地址,从列表中选择任何电子邮件地址。

When you select it, a button is enabled — to send a test mail. When you click on that, it will open a modal — that allows you to draft your mail.

选择它后,将启用一个按钮-发送测试邮件。 当您单击它时,它将打开一个模式-使您可以起草邮件。

The From address is not editable — it is the one you chose when you clicked the Sent mail button. You can choose the To address. But again, it has to be within the constraints of SES.

“发件人”地址不可编辑-这是单击“已发送邮件”按钮时选择的地址。 您可以选择“收件人”地址。 但同样,它必须在SES的限制之内。

SES allows two formats for sending mails — Raw and Formatted. Formatted is the simplest. You just fill in the required details in each field and click send. That is all. The email is sent!

SES允许两种格式发送邮件-Raw和Formatted。 格式化是最简单的。 您只需在每个字段中填写所需的详细信息,然后单击发送。 就这些。 电子邮件已发送!

AWS CLI (AWS CLI)

Sending an email from the CLI is just a line of code. All you need is the base command with a chunk of options.

从CLI发送电子邮件只是一行代码。 您需要的只是带有大量选项的基本命令。

aws ses send-email --from sender@domain.com --to receiver@domain.com --text "This is for those who cannot read HTML." --html "<h1>Hello World</h1><p>This is a pretty mail with HTML formatting</p>" --subject "Hello World"

You can have a more complex message text or also specify an HTML message by using the appropriate parameters.

您可以使用更复杂的消息文本,也可以使用适当的参数指定HTML消息。

Boto3 (Boto3)

A Python script can also use the Boto3 to send a similar email. You just need to create an SES client and invoke the appropriate method.

Python脚本也可以使用Boto3发送类似的电子邮件。 您只需要创建一个SES客户端并调用适当的方法。

import boto3
client = boto3.client('ses')
response = client.send_email(
    Destination={
        'ToAddresses': [
            'receiver@domain.com'
        ],
    },
    Message={
        'Body': {
            'Html': {
                'Charset': 'UTF-8',
                'Data': '<h1>Hello World</h1><p>This is a pretty mail with HTML formatting</p>',
            },
            'Text': {
                'Charset': 'UTF-8',
                'Data': 'This is for those who cannot read HTML.',
            },
        },
        'Subject': {
            'Charset': 'UTF-8',
            'Data': 'Hello World',
        },
    },
    Source='sender@domain.com',
)

原始邮件(Raw Mail)

In the above examples, we saw some very basic emails. These can be useful — perhaps for some auto-generated notification mails. But in real life, we need a lot more than that.

在以上示例中,我们看到了一些非常基本的电子邮件。 这些可能很有用-也许对于某些自动生成的通知邮件。 但是在现实生活中,我们不仅需要更多。

For this, we use the Raw mails. Here, we can specify and configure a lot details, that can be required while sending an email. If you want real code, you should get into the habit of using the Raw mails and nothing else.

为此,我们使用原始邮件。 在这里,我们可以指定和配置很多详细信息,这在发送电子邮件时可能是必需的。 如果您想要真正的代码,则应该养成使用Raw邮件的习惯,而别无其他。

Here is a sample code for sending a raw mail.

这是发送原始邮件的示例代码。

import os
import boto3
from botocore.exceptions import ClientError
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.application import MIMEApplication
# Setup the basics here
SENDER = "Sender Name <sender@domain.com>"
RECIPIENT = "recipient@domain.com"
SUBJECT = "Customer service contact info"
# The configuration set used to track mails
CONFIGURATION_SET = "ConfigSet"
# The full path to the file that will be attached to the email.
ATTACHMENT = "path/to/customers-to-contact.xlsx"
# The email body for recipients with non-HTML email clients.
BODY_TEXT = "Hello,\r\nPlease see the attached file for a list of customers to contact."
# The HTML body of the email.
BODY_HTML = """\
<html>
<head></head>
<body>
<h1>Hello!</h1>
<p>Please see the attached file for a list of customers to contact.</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')
# Create an instance of multipart/mixed parent container.
msg = MIMEMultipart('mixed')
# Add subject, from and to lines.
msg['Subject'] = SUBJECT 
msg['From'] = SENDER 
msg['To'] = RECIPIENT
# Create a multipart/alternative child container.
msg_body = MIMEMultipart('alternative')
# Encode the text and HTML content and set the character encoding. This step is
# necessary if you're sending a message with characters outside the ASCII range.
textpart = MIMEText(BODY_TEXT.encode(CHARSET), 'plain', CHARSET)
htmlpart = MIMEText(BODY_HTML.encode(CHARSET), 'html', CHARSET)
# Add the text and HTML parts to the child container.
msg_body.attach(textpart)
msg_body.attach(htmlpart)
# Define the attachment part and encode it using MIMEApplication.
att = MIMEApplication(open(ATTACHMENT, 'rb').read())
# Add a header to tell the email client to treat this part as an attachment,
# and to give the attachment a name.
att.add_header('Content-Disposition','attachment',filename=os.path.basename(ATTACHMENT))
# Attach the multipart/alternative child container to the multipart/mixed
# parent container.
msg.attach(msg_body)
# Add the attachment to the parent container.
msg.attach(att)
#print(msg)
try:
    #Provide the contents of the email.
    response = client.send_raw_email(
        Source=SENDER,
        Destinations=[
            RECIPIENT
        ],
        RawMessage={
            'Data':msg.as_string(),
        },
        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'])

Essentially, it instantiates an object msg, and populates all the relevant fields. Finally this is passed into the send_raw_email() method, that actually sends the mail.

本质上,它实例化一个对象msg,并填充所有相关字段。 最后,将其传递到send_raw_email()方法中,该方法实际发送邮件。

SMTP (SMTP)

Well, all that was bound within the boundaries of AWS. We can also go out of AWS limits and still use this service. SES also provides us a simple SMTP interface to send emails using the SES.

好吧,所有这些都被限制在AWS的范围之内。 我们还可以超出AWS限制,仍然使用此服务。 SES还为我们提供了一个简单的SMTP界面,以使用SES发送电子邮件。

This is simple to configure. On the SES console, click on the SMTP settings on the left panel. That will take you to the SMTP configuration page. Click on the button to create your SMTP user and password. The SMTP credentials that it creates, are simply an IAM user — with specific permissions to invoke email requests.

这很容易配置。 在SES控制台上,单击左侧面板上的SMTP设置。 这将带您进入SMTP配置页面。 单击按钮创建您的SMTP用户和密码。 它创建的SMTP凭据只是一个IAM用户-具有调用电子邮件请求的特定权限。

With the user credentials, you can run the below script to send out simple emails.

使用用户凭据,您可以运行以下脚本来发送简单的电子邮件。

from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import smtplib
smtphost = "email-smtp.us-east-1.amazonaws.com"
# Get the user name and password - from the IAM user that is created by SES.
password = "BFSNXwvnfnBHmz+dqPAXWgYeH3sfdsgvfxKbiUR6NvD7oW"
username = "AKIA4VCC4EEK5EXQOWEFGML"
message = "Test from Python via AuthSMTP"
msg = MIMEMultipart()
msg['From'] = "sender@domain.com"
msg['To'] = "receiver@domain.com"
msg['Subject'] = "Test from Python via AuthSMTP"
msg.attach(MIMEText(message, 'plain'))
server = smtplib.SMTP(smtphost)
server.starttls()
server.login(username, password)
server.sendmail(msg['From'], msg['To'], msg.as_string())
server.quit()

The SMTP server is only a convenience utility provided by AWS. Internally, it just maps an incoming SMTP request into the corresponding SES request and then processes it.

SMTP服务器只是AWS提供的便利工具。 在内部,它只是将传入的SMTP请求映射到相应的SES请求中,然后对其进行处理。

There are several feature rich open source SMTP servers, that you can use to create emails with all the functionality you like. Invoking the SMTP send is absolutely simple, and it can be done using your code running anywhere — not restricted to running within AWS.

有几种功能丰富的开源SMTP服务器,可用于创建具有所有所需功能的电子邮件。 调用SMTP发送非常简单,并且可以使用可在任何地方运行的代码来完成它-不限于在AWS中运行。

Original: https://thewiz.net/aws_ses_to_send_emails__38d1.html

原文: https//thewiz.net/aws_ses_to_send_emails__38d1.html

翻译自: https://medium.com/@vikassolegaonkar/amazon-ses-to-send-emails-58997d932871

亚马逊ses如何发qq

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值