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

# Sending e-mail messages with Python

> Learn how to send e-mail messages using Python with these detailed instructions including code snippets and links to several related articles.

This article demonstrates how to send e-mail messages using Python.

<Warning>
  **Important**

  You cannot use external SMTP servers to send e-mail messages if you have one of the following hosting packages:

  * Shared web hosting

  * Reseller hosting

  * Managed WordPress hosting

  For these hosting packages, you must use hosting.com servers. Other hosting packages have fewer restrictions, and can use some external SMTP servers to send e-mail messages.
</Warning>

## Using Python to send e-mail messages

Python's **smtplib** module makes it easy to send e-mail messages from a script. To do this, you create an instance of the **SMTP** class, which contains all of the functionality you need to connect to a mail server and send messages.

The following sample code demonstrates how to send an e-mail message from a hosting.com server using the **SMTP** class. In your own code, replace ***[recipient@example.com](mailto:recipient@example.com)*** with the intended message recipient, and ***[sender@example.com](mailto:sender@example.com)*** with the return e-mail address. For the **login()** method, specify the account username and password that you want to use to authenticate to the SMTP server:

```python theme={null}
#!/usr/bin/python3

import sys
import smtplib

from_addr = 'sender@example.com'
to_addrs = ['recipient@example.com']
msg = """From: sender@example.com
To: recipient@example.com
Subject: This is the message subject

This is the message body.
"""

try:
    s = smtplib.SMTP('localhost')
    s.login('sender@example.com', 'password')
    s.sendmail(from_addr, to _addrs, msg)
    s.quit()
except smtplib.SMTPException:
    print ("Error:", sys.exc_info()[0])
```

This script specifies the sender, recipient, and message. It then constructs an instance of the **SMTP** class. After the class instance is created, all you need to do is log in to the server and send the message using the **sendmail()** method.

This is a very simple example that demonstrates basic functionality provided by the **SMTP** class. To view the official documentation for the **smtplib** module (including the **SMTP** class), please visit [https://docs.python.org/3/library/smtplib.html](https://docs.python.org/3/library/smtplib.html).

## Related articles

* [Viewing e-mail message headers](/docs/viewing-e-mail-message-headers)
