33"""Send the contents of a directory as a MIME message."""
44
55import os
6- import sys
76import smtplib
87# For guessing MIME type based on file name extension
98import mimetypes
109
1110from argparse import ArgumentParser
1211
13- from email import encoders
14- from email .message import Message
15- from email .mime .audio import MIMEAudio
16- from email .mime .base import MIMEBase
17- from email .mime .image import MIMEImage
18- from email .mime .multipart import MIMEMultipart
19- from email .mime .text import MIMEText
20-
21- COMMASPACE = ', '
12+ from email .message import EmailMessage
13+ from email .policy import SMTP
2214
2315
2416def main ():
@@ -47,12 +39,12 @@ def main():
4739 directory = args .directory
4840 if not directory :
4941 directory = '.'
50- # Create the enclosing (outer) message
51- outer = MIMEMultipart ()
52- outer ['Subject' ] = 'Contents of directory %s' % os .path .abspath (directory )
53- outer ['To' ] = COMMASPACE .join (args .recipients )
54- outer ['From' ] = args .sender
55- outer .preamble = 'You will not see this in a MIME-aware mail reader.\n '
42+ # Create the message
43+ msg = EmailMessage ()
44+ msg ['Subject' ] = 'Contents of directory %s' % os .path .abspath (directory )
45+ msg ['To' ] = ', ' .join (args .recipients )
46+ msg ['From' ] = args .sender
47+ msg .preamble = 'You will not see this in a MIME-aware mail reader.\n '
5648
5749 for filename in os .listdir (directory ):
5850 path = os .path .join (directory , filename )
@@ -67,33 +59,18 @@ def main():
6759 # use a generic bag-of-bits type.
6860 ctype = 'application/octet-stream'
6961 maintype , subtype = ctype .split ('/' , 1 )
70- if maintype == 'text' :
71- with open (path ) as fp :
72- # Note: we should handle calculating the charset
73- msg = MIMEText (fp .read (), _subtype = subtype )
74- elif maintype == 'image' :
75- with open (path , 'rb' ) as fp :
76- msg = MIMEImage (fp .read (), _subtype = subtype )
77- elif maintype == 'audio' :
78- with open (path , 'rb' ) as fp :
79- msg = MIMEAudio (fp .read (), _subtype = subtype )
80- else :
81- with open (path , 'rb' ) as fp :
82- msg = MIMEBase (maintype , subtype )
83- msg .set_payload (fp .read ())
84- # Encode the payload using Base64
85- encoders .encode_base64 (msg )
86- # Set the filename parameter
87- msg .add_header ('Content-Disposition' , 'attachment' , filename = filename )
88- outer .attach (msg )
62+ with open (path , 'rb' ) as fp :
63+ msg .add_attachment (fp .read (),
64+ maintype = maintype ,
65+ subtype = subtype ,
66+ filename = filename )
8967 # Now send or store the message
90- composed = outer .as_string ()
9168 if args .output :
92- with open (args .output , 'w ' ) as fp :
93- fp .write (composed )
69+ with open (args .output , 'wb ' ) as fp :
70+ fp .write (msg . as_bytes ( policy = SMTP ) )
9471 else :
9572 with smtplib .SMTP ('localhost' ) as s :
96- s .sendmail ( args . sender , args . recipients , composed )
73+ s .send_message ( msg )
9774
9875
9976if __name__ == '__main__' :
0 commit comments