I'm struggling with this:
I'm writing a script that looks at the image (jpg) files in a directory.
Then it should add those images to an email....
so far, I have some code (see below) for 2 images, or 3 or 5 or 10 if you want, but it's manually done and I don't always know how many files there will be in the directory
Code: Select all
path = "I:/raspberry/scripts python/email templates/images/" image1 = 'foto_1.jpg' image2 = 'foto_2.jpg' image_name1 = path + image1 image_name2 = path + image2 # This example assumes the image1 is in the directory 'path' fp = open(image_name1, 'rb') msgImage = MIMEImage(fp.read(), _subtype='jpg') # Define the image1's ID as referenced below msgImage.add_header('Content-ID', '<' + image1 + '>') msgRoot.attach(msgImage) # This example assumes the image2 is in the directory 'path' fp = open(image_name2, 'rb') msgImage = MIMEImage(fp.read(), _subtype='jpg') fp.close() # Define the image2's ID as referenced below msgImage.add_header('Content-ID', '<' + image2 + '>') msgRoot.attach(msgImage) # We reference the image in the IMG SRC attribute by the ID we gave it above msgText = MIMEText('<b>Some <i>HTML</i> text</b> and an image.<br><img src="cid:' + image1 + '"><br><img src="cid:' + image2 + '"><br>Nifty!', 'html') msgAlternative.attach(msgText)
I thnk I have to somehow go over names of the files in the directory and assign those to image_name1, image_name2 ,.....
I also think it has something to do with lists, but I'm not sure how to handle this to place the image_name variables in the one line of code.
I'm learning by try and error, assisted by Google and.... you guys from the forum!!