# SMTP email in Python

Send plain-text and HTML email with attachments from Python services.

Python services send email through SMTP connections defined in the Dashboard. The connection holds the server's address and credentials, and every service can use it by name.

## Create a connection {#create-a-connection}

To create a connection, go to **Connections** > **Outgoing** > **E-mail** > **SMTP** in the Dashboard, click **Create a new SMTP connection** and fill in the form:

1. **Name:** My Connection
2. **Host:** your SMTP server's address, e.g. `smtp.example.com`
3. **Username:** the account the email is sent from
4. Click **OK**

[Video: New SMTP connection](https://zatosource-production.b-cdn.net/docs/gfx/email/smtp-create.webm?v=1790440776)

Dashboard menu: Connections > Outgoing > E-mail > SMTP

## Send a plain-text email {#send-a-plain-text-email}

To send a message, build an `SMTPMessage` and pass it to the connection's `send` method:

```python
# -*- coding: utf-8 -*-

# Zato
from zato.common import SMTPMessage
from zato.server.service import Service

class MyService(Service):

    def handle(self):

        # Obtain a connection
        conn = self.email.smtp.get('My Connection').conn

        # Create a regular e-mail
        msg = SMTPMessage()
        msg.subject = 'Hello'
        msg.to = 'hello@example.com'
        msg.from_ = 'howdy@example.com'
        msg.body = 'Hello, how are you?'

        # Send the message
        conn.send(msg)
```

## Send an HTML email {#send-an-html-email}

Set `is_html` to True and the message body is sent as HTML:

```python
# -*- coding: utf-8 -*-

# Zato
from zato.common import SMTPMessage
from zato.server.service import Service

class MyService(Service):

    def handle(self):

        # Obtain a connection
        conn = self.email.smtp.get('My Connection').conn

        # Create a regular e-mail
        msg = SMTPMessage()

        # The flag indicating it is HTML
        msg.is_html = True

        msg.subject = 'Hello'
        msg.to = 'hello@example.com'
        msg.from_ = 'howdy@example.com'
        msg.body = '<b>Hello, how are you?</b>'

        # Send the message
        conn.send(msg)
```

## Send an email with attachments {#send-an-email-with-attachments}

To add attachments, call `.attach(name, payload)` on the message. Attachments work with both plain-text and HTML email:

```python
# -*- coding: utf-8 -*-

# Zato
from zato.common import SMTPMessage
from zato.server.service import Service

class MyService(Service):

    def handle(self):

        # Obtain a connection
        conn = self.email.smtp.get('My Connection').conn

        # Create a regular e-mail
        msg = SMTPMessage()
        msg.subject = 'Hello'
        msg.to = 'hello@example.com'
        msg.from_ = 'howdy@example.com'
        msg.body = 'Hello, how are you?'

        # Send attachments along
        msg.attach('attachment_name1.txt', 'Attachment as string goes here')
        msg.attach('attachment_name2.txt', 'Here is another one')

        # Send the message
        conn.send(msg)
```

## See also {#see-also}

- [IMAP](https://zato.io/docs/dev/examples/imap.html) - Receive and process email, including Microsoft 365 with OAuth2
- [Scheduler](https://zato.io/docs/dev/examples/scheduler.html) - Send email on an interval or at a specific time
- [Config files](https://zato.io/docs/dev/examples/config.html) - Keep addresses and templates outside code

## Learn more {#learn-more}

- [Development documentation](https://zato.io/docs/dev/) - The complete reference for writing services
- [Requests and responses](https://zato.io/docs/dev/request-response/) - What a service receives, what it returns and how both are structured
- [Integration examples](https://zato.io/docs/dev/examples/) - Ready-made code for the systems you are likely to connect to
- [IDE and debugging](https://zato.io/docs/dev/ide/) - Write services in the Dashboard or in your own editor
- [Data models](https://zato.io/docs/dev/model/) - Declare inputs and outputs and have them validated for you
- [In-depth API tutorial](https://zato.io/tutorials/main/01.html) - The full platform tutorial, from installation to production patterns
