我无法通过 AWS SES 发送带有主题的电子邮件

时间:2021-01-27 12:31:22

标签: python email aws-lambda mime amazon-ses

我想创建一个程序,该程序从网站中提取数据,将其可视化为图表,然后发送附有该数据的电子邮件。 我可以在没有主题的情况下发送它,但我真的想添加一个主题。 我认为 msg = MIMEMultipart() 可能有问题。

# Add the text and HTML parts to the child container.
    msg_body.attach(htmlpart)

    # msg = MIMEMultipart()  ←←←←←←←←←←←←←←←←Here!
    client = boto3.client('ses', region_name=REGION)

但是,如果我将其注释掉,则会出现此错误:

[ERROR] AttributeError: 'list' object has no attribute 'encode'
Traceback (most recent call last):
  File "/var/task/lambda_function.py", line 193, in lambda_handler
    r = send_email(SRC_MAIL, DST_MAIL, title, message, "/tmp/img.png")
  File "/var/task/lambda_function.py", line 138, in send_email
    'Data':msg.as_string(),
  File "/var/lang/lib/python3.8/email/message.py", line 158, in as_string
    g.flatten(self, unixfrom=unixfrom)
  File "/var/lang/lib/python3.8/email/generator.py", line 116, in flatten
    self._write(msg)
  File "/var/lang/lib/python3.8/email/generator.py", line 195, in _write
    self._write_headers(msg)
  File "/var/lang/lib/python3.8/email/generator.py", line 222, in _write_headers
    self.write(self.policy.fold(h, v))
  File "/var/lang/lib/python3.8/email/_policybase.py", line 326, in fold
    return self._fold(name, value, sanitize=True)
  File "/var/lang/lib/python3.8/email/_policybase.py", line 369, in _fold
    parts.append(h.encode(linesep=self.linesep, maxlinelen=maxlinelen))END 
import json
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
import boto3
import os
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.application import MIMEApplication
import pandas as pd
import requests
import io
import datetime
from datetime import timedelta
import locale


SRC_MAIL = "aaa@a.co.jp"
DST_MAIL = ["aaa@a.co.jp"]
REGION = "ap-northeast-1"


def make_email_html():
    result = "<h1>this is h1</h1>"
    return result


def send_email(source, to, subject, body, img_absolute_path):
    # The character encoding for the email.
    CHARSET = "utf-8"
    # Create a multipart/mixed parent container.
    msg = MIMEMultipart('mixed')
    # Add subject, from and to lines.
    msg['Subject'] = subject
    msg['From'] = source
    msg['To'] = to
    
    # 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.

    s = make_email_html()
    htmlpart = MIMEText(s.encode(CHARSET), 'html', CHARSET)
 
    # Add the text and HTML parts to the child container.
    msg_body.attach(htmlpart)

    # msg = MIMEMultipart()
    client = boto3.client('ses', region_name=REGION)
    
    att = MIMEApplication(open(img_absolute_path, 'rb').read())
    att.add_header('Content-Disposition','attachment',filename=os.path.basename(img_absolute_path))
    
    # 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)
    
    response = client.send_raw_email(
        Source=source,
        Destinations=to,
        RawMessage={
            'Data':msg.as_string(),
        }
    )
    
    return response

def lambda_handler(event, context):
    title = 'this is title'
    message = json.dumps(event, indent=4)
    r = send_email(SRC_MAIL, DST_MAIL, title, message, "/tmp/img.png")
    
    return r

0 个答案:

没有答案