Iam writing a function to send email with excel attachment. But I am having some issues while calling the function . I am AWS server sending the email. I have pasted my email sending function along with the function call.
Email Function
import os
from email.mime.text import MIMEText
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart
import boto3
from botocore.exceptions import ClientError
EMAIL_FROM = 'abc@gmail.com'
STAGE = os.getenv('STAGE')
ses = boto3.client('ses', 'eu-west-1')
def send(to: list, subject: str, body: str):
try:
ses.send_email(
Destination={
'ToAddresses': to
},
Message={
'Body': {
'Text': {
'Charset': 'UTF-8',
'Data': body,
},
},
'Subject': {
'Charset': 'UTF-8',
'Data': f"[{STAGE}] {subject}",
},
},
Source=EMAIL_FROM
)
except ClientError as e:
raise ValueError(e.response['Error']['Message'])
def send_with_attachment(to: list, subject: str, body: str, attachments: dict):
message = MIMEMultipart()
message['Subject'] = f"[K+N:{STAGE}] {subject}"
message['From'] = EMAIL_FROM
message['To'] = ','.join(to)
part = MIMEText(body)
message.attach(part)
# attachments
for filename, content in attachments.items():
part = MIMEApplication(str.encode(content))
part.add_header('Content-Disposition', 'attachment', filename=filename)
message.attach(part)
try:
ses.send_raw_email(
Source=message['From'],
Destinations=to,
RawMessage={
'Data': message.as_string()
}
)
except ClientError as e:
raise ValueError(e.response['Error']['Message'])
Calling Function
send_with_attachment(
to='xyz@gmail.com',
subject='Report',
body='Test',
attachments={
open('Report.xlsx', 'rb')
}
)
Error : 'set' object has no attribute 'items'
I am having issue while calling the function. Any help on how to resolve the issue.