0

The code below sends an email with boto3 through SES with 1 attachment. How can I change the code to send 2 email *.csv attachments.

mail.py

client = boto3.client('ses',region_name='us-east-1')
MESSAGE = 'From: '+SENDER+'\n'
MESSAGE = MESSAGE + 'To: '+RECIPIENT+'\n'
MESSAGE = MESSAGE + 'Subject: '+SUBJECT+'\n'
MESSAGE = MESSAGE + 'MIME-Version: 1.0\n'
MESSAGE = MESSAGE + 'Content-type: Multipart/Mixed; boundary="NextPart"\n\n'
MESSAGE = MESSAGE + '--NextPart\nContent-Type: text/plain\n\n'+BODY_TEXT+'\n\n'
MESSAGE = MESSAGE + '--NextPart\nContent-Type: text/plain;\n'
MESSAGE = MESSAGE + 'Content-Disposition: attachment; filename="attachment.txt"\n\n'
MESSAGE = MESSAGE + 'This is the text in the attachment.\n\n'
MESSAGE = MESSAGE + '--NextPart--\n'
MESSAGE = MESSAGE + 'Content-type: Multipart/Mixed; boundary="NextPart"\n\n'
MESSAGE = MESSAGE + '--NextPart\n'
MESSAGE = MESSAGE + 'Content-Type: text/plain\n\n'
MESSAGE = MESSAGE + BODY_TEXT+'\n\n'
MESSAGE = MESSAGE + '--NextPart\n'
MESSAGE = MESSAGE + 'Content-Type: text/plain;\n'
MESSAGE = MESSAGE + 'Content-Disposition: attachment; filename="attachment.txt"\n\n'
MESSAGE = MESSAGE + 'This is the text in the attachment.\n\n--NextPart--'

RAW_MESSAGE = {
    'Data': MESSAGE
}
response = client.send_raw_email(
    Destinations=[RECIPIENT,],
    RawMessage=RAW_MESSAGE,
    Source=SENDER
)
John Rotenstein
  • 241,921
  • 22
  • 380
  • 470
Victor 'Chris' Cabral
  • 2,135
  • 1
  • 16
  • 33
  • You just repeat what you have done for the first attachment. ;-) – mootmoot Jan 23 '18 at 15:21
  • I literally tried to get the syntax right but my emails would only show up with one attachment. – Victor 'Chris' Cabral Jan 23 '18 at 15:47
  • IMHO, the text construction is quite confusing to debug. You can make use of the answer here, which is lean and clean : https://stackoverflow.com/questions/3362600/how-to-send-email-attachments – mootmoot Jan 23 '18 at 16:52
  • Here is the example of using SMTPlib together with SES. – mootmoot Jan 23 '18 at 16:55
  • @mootmoot I agree that it is difficult to read. Boto3 requires raw message for emails with attachments. If you were to combine both approaches to make a clean example of multiple attachment emails in boto what would it look like? – Victor 'Chris' Cabral Jan 23 '18 at 17:04
  • sorry , this is the missing SES example https://gist.github.com/ruanbekker/a0a119faee3f7493262a8c937ae0c33f – mootmoot Jan 23 '18 at 17:11
  • I found examples here are more comprehensive http://blog.vero4ka.info/blog/2016/10/26/how-to-send-an-email-with-attachment-via-amazon-ses-in-python/ – mootmoot Jan 23 '18 at 17:14
  • @mootmoot I can put the whole answer together now that I have boto3 working with multiple attachments (combining the MIMEText with boto3's send_raw_email method). Let me know if you want to answer the question formally. – Victor 'Chris' Cabral Jan 23 '18 at 19:41

0 Answers0