> ## Documentation Index
> Fetch the complete documentation index at: https://docs.domino.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Configuring and Sending Emails in Domino Using Python smtplib

> Guide to sending emails from Domino using Python's smtplib. Covers secure SMTP setup via SMTPS or STARTTLS, sending plain-text emails, and HTML-formatted emails using MIME.

## Issue

Users need guidance on how to send emails from Domino using Python's `smtplib` library, including how to set up a secure SMTP connection, send plain-text emails, and send HTML-formatted emails using MIME.

## Root cause

Sending emails from Domino requires configuring a secure SMTP connection and using the correct Python libraries. Without proper setup of connection security, authentication, and MIME formatting, emails may fail to send or render incorrectly.

## Resolution

This article covers three topics:

* Setting up a secure SMTP connection using SMTPS or STARTTLS
* Sending plain-text emails using `smtplib`
* Sending HTML-formatted emails using the `email.mime` module

### 1. Setting up a local SMTP debug server

To test your code locally, use the Python `smtpd` binary bound to port 1025. Messages will not leave localhost and will be output to stdout as a byte stream:

```bash theme={null}
python -m smtpd -c DebuggingServer -n localhost:1025
```

### 2. Starting a secure SMTP connection

There are two ways to secure your SMTP connection. Both examples use Gmail's SMTP server and Python's `ssl` package to load the system's default trusted CA bundles via `create_default_context()`.

**Option A: SMTPS (secured from the outset)**

```python theme={null}
# Using SMTPS to secure the connection from the outset
import smtplib, ssl

port = 465  # For SSL
password = input("Input password and press enter: ")

# Create a secure SSL context, loads system's default trusted CAs
context = ssl.create_default_context()

with smtplib.SMTP_SSL("smtp.gmail.com", port, context=context) as server:
    server.login("my@gmail.com", password)
```

**Option B: STARTTLS (connection secured after initial handshake)**

```python theme={null}
# Using STARTTLS to secure the connection from a point onwards
import smtplib, ssl

smtp_server = "smtp.gmail.com"
port = 587  # For STARTTLS
sender_email = "my@gmail.com"  # Enter your address
password = input("Input password and press enter: ")

# Create a secure SSL context, loads system's default trusted CAs
context = ssl.create_default_context()

try:
    server = smtplib.SMTP(smtp_server, port)
    server.ehlo()  # Can be omitted, called implicitly by the method if required
    server.starttls(context=context)  # Secure the connection
    server.ehlo()  # Can be omitted, called implicitly by the method if required
    server.login(sender_email, password)
    # TODO: Send email here
except Exception as e:
    # Print any error messages to stdout
    print(e)
finally:
    server.quit()
```

### 3. Sending a plain-text email

Define the sender, receiver, and message body at the top of your script. Use `server.sendmail` to send the email.

**Using SMTPS:**

```python theme={null}
import smtplib, ssl

port = 465  # For SSL
smtp_server = "smtp.gmail.com"
sender_email = "my@gmail.com"  # Enter your address
receiver_email = "your@gmail.com"  # Enter receiver address
password = input("Input password and press enter: ")
message = """\
Subject: Hi there

Message body after 2 new lines."""

context = ssl.create_default_context()
with smtplib.SMTP_SSL(smtp_server, port, context=context) as server:
    server.login(sender_email, password)
    server.sendmail(sender_email, receiver_email, message)
```

**Using STARTTLS:**

```python theme={null}
import smtplib, ssl

port = 587  # For STARTTLS
smtp_server = "smtp.gmail.com"
sender_email = "my@gmail.com"  # Enter your address
receiver_email = "your@gmail.com"  # Enter receiver address
password = input("Input password and press enter: ")
message = """\
Subject: Hi there

Message body after 2 new lines."""

context = ssl.create_default_context()
with smtplib.SMTP(smtp_server, port) as server:
    server.ehlo()  # Can be omitted, called implicitly by the method if required
    server.starttls(context=context)
    server.ehlo()  # Can be omitted, called implicitly by the method if required
    server.login(sender_email, password)
    server.sendmail(sender_email, receiver_email, message)
```

### 4. Sending HTML-formatted emails using MIME

Use the `email.mime` module to send emails combining HTML and plain-text content. Since email clients render the last MIME part first, the HTML part must be added after the plain-text part.

```python theme={null}
import smtplib, ssl
from email.mime.text import MIMEText  # Contains the HTML and plain-text content
from email.mime.multipart import MIMEMultipart  # Combines the two

sender_email = "my@gmail.com"  # Enter your address
receiver_email = "your@gmail.com"  # Enter receiver address
password = input("Input password and press enter: ")

message = MIMEMultipart("alternative")
message["Subject"] = "multipart test"
message["From"] = sender_email
message["To"] = receiver_email

# Create the plain-text and HTML version of your message
text = """\
Hi,
How are you?"""
html = """\
<html>
  <body>
    <p>Hi,<br>
      How are you?<br>
    </p>
  </body>
</html>
"""

# Convert these into MIMEText objects
text1 = MIMEText(text, "plain")
html1 = MIMEText(html, "html")

# Add the objects to the MIMEMultipart object
# The email client renders the last part first, hence we add the HTML part last
message.attach(text1)
message.attach(html1)

# Create secure connection with server and send email
context = ssl.create_default_context()
with smtplib.SMTP_SSL("smtp.gmail.com", 465, context=context) as server:
    server.login(sender_email, password)
    server.sendmail(
        sender_email, receiver_email, message.as_string()
    )
```

## Notes

When configuring for a production Domino deployment (assumed to use AWS SES as the SMTP server), the following values are required:

* **SMTP host:** The server name from the SMTP settings page, accessible via the AWS SES service page.
* **SMTP username and password:** Generate these from the SMTP settings page using the **Create My SMTP Credentials** button.
* These values should be available in Domino Central Config. If not, they must be added as Central Config key-value pairs. Contact your internal Domino admin for assistance.
* The code examples in this article are provided as a reference. Some adjustments and indentation corrections may be needed to suit your specific application.


## Related topics

- [How-to guides](/knowledge-base/how-to-guides-overview.md)
- [Analyze costs using API](/cloud/admin/operations/finops/3-analyze-costs-API.md)
- [The python-domino library](/cloud/reference/python-sdk/python-domino.md)
- [Define Flows](/cloud/platform-capabilities/features/flows/define-flows.md)
