import smtplib
from email.message import EmailMessage

# Crear el mensaje
def send_email(destinatario,asunto, mensaje):
    msg = EmailMessage()
    msg['Subject'] = asunto
    msg['From'] = 'serviciosalcliente@grupovelas.com.mx'
    msg['To'] = destinatario
    msg.add_alternative(mensaje, subtype='html')
    # msg.set_content('Hola, este es un correo enviado con Python 🐍')

    # Conexión al servidor SMTP de Gmail
    with smtplib.SMTP('10.10.202.4', 25) as smtp:
        smtp.ehlo("www.grupovelas.com.mx")  # equivalente a HELO
        smtp.login('serviciosalcliente@grupovelas.com.mx', '5718011')
        smtp.send_message(msg)

    # print(msg)

    # print("Correo enviado correctamente")


# import base64
# from email.mime.text import MIMEText
# from google.auth.transport.requests import Request
# from google_auth_oauthlib.flow import InstalledAppFlow
# from googleapiclient.discovery import build
# from email.mime.text import MIMEText
# import os
# from google.oauth2.credentials import Credentials

# # Alcances mínimos para enviar correos
# SCOPES = ["https://www.googleapis.com/auth/gmail.send"]

# def get_service():
#     creds = None
#     # Guarda el token para no pedir login cada vez
#     if os.path.exists("token.json"):
#         creds = Credentials.from_authorized_user_file("token.json", SCOPES)
#     if not creds or not creds.valid:
#         if creds and creds.expired and creds.refresh_token:
#             creds.refresh(Request())
#         else:
#             flow = InstalledAppFlow.from_client_secrets_file(
#                 "credentials.json", SCOPES
#             )
#             creds = flow.run_local_server(port=0)
#         with open("token.json", "w") as token:
#             token.write(creds.to_json())

#     return build("gmail", "v1", credentials=creds)

# def send_email(destinatario,asunto, mensaje):
#     service = get_service()
    
#     message = MIMEText(mensaje,"html")
#     message["to"] = destinatario
#     message["from"] = "luiscruz130901@gmail.com"
#     message["subject"] = asunto

#     mensaje = "prueba"
#     raw = base64.urlsafe_b64encode(message.as_bytes()).decode()
#     body = {"raw": raw}

#     sent = service.users().messages().send(userId="me", body=body).execute()
