亚马逊ses如何发qq_使用Amazon SES发送电子邮件

亚马逊ses如何发qq

by Kangze Huang

黄康泽

使用Amazon SES发送电子邮件 (Sending emails with Amazon SES)

完整的AWS Web样板-教程3 (The Complete AWS Web Boilerplate — Tutorial 3)

目录 (Table of Contents)

Part 0: Introduction to the Complete AWS Web Boilerplate

第0部分: 完整的AWS Web Boilerplate简介

Part 1: User Authentication with AWS Cognito (3 parts)

第1部分: 使用AWS Cognito进行用户身份验证 (三部分)

Part 2: Saving File Storage Costs with Amazon S3 (1 part)

第2部分: 使用Amazon S3节省文件存储成本 (第1部分)

Part 3: Sending Emails with Amazon SES (1 part)

第3部分: 使用Amazon SES发送电子邮件 (第1部分)

Part 4: Manage Users and Permissions with AWS IAM [Coming Soon]

第4部分:使用AWS IAM管理用户和权限[即将推出]

Part 5: Cloud Server Hosting with AWS EC2 and ELB[Coming Soon]

第5部分:使用AWS EC2和ELB托管云服务器[即将推出]

Part 6: The MongoDB Killer: AWS DynamoDB [Coming Soon]

第6部分:MongoDB杀手:: AWS DynamoDB [即将推出]

Part 7: Painless SQL Scaling using AWS RDS [Coming Soon]

第7部分:使用AWS RDS进行无痛SQL扩展[即将推出]

Part 8: Serverless Architecture with Amazon Lambda [Coming Soon]

第8部分:使用Amazon Lambda的无服务器架构[即将推出]

Download the Github here.

此处下载Github。

建立 (Setup)

Sending emails with Amazon SES is really straightforward. Let’s start at the set-up. Go to Amazon SES and click Email Addresses in the sidebar. Then click Verify a New Email Address and enter an email you want to use for messaging in the app.

使用Amazon SES发送电子邮件非常简单。 让我们从设置开始。 转到Amazon SES,然后单击侧边栏中的电子邮件地址 。 然后单击“ 验证新电子邮件地址” ,然后在应用程序中输入要用于发送消息的电子邮件。

Now go to your email provider and click the verification link. After verifying, go back to Amazon SES Email Addresses tab. You should now see your email verified.

现在转到您的电子邮件提供商,然后单击验证链接。 验证后,返回到Amazon SES 电子邮件地址选项卡。 您现在应该看到您的电子邮件已验证。

This was necessary for 2 reasons. First is that we need an email for sending a message, and the second is because we are in a sandbox environment. A sandbox environment means you can only send and receive emails from verified addresses, and prevents you from spamming people. This is all the set-up we need for this boilerplate.

这有两个原因。 首先是我们需要一封电子邮件来发送消息,其次是因为我们处于沙盒环境中。 沙盒环境意味着您只能从经过验证的地址发送和接收电子邮件,并且可以防止向他人发送垃圾邮件。 这就是我们需要的所有样板设置。

If you want be able to send emails to any email address, you need to make a written request to Amazon to graduate from the sandbox environment. To do this, navigate to the top-right hand corner at Support > Support Center.

如果您希望能够将电子邮件发送到任何电子邮件地址,则需要向亚马逊提出书面要求,以便从沙盒环境中毕业。 为此,请导航至“ 支持”gt;上的右上角 支持中心

At this next screen, click Create case.

在下一个屏幕上,点击Create case

This is a straightforward form, but we’ll briefly explain. Select Service Limit Increase and set the Limit Type to SES Sending Limits. Now create 2 requests, one where Limit is Desired Daily Sending Quota (how many emails can be sent in one day), and the other where Limit is Desired Maximum Send Rate. Set the New limit value to the amount that you need. Finally, optionally set the Mail Type as it increases you odds of approval. Use transactional if your emails are generated as a request of a user’s activity. There are others available for other use cases.

这是一种简单的形式,但我们将简要说明。 选择服务限制增加,然后将限制类型设置为SES发送限制 。 现在创建2个请求,一个请求的“ 限制”是“ 每日期望的发送配额” (一天可以发送多少电子邮件),另一个请求的“ 限制”是“ 期望的最大发送速率” 。 将新限制值设置为所需的数量。 最后,可以选择设置“邮件类型”,因为它增加了批准的几率。 如果您的电子邮件是根据用户活动的请求生成的,则使用事务性。 还有其他可供其他用例使用的情况。

The rest of the request is easy. Make sure you agree to comply with the Terms of Service, and you have a process to handle bounces and complaints (for when users mark your email as spam). Finally, give a brief explanation of your use case.

其余的请求很容易。 确保您同意遵守服务条款,并且有一个处理退信和投诉的程序 (当用户将您的电子邮件标记为垃圾邮件时)。 最后,简要说明您的用例。

Submit your code and you should get an email from Amazon with the results of your service increase request. Once approved, your app can send messages to any email.

提交您的代码,您应该从亚马逊收到一封电子邮件,其中包含您的服务增加请求的结果。 获得批准后,您的应用可以将消息发送到任何电子邮件。

代码 (The Code)

We’re ready to dig into the code! Go to App/src/api/aws/aws_ses.js where the bulk of the code resides. Let’s take a look at the main function sendAWSEmail():

我们准备深入研究代码! 转到大部分代码所在的App/src/api/aws/aws_ses.js 。 让我们看一下主要功能sendAWSEmail()

export function sendAWSEmail(email, message){ const ses = new AWS.SES({  region: 'us-east-1' }) const p = new Promise((res, rej)=>{  if(!email|| message){   rej('Missing user email or message content')  }else{   const params = createInquiryParamsConfig(email, message)   // console.log('Sending email with attached params!')   AWS.config.credentials.refresh(function(){    // console.log(AWS.config.credentials)    ses.sendEmail(params, function(err, data) {      if(err){        // console.log(err, err.stack); // an error occurred        rej(err)      }else{       // console.log(data);           // successful response     res('Success! Email sent')      }    })   })  } }) return p}

This is extremely straightforward. We receive two arguments, an email to send to, and a message to be sent. The first thing we do in this function is instantiate the AWS SES object for interacting with AWS by simply passing in the region. Next we check if there is a recipient email and a message. If both are provided, then we can actually send the email.

这非常简单。 我们收到两个参数,即要发送给的电子邮件和要发送的消息。 我们在此功能中要做的第一件事是通过简单地传入区域来实例化AWS SES对象以与AWS进行交互。 接下来,我们检查是否有收件人电子邮件和消息。 如果两者都提供,那么我们实际上可以发送电子邮件。

Assuming we have both a recipient email and message, we will create a params object for AWS SES to read for all the info & options necessary. This params object is created with createInquiryParamsConfig(). Before we dive into that rabbit hole, let’s just quickly finish up explaining the rest of sendAWSEmail(). We refresh AWS Cognito user’s credentials (that we set with AWS Cognito, explained in my other tutorial) and call ses.sendEmail with params and a response callback passed in. Reject the promise if there is an error, and resolve with a success message if there is no error. ses.sendEmail is the only AWS function we will use, and everything else is we need is determined in params.

假设我们同时拥有收件人电子邮件和消息,我们将为AWS SES创建一个params对象,以读取所有必要的信息和选项。 该params对象是使用createInquiryParamsConfig()创建的。 在深入sendAWSEmail()这个兔子洞之前,让我们快速完成其余的sendAWSEmail()解释。 我们刷新AWS Cognito用户的凭证(我们使用AWS Cognito设置的凭证,在我的其他教程中进行了说明 ),并使用ses.sendEmailparams和响应回调调用ses.sendEmail 。没有错误。 ses.sendEmail是我们将要使用的唯一AWS函数,而我们需要的其他一切都在params确定。

Now let’s see how to make params with createInquiryParamsConfig().

现在,让我们看看如何使用createInquiryParamsConfig()进行params设置。

function createInquiryParamsConfig(email, message){ const params = {   Destination: {      BccAddresses: [],     CcAddresses: [],     ToAddresses: [ email ]   },   Message: {      Body: {        Html: {         Data: generateHTMLInquiryEmail(landlordEmail, message),         Charset: 'UTF-8'       }     },     Subject: {        Data: 'Kangzeroos Boilerplate says hi ' + email,       Charset: 'UTF-8'     }   },   Source: 'yourApp@gmail.com',    ReplyToAddresses: [ 'yourApp@gmail.com' ],   ReturnPath: 'yourApp@gmail.com' } return params}

Pretty straightforward, we pass in email and message, and return a big javascript object. All the values you see here are necessary, but you can add a ton of other optional configurations too. The function we must pay attention to is generateHTMLInquiryEmail(). Let’s look at that.

非常简单,我们传递emailmessage ,并返回一个大的javascript对象。 您在此处看到的所有值都是必需的,但您也可以添加大量其他可选配置。 我们必须注意的函数是generateHTMLInquiryEmail() 。 让我们看看。

function generateHTMLInquiryEmail(email, message){ return `  <!DOCTYPE html>  <html>    <head>      <meta charset='UTF-8' />      <title>title</title>    </head>    <body>     <table border='0' cellpadding='0' cellspacing='0' height='100%' width='100%' id='bodyTable'>      <tr>          <td align='center' valign='top'>              <table border='0' cellpadding='20' cellspacing='0' width='600' id='emailContainer'>                  <tr style='background-color:#99ccff;'>                      <td align='center' valign='top'>                          <table border='0' cellpadding='20' cellspacing='0' width='100%' id='emailBody'>                              <tr>                                  <td align='center' valign='top' style='color:#337ab7;'>                                      <h3>${message}</h3>                                  </td>                              </tr>                          </table>                      </td>                  </tr>                  <tr style='background-color:#74a9d8;'>                      <td align='center' valign='top'>                          <table border='0' cellpadding='20' cellspacing='0' width='100%' id='emailReply'>                              <tr style='font-size: 1.2rem'>                                  <td align='center' valign='top'>                                      <span style='color:#286090; font-weight:bold;'>Send From:</span> <br/> ${email}                                  </td>                              </tr>                          </table>                      </td>                  </tr>              </table>          </td>      </tr>      </table>    </body>  </html> `}

All we are doing here is creating an HTML file and passing in the email and message to create a custom email. We use ES6 string literals to add in string variables with ${ } like so: <h3>${message}</h3>.

我们在这里所做的就是创建一个HTML文件,并传入emailmessage以创建自定义电子邮件。 我们使用ES6字符串文字以${ }添加字符串变量,如下所示: <h3>${message } </ h3>。

And that’s it! You can use whatever front end code you want, simply pass in an email and message to sendAWSEmail(). Just remember sendAWSEmail() returns a promise, so you will have to handle that accordingly. If you don’t know how to handle promises, check out my other tutorial here.

就是这样! 您可以使用所需的任何前端代码,只需将emailmessagesendAWSEmail() 。 只要记住sendAWSEmail()返回一个promise,就必须相应地处理它。 如果您不知道如何处理承诺,请在此处查看我的其他教程

See you next time!

下次见!

目录 (Table of Contents)

Part 0: Introduction to the Complete AWS Web Boilerplate

第0部分: 完整的AWS Web Boilerplate简介

Part 1: User Authentication with AWS Cognito (3 parts)

第1部分: 使用AWS Cognito进行用户身份验证 (3部分)

Part 2: Saving File Storage Costs with Amazon S3 (1 part)

第2部分: 使用Amazon S3节省文件存储成本 (第1部分)

Part 3: Sending Emails with Amazon SES (1 part)

第3部分: 使用Amazon SES发送电子邮件 (第1部分)

Part 4: Manage Users and Permissions with AWS IAM [Coming Soon]

第4部分:使用AWS IAM管理用户和权限[即将推出]

Part 5: Cloud Server Hosting with AWS EC2 and ELB[Coming Soon]

第5部分:使用AWS EC2和ELB托管云服务器[即将推出]

Part 6: The MongoDB Killer: AWS DynamoDB [Coming Soon]

第6部分:MongoDB杀手:: AWS DynamoDB [即将推出]

Part 7: Painless SQL Scaling using AWS RDS [Coming Soon]

第7部分:使用AWS RDS进行无痛SQL扩展[即将推出]

Part 8: Serverless Architecture with Amazon Lambda [Coming Soon]

第8部分:使用Amazon Lambda的无服务器架构[即将推出]

This method was partially used in the deployment of renthero.ca

此方法部分用于renthero.ca的部署中

翻译自: https://www.freecodecamp.org/news/sending-emails-with-amazon-ses-7617e83327b6/

亚马逊ses如何发qq

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值