Skip to content

sxolar.util.gmail

Utilities for sending emails using Gmail.

send_email(subject, to, body, from_email=EMAIL_SENDER, safe=True, is_plain=True, app_password=EMAIL_APP_PASSWORD)

Send an email using Gmail.

Parameters:

Name Type Description Default
subject str

str, The subject of the email.

required
to list[str]

str, The email address to send the email to.

required
body str

str, The body of the email.

required
from_email str

str, default EMAIL_SENDER, the email address to send the email from.

EMAIL_SENDER
safe bool

bool, default True, whether to suppress exceptions when sending the email.

True
is_plain bool

bool, default True, whether the email is plain text or html.

True
app_password str

str, default EMAIL_APP_PASSWORD, the app password for the Gmail account.

EMAIL_APP_PASSWORD
Source code in sxolar/util/gmail.py
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
def send_email(
    subject: str,
    to: list[str],
    body: str,
    from_email: str = EMAIL_SENDER,
    safe: bool = True,
    is_plain: bool = True,
    app_password: str = EMAIL_APP_PASSWORD,
):
    """Send an email using Gmail.

    Args:
        subject:
            str, The subject of the email.
        to:
            str, The email address to send the email to.
        body:
            str, The body of the email.
        from_email:
            str, default EMAIL_SENDER, the email address to send the email from.
        safe:
            bool, default True, whether to suppress exceptions when sending the email.
        is_plain:
            bool, default True, whether the email is plain text or html.
        app_password:
            str, default EMAIL_APP_PASSWORD, the app password for the Gmail account.
    """
    if app_password is None:
        raise ValueError(
            f"Please set the {EMAIL_APP_PASSWORD_ENV_KEY} environment variable to "
            f"your Gmail app password or specify it as the app_password argument."
        )

    # Create a multipart message and set headers
    message = MIMEMultipart("alternative")
    message["Subject"] = subject
    message["From"] = EMAIL_SENDER
    message["To"] = ", ".join(to)

    # Check if the email is plain text or html
    if is_plain:
        part = MIMEText(body, "plain")
        message.attach(part)
    else:
        html_content = f"""\
        <html>
          <body>
            {body}
          </body>
        </html>
        """

        # Turn these into MIMEText objects and attach them to the MIMEMultipart message
        part = MIMEText(html_content, "html")
        message.attach(part)

    # Connect to Gmail's SMTP server and send the email
    try:
        # Gmail SMTP server details
        smtp_server = "smtp.gmail.com"
        smtp_port = 587  # TLS port

        # Establish a secure session using starttls
        with smtplib.SMTP(smtp_server, smtp_port) as server:
            server.starttls()
            server.login(from_email, app_password)
            server.send_message(message)

    except Exception as e:
        if safe:
            print(f"Error sending email: {e}")
        else:
            raise e