如何将输出作为文件发送到附件中

时间:2019-05-31 01:31:20

标签: python boto3 amazon-ses billing

我在代码中缺少某些内容,因此无法修复它。虽然当我打印输出文件。这是绝对正确的,但是现在我需要通过电子邮件发送输出。可能有人请帮助我。出于安全原因,此处未提及电子邮件地址,但往返工作正常。我试图从其他地方使用ses代码,所以这就是我无法修复的原因。我只尝试使用boto3,ses,cost Explorer API

      print('\t'.join(['TimePeriod', 'LinkedAccount', 'Service', 'Amount', 'Unit', 'Estimated']))
      for result_by_time in results:
        for group in result_by_time['Groups']:
          amount = group['Metrics']['UnblendedCost']['Amount']
          unit = group['Metrics']['UnblendedCost']['Unit']
          print(result_by_time['TimePeriod']['Start'], '\t', '\t'.join(group['Keys']), '\t', amount, '\t', unit, '\t', result_by_time['Estimated'])


      send_email(start, result_by_time)

def send_email(start, attachment):
    msg = MIMEMultipart()
    msg['From']  = ""
    msg['To'] = ""
    msg['Subject'] = "Monthly AWS Cost Breakdown: {}".format(start)

    # what a recipient sees if they don't use an email reader
    msg.preamble = 'Multipart message.\n'

    # the message body
    part = MIMEText('Here is the aws billing data from last month.')
    msg.attach(part)

    # the attachment
    part = MIMEApplication(attachment)
    part.add_header('Content-Disposition', 'attachment', filename="AWS-MonthlyCostProject-{}.tsv".format(start))
    msg.attach(part)

    # Create an AWS Simple Email Service (SES) client
    client = boto3.client('ses')

    try:
        response = client.send_raw_email(
            RawMessage={
                 'Data': msg.as_string(),
            },
            #Source=msg['From'],
            #Destinations=to_emails
        )
    # Display an error if something goes wrong.
    except ClientError as e:
        print(e.response['Error']['Message'])
    else:
        print("Email sent! Message ID:"),
        print(response['ResponseMetadata']['RequestId'])




 "errorMessage": "expected bytes-like object, not dict",
  "errorType": "TypeError",
  "stackTrace": [
    [
      "/var/task/lambda_function.py",
      44,
      "lambda_handler",
      "send_email(start, result_by_time)"
    ],
    [
      "/var/task/lambda_function.py",
      60,
      "send_email",
      "part = MIMEApplication(attachment)"
    ],
    [
      "/var/lang/lib/python3.6/email/mime/application.py",
      37,
      "__init__",
      "_encoder(self)"
    ],
    [
      "/var/lang/lib/python3.6/email/encoders.py",
      32,
      "encode_base64",
      "encdata = str(_bencode(orig), 'ascii')"
    ],
    [
      "/var/lang/lib/python3.6/base64.py",
      527,
      "encodebytes",
      "_input_type_check(s)"
    ],
    [
      "/var/lang/lib/python3.6/base64.py",
      513,
      "_input_type_check",
      "raise TypeError(msg) from err"`enter code here`
    ]
  ]
}

0 个答案:

没有答案