I found and extended a script that will scan a directory and then email the pdfs inside it. The problem occurs when I receive the email. The PDFs are arriving as 'noname' files without an extension. I have to download them and manually add the extension to display them. Is there something I am missing here??
import sys
import os
import smtplib
from email import encoders
from email.mime.base import MIMEBase
from email.mime.multipart import MIMEMultipart
COMMASPACE = ', '
def main():
sender = 'jp@voice.com'
password = 'pword'
recipients = 'j@gmail.com'
# Create enclosing (outer) message
outer = MIMEMultipart()
outer['Subject'] = 'TEST EMAIL'
outer['To'] = COMMASPACE.join(recipients)
outer['From'] = sender
outer.preamble = 'You will not see this in a MIMIE aware reader'
# list of attachments
attachments = r'C:/Users/Ace/Desktop/PDFTST/'
attach = os.listdir(attachments)
for file in attach:
try:
with open(attachments + file, 'rb') as fp:
msg = MIMEBase('application', "octet-stream")
msg.set_payload(fp.read())
encoders.encode_base64(msg)
outer.attach(msg)
except:
print("unable to open the attach. Error: ", sys.exc_info()[0])
raise
composed = outer.as_string()
try:
with smtplib.SMTP('smtp.gmail.com', 587) as s:
s.ehlo()
s.starttls()
s.ehlo()
s.login(sender, password)
s.sendmail(sender, recipients, composed)
s.close()
print("email sent!!!!")
except:
print("Unable to send email. Error: ", sys.exc_info()[0])
raise
if __name__ == '__main__':
main()