Skip to content

Email notification

dadosfera.services.email_notification.send_notification

send_notification(server_smtp, port, from_email, app_password, to_email, subject, message)

Sends an email notification via SMTP TLS.

PARAMETER DESCRIPTION
server_smtp

SMTP server.

TYPE: str

port

SMTP server port.

TYPE: int

from_email

Sender's email.

TYPE: str

app_password

APP password.

TYPE: str

to_email

Recipient's email(s).

TYPE: str or list

subject

Email subject.

TYPE: str

message

Email message body content.

TYPE: str

Source code in dadosfera/services/email_notification.py
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
def send_notification(server_smtp, port, from_email, app_password, to_email, subject, message):
    """
    Sends an email notification via SMTP TLS.

    Parameters:
        server_smtp (str): SMTP server.
        port (int): SMTP server port.
        from_email (str): Sender's email.
        app_password (str): APP password.
        to_email (str or list): Recipient's email(s).
        subject (str): Email subject.
        message (str): Email message body content.
    """
    try:

        renderer = DictBodyRenderer(template=NOTIFICATION_EMAIL_TEMPLATE, title=subject, message=message)
        body = renderer.render()

        mail_sender = EmailSender(server_smtp, port, from_email, password=app_password)
        message = mail_sender.create_message(to_email, subject, body)

        mail_sender.send_email(to_email, subject, message)
    except Exception as e:
        logger.error(f"An error occurred: {e}")
        raise e