import os
import smtplib
import imghdr
from email.message import EmailMessage
EMAIL_ID = os.environ.get('USER_ID') #Use the Email Id as an environment variable
EMAIL_PASSWORD = os.environ.get('USER_PASS') #Use the Email password as an environment variable
msg = EmailMessage()
msg['Subject'] = 'Sharing Img as an attachment!'
msg['From'] = EMAIL_ID
msg['To'] = 'receiver@gmail.com'
msg.set_content("""
Please find attached,
✌ 🙌 🤘
""")
files = ['pic1.jpg','pic2.jpg','pic3.jpg','pic4.jpg'] #Use same directory in which .py is saved, or give the full path of image
for file in files:
with open(file,'rb') as x:
data = x.read()
type = imghdr.what(x.name)
name = x.name
msg.add_attachment(data, maintype='image', subtype=type, filename=name)
with smtplib.SMTP_SSL('smtp.gmail.com', 465) as smtp:
smtp.login(EMAIL_ID, EMAIL_PASSWORD)
smtp.send_message(msg)