Spaces:
Build error
Build error
Update app.py
Browse files247 unmodified lines248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303247 unmodified linesdef send_email_with_attachment(recipient_email, subject, body, image_data):sender_email = os.getenv("EMAIL_SENDER")sender_password = os.getenv("EMAIL_PASSWORD")# Create a multipart messagemsg = MIMEMultipart()msg['From'] = sender_emailmsg['To'] = recipient_emailmsg['Subject'] = subject# Attach the body with the msg instancemsg.attach(MIMEText(body, 'plain'))"""# Open the file to be sentattachment = open(attachment_path, "rb")print("Attached the image")# Instance of MIMEBase and named as ppart = MIMEBase('application', 'octet-stream')# To change the payload into encoded formpart.set_payload((attachment).read())# Encode into base64encoders.encode_base64(part)part.add_header('Content-Disposition', f"attachment; filename= {attachment_path}")# Attach the instance 'part' to instance 'msg'msg.attach(part)"""# Create a MIMEBase instance for the attachmentpart = MIMEBase('application', 'octet-stream')# Set the payload with the image datapart.set_payload(image_data)# Encode the payload into base64encoders.encode_base64(part)# Add a header with the filename (you can set any filename you prefer)part.add_header('Content-Disposition', 'attachment; filename="image.png"')# Attach the instance 'part' to the instance 'msg'msg.attach(part)# Create SMTP session for sending the mailserver = smtplib.SMTP('smtp.gmail.com', 587)server.starttls()server.login(sender_email, sender_password)text = msg.as_string()server.sendmail(sender_email, recipient_email, text)server.quit()# return 1247 unmodified lines248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308247 unmodified linesdef send_email_with_attachment(recipient_email, subject, body, image_data):try:sender_email = os.getenv("EMAIL_SENDER")sender_password = os.getenv("EMAIL_PASSWORD")# Create a multipart messagemsg = MIMEMultipart()msg['From'] = sender_emailmsg['To'] = recipient_emailmsg['Subject'] = subject# Attach the body with the msg instancemsg.attach(MIMEText(body, 'plain'))"""# Open the file to be sentattachment = open(attachment_path, "rb")print("Attached the image")# Instance of MIMEBase and named as ppart = MIMEBase('application', 'octet-stream')# To change the payload into encoded formpart.set_payload((attachment).read())# Encode into base64encoders.encode_base64(part)part.add_header('Content-Disposition', f"attachment; filename= {attachment_path}")# Attach the instance 'part' to instance 'msg'msg.attach(part)"""# Create a MIMEBase instance for the attachmentpart = MIMEBase('application', 'octet-stream')# Set the payload with the image datapart.set_payload(image_data)# Encode the payload into base64encoders.encode_base64(part)# Add a header with the filename (you can set any filename you prefer)part.add_header('Content-Disposition', 'attachment; filename="image.png"')# Attach the instance 'part' to the instance 'msg'msg.attach(part)# Create SMTP session for sending the mailserver = smtplib.SMTP('smtp.gmail.com', 587)server.starttls()server.login(sender_email, sender_password)text = msg.as_string()server.sendmail(sender_email, recipient_email, text)server.quit()except error:print error# return 1
app.py+55-50