Yol*_*o49 5 python amazon-ses aws-lambda
我想设置一个 Lambda 函数来解析传入 SES 的电子邮件。我遵循了文档并设置了收据规则。
我通过在 txt 文件中存储 MIME 电子邮件、解析电子邮件并将所需信息存储在要存储在数据库中的 JSON 文档中来测试我的脚本。现在,我不确定如何访问从 SES 收到的电子邮件并将信息提取到我的 Python 脚本中。任何帮助将不胜感激。
from email.parser import Parser
parser = Parser()
f = open('roundtripMime.txt', "r")
rawText = f.read()
incoming = Parser().parsestr(rawText)
subject = incoming
subjectList = subject.split("|")
#Get number
NumberList = subjectList[0].split()
Number = NumberList[2].strip("()")
#Get Name
fullNameList = subjectList[3].split("/")
firstName = fullNameList[1].strip()
lastName = fullNameList[0].strip()
Run Code Online (Sandbox Code Playgroud)
您可以在 SES 规则集中设置一个操作,以自动将您的电子邮件文件放入 S3。然后您在 S3 中设置一个事件(针对特定存储桶)以触发您的 lambda 函数。这样,您就可以使用以下内容检索电子邮件:
def lambda_handler(event, context):
for record in event['Records']:
key = record['s3']['object']['key']
bucket = record['s3']['bucket']['name']
# here you can download the file from s3 with bucket and key
Run Code Online (Sandbox Code Playgroud)
McG*_*ady -2
请参阅Amazon Simple Email Service 文档
其实,还有更好的办法;使用boto3,您可以轻松发送电子邮件和处理消息。
# Get the service resource
sqs = boto3.resource('sqs')
# Get the queue
queue = sqs.get_queue_by_name(QueueName='test')
# Process messages by printing out body and optional author name
for message in queue.receive_messages(MessageAttributeNames=['Author']):
# Get the custom author message attribute if it was set
author_text = ''
if message.message_attributes is not None:
author_name = message.message_attributes.get('Author').get('StringValue')
if author_name:
author_text = ' ({0})'.format(author_name)
# Print out the body and author (if set)
print('Hello, {0}!{1}'.format(message.body, author_text))
# Let the queue know that the message is processed
message.delete()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7887 次 |
| 最近记录: |