如何在LoopBack中为AWS SES电子邮件服务配置电子邮件数据源?

时间:2017-10-18 22:03:29

标签: amazon-web-services loopbackjs

LoopBack instructions要求datasources.json文件的transports数组中的条目。

AWS使用nodemailer instructions要求创建对象。

但是你不能在json文件中创建一个JS对象。

到目前为止,这是我的datasource.json文件。如何为AWS SES配置awsEmail数据源?

... 
  "awsEmail": {
    "name": "awsEmail",
    "connector": "mail",
    "transports": [
      {
        "type": "ses",
        "host": "aws.amazon.com",
        "secure": false,
        "port": 587,
        "tls": {
          "rejectUnauthorized": false
        },
        "auth": {
          "user": "my user name",
          "pass": "my password"
        }
      }
    ]
  }
...

3 个答案:

答案 0 :(得分:0)

您是否考虑过使用SMTP进行此操作?以下对我有用。

"awsEmail": {
  "name": "awsEmail",
  "connector": "mail",
  "defaultForType": "mail",
  "transports": [
    {
      "type": "SMTP",
      "host": "email-smtp.us-east-1.amazonaws.com",
      "secure": true,
      "port": 465,
      "auth": {
        "user": "your smtp username",
        "pass": "your smtp password"
      }
    }
  ]
}

答案 1 :(得分:0)

"ses": {
    "name": "ses",
    "connector": "email",
    "transports": [
      {
        "type": "ses",
        "accessKeyId": "AWS accessKeyId",
        "secretAccessKey": "AWS secretAccessKey",
        "rateLimit": 1
      }
    ]
  }

这对我有用。

答案 2 :(得分:-1)

您必须首先通过控制台或cli创建Ses用户,然后在代码中使用该用户。以下是我用于lambda执行的python代码,它将s3中的文件发送到我的电子邮件中。请注意输入corret用户和pwd

ses_user = "AKIXXXXXXXXXXXXXXXXXXXXXX"
ses_pwd = "AiKxxxxxxxxxxxxxxxiiiiiiiiiiiixxxxxx"

def mail(fromadd,to, subject, text, attach):
   msg = MIMEMultipart()
   msg['From'] = fromadd
   msg['To'] = to
   msg['Subject'] = subject
   msg.attach(MIMEText(text))
   part = MIMEBase('application', 'octet-stream')
   part.set_payload(open(attach, 'rb').read())
   Encoders.encode_base64(part)
   part.add_header('Content-Disposition','attachment; filename="%s"' % os.path.basename(attach))
   msg.attach(part)
   mailServer = smtplib.SMTP("email-smtp.us-east-1.amazonaws.com", 587)
   mailServer.ehlo()
   mailServer.starttls()
   mailServer.ehlo()
   mailServer.login(ses_user, ses_pwd)
   mailServer.sendmail(fromadd, to, msg.as_string())
   # Should be mailServer.quit(), but that crashes...
   mailServer.close()

date_fmt = strftime("%Y_%m_%d", gmtime())
#Give your file path
filepath ='/tmp/filename_' + date_fmt + '.csv'
#Give your filename
mail("sender_email","reciepient_email",filepath)
s3.Object('bucket', filename).put(Body=open(filepath, 'rb'))