import smtplib import numpy as np from email.mime.text import MIMEText from email.mime.image import MIMEImage from email.mime.multipart import MIMEMultipart from email.utils import formatdate def send_email(email, selected_images): # Email configuration (replace with your SMTP server details) smtp_server = "smtp.gmail.com" smtp_port = 587 # Create the email message msg = MIMEMultipart() msg['Subject'] = 'Selected Image' msg['From'] = os.getenv("sender_email") msg['To'] = email msg['Date'] = formatdate() # Add text to the email text = MIMEText(f""" 今日はブースを訪れてくれてありがとうございました。。  以下は、あなたが生成した{len(selected_images)}枚の画像です。 """,_charset="utf-8") msg.attach(text) print(selected_images) for i,image in enumerate(selected_images): image=np.array(image)[0] print(image) with open(image, 'rb') as f: img_data = f.read() image = MIMEImage(img_data, name=f"image_{i+1}.jpg") msg.attach(image) # Send the email try: with smtplib.SMTP(smtp_server, smtp_port) as server: server.starttls() server.login( os.getenv("sender_email"), os.getenv("sender_password")) server.send_message(msg) return "Email sent successfully!" except smtplib.SMTPAuthenticationError: raise Exception("SMTP Authentication failed. Please check your email and password.") except smtplib.SMTPException as e: raise Exception(f"SMTP error occurred: {str(e)}") except Exception as e: raise Exception(f"Failed to send email: {str(e)}")