Skip to content

Commit

Permalink
Merge pull request #5 from ijara/2-send-email
Browse files Browse the repository at this point in the history
2 send email
  • Loading branch information
ijara authored Nov 12, 2024
2 parents 3bdbc56 + c00d535 commit 958af8d
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 3 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -165,4 +165,5 @@ cython_debug/
.env
config.py
#daily/
pdf/
pdf/
emails.txt
4 changes: 3 additions & 1 deletion config.py.bak
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
OPENAI_API_KEY='apikey'
OPENAI_API_KEY='apikey'
EMAIL_USER='[email protected]'
EMAIL_PASSWORD='Jpg1995'
1 change: 1 addition & 0 deletions emails.txt.bak
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[email protected]
31 changes: 30 additions & 1 deletion main.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,25 @@ def descargar_pdfs(pdf_urls):
print(f"Todos los PDFs han sido descargados en: {carpeta_dia}")
return carpeta_dia

def enviar_email(texto):
from redmail import EmailSender
email = EmailSender(
host="smtp.gmail.com",
port=587,
username=EMAIL_USER,
password=EMAIL_PASSWORD,
)

# Example emails.txt content:
# [email protected]
# [email protected]
# [email protected]
with open('emails.txt', 'r') as f:
email.receivers = [line.strip() for line in f if line.strip()]
email.send(
subject="Newsletter Diario Oficial - " + datetime.datetime.now().strftime("%Y%m%d"),
html=texto
)

def cargar_pdf_a_chatgpt(ruta_pdf):
print(f"Intentando cargar PDF a ChatGPT: {os.path.basename(ruta_pdf)}")
Expand Down Expand Up @@ -160,7 +179,16 @@ def main():

if debug:
print("Modo de depuración activado.")

try:
with open('docs/latest.html', 'r', encoding='utf-8') as archivo:
contenido = archivo.read()
print("Contenido de latest.html:")
print(contenido)
enviar_email(contenido)
except FileNotFoundError:
print("El archivo docs/latest.html no existe")
except Exception as e:
print(f"Error al leer el archivo: {str(e)}")
sys.exit()


Expand Down Expand Up @@ -237,5 +265,6 @@ def main():

if __name__ == "__main__":
from config import OPENAI_API_KEY # Importa la clave API desde un archivo de configuración
from config import EMAIL_USER, EMAIL_PASSWORD
openai.api_key = OPENAI_API_KEY # Usa la variable importada para la API key de OpenAI
main()

0 comments on commit 958af8d

Please sign in to comment.