Cansada de ser feliz

Bienvenidos a mi flujo de conciencia

How to send an email with attachment via Amazon SES in Python

| Comments

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import xlwt
from boto3 import client

email_from = 'from@example.com'
email_to = 'to@example.com'

# Create a workbook
xls = xlwt.Workbook()
# Create a sheet
sheet = xls.add_sheet('Sheet name')
# Set styles for headers and dates
header_style = xlwt.easyxf('font: bold True;')
date_format = xlwt.XFStyle()
date_format.num_format_str = 'dd/mm/yyyy hh:mm:ss'

# Header
labels = [
    u'Title 1',
    u'Title 2',
]
content = [labels]

# Get data for other rows
for row in get_rows_with_content():
    content.append(list(row))

# Write it down
for row in range(len(content)):
    for col in range(len(content[row])):
        data = content[row][col]
        # If it's a header
        if row == 0:
            sheet.write(row, col, data, header_style)
            continue
        # If it's a date
        if type(data) == datetime.datetime:
            sheet.write(row, col, data, date_format)
        else:
            sheet.write(row, col, data)

# Set filename
today = datetime.date.today()
file_name = 'my_file_{}.xls'.format(today.strftime("%Y-%m-%d"))

# Save the file
output = StringIO.StringIO()
xls.save(output)

# Build an email
msg = MIMEMultipart()
msg['Subject'] = 'Hi, here is your file'
msg['From'] = email_from
msg['To'] = email_to
# What a recipient sees if they don't use an email reader
msg.preamble = 'Multipart message.\n'

# The attachment
part = MIMEApplication(output.getvalue())
part.add_header('Content-Disposition', 'attachment', filename=file_name)
part.add_header('Content-Type', 'application/vnd.ms-excel; charset=UTF-8')
msg.attach(part)

# Connect to Amazon SES
ses = client(
    'ses',
    region_name='us-east-1',
    aws_access_key_id=config['AWS_ACCESS_KEY_ID'],
    aws_secret_access_key=config['AWS_SECRET_ACCESS_KEY'],
)
# And finally, send the email
ses.send_raw_email(
    Source=email_from,
    Destinations=[email_to],
    RawMessage={
        'Data': msg.as_string(),
    }
)

If we need to create a ZIP archive first:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import zipfile
import tempfile
from email import encoders
from email.mime.multipart import MIMEMultipart, MIMEBase

# ....
output = StringIO.StringIO()
xls.save(output)

# Create zip file
zf = tempfile.TemporaryFile(prefix='fileprefix', suffix='.zip')
zip = zipfile.ZipFile(zf, 'w')
zip.writestr(file_name, output.getvalue())
zip.close()
zf.seek(0)

msg = MIMEMultipart()
msg['Subject'] = 'Hi, here is your file'
msg['From'] = email_from
msg['To'] = email_to

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

# the attachment
part = MIMEBase('application', 'zip')
part.set_payload(zf.read())
encoders.encode_base64(part)
part.add_header(
    'Content-Disposition', 'attachment',
    filename=u'{}.zip'.format(file_name),
)
msg.attach(part)
# ....

Comments