Skip to content

Commit

Permalink
Merge pull request #8 from lapig-ufg/email
Browse files Browse the repository at this point in the history
send email
  • Loading branch information
jairomr authored Oct 3, 2024
2 parents fa5d8ff + eefaeb4 commit b9c45a1
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions workers/send_email.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@

def send_email(payload: SendEmail):
try:
SERVER_MAIL = os.environ.get('SERVER_EMAIL')
PORT_EMAIL = os.environ.get("EMAIL_PORT",465)

sender_email = os.environ.get('EMAIL_SENDER')
password = os.environ.get('EMAIL_PASSWORD')
receiver_email = payload['receiver_email']
Expand All @@ -28,15 +31,21 @@ def send_email(payload: SendEmail):
message.attach(MIMEText(html, "html"))

# Create secure connection with server and send email

context = ssl.create_default_context()
with smtplib.SMTP_SSL(os.environ.get('SERVER_EMAIL'), os.environ.get("EMAIL_PORT",465), context=context) as server:

server.login(sender_email, password)
server.sendmail(
sender_email, receiver_email, message.as_string()
)

try:
# Conecta ao servidor de e-mail usando TLS
with smtplib.SMTP(SERVER_MAIL, PORT_EMAIL) as server:
server.starttls(context=context) # Inicia a conexão TLS
server.login(sender_email, password) # Faz login no servidor SMTP
server.sendmail(sender_email, receiver_email, message.as_string()) # Envia o e-mail
print("E-mail enviado com sucesso!")

except Exception as e:
raise Exception(f"Error sending email: {e} \n {sender_email} - {receiver_email}\n {SERVER_MAIL}:{PORT_EMAIL}")
except Exception as e:
raise Exception(f"Error sending email: {e} {sender_email} {password} {receiver_email}")
raise Exception(f"Error sending email: {e}")
return {"status": "Email sent successfully"}


Expand Down

0 comments on commit b9c45a1

Please sign in to comment.