"Effect": "Allow",
"Action": [
"ses:SendEmail",
"ses:SendRawEmail"
],
"Resource": "*"
以下是一个使用Python的AWS Lambda连接到SES SMTP服务器发送邮件的示例代码:
import os
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.application import MIMEApplication
def send_email(event, context):
try:
# SMTP邮件参数
smtp_host = 'email-smtp.us-west-2.amazonaws.com'
smtp_port = 587
smtp_username = '[your-smtp-username-here]'
smtp_password = '[your-smtp-password-here]'
# 邮件内容参数
sender = '[your-sender-email-address-here]'
recipient = '[your-recipient-email-address-here]'
subject = 'Test Email from AWS Lambda'
body_text = 'This email was sent from an AWS Lambda function.'
# 创建邮件对象
msg = MIMEMultipart()
msg['Subject'] = subject
msg['From'] = sender
msg['To'] = recipient
# 添加邮件正文
body = MIMEText(body_text)
msg.attach(body)
# 添加附件
attachment = MIMEApplication(open('test.pdf', 'rb').read())
attachment.add_header('Content-Disposition', 'attachment', filename='test.pdf')
msg.attach(attachment)
# 创建SMTP客户端
client = smtplib.SMTP(smtp_host, smtp_port)
client.ehlo()
client.starttls()
client.login(smtp_username, smtp_password)
# 发送邮件
client.sendmail(sender, recipient, msg.as_string())
client.close()