import gradio as gr 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 import os 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)}") examples= ["Test_images/Woman_1.png","Test_images/prompt_support_examples/Man_1.png"] select_list=[] from src.utils import convert_to_pil def update_gallery(img,select_list=select_list): #img=convert_to_pil(img) if select_list is None: select_list=[] select_list.append(img) return select_list def clear_selection(select_list=select_list): if select_list is not None: select_list=[] return select_list def create_mail_tab(): with gr.TabItem("Mail", id="mail"): email_input = gr.Textbox(label="Enter your email:") image_gallery = gr.Gallery(label="Selected Images", show_label=True, elem_id="gallery") with gr.Row(): clear_button = gr.Button("Clear Selection") send_button = gr.Button("Send Email") result_output = gr.Textbox(label="Result") # Examples with gr.Row(): print(examples) examples=np.array(examples) for example in examples: print(example) img=gr.Image(value=example, interactive=False, label=os.path.basename(example),type="filepath") img.select(update_gallery,inputs=img,outputs=image_gallery) # Connect the send button to the send_email function send_button.click( fn=send_email, inputs=[email_input, image_gallery], outputs=result_output ) # Clear the selected images clear_button.click( fn=clear_selection, inputs=None, outputs=image_gallery, )