File size: 1,744 Bytes
064aede
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48

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)}")