How to send emails from Python
To send emails from Python3 in an authenticated way via SMTP, you can use the smtplib library.
You will need the access details for the email account from which the emails will be sent, along with the server access details. In this case, we will use the following details:
- Sender: [email protected]
- Password: Password
- Server: mail.swmanuales.com
- Port: 465
info It is recommended to have a TLS certificate installed for the email. You can install it from the following manual: Manual: How to Activate TLS for my email via SWPanel..
Sending an email in plain text format
The code for sending an email in plain text and in HTML is very similar.
In this case, we are sending an email to the destination '[email protected]', with the subject "Welcome to SWPanel" and the text: "Welcome to SWPanel Access your SWPanel: https://swpanel.com" in plain text format.
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
# Configuración de la conexión
sender = '[email protected]'
password = 'Password'
server = 'mail.swmanuales.com'
port = 465
# Configuración del destinatario
to = '[email protected]'
# configuración de las cabeceras y del mensaje
message = MIMEMultipart("alternative")
message["Subject"] = "Welcome to SWPanel"
message["From"] = sender
message["To"] = to
body = """
Welcome to SWPanel
Access your SWPanel: https://swpanel.com
"""
part = MIMEText(body, "plain")
message.attach(part)
# Envío del mensaje
try:
smtp_server = smtplib.SMTP_SSL(server, port)
smtp_server.ehlo()
smtp_server.login(sender, password)
smtp_server.sendmail(sender, to, message.as_string())
smtp_server.close()
print ("Sent successfully!")
except Exception as ex:
print ("An error has occurred...",ex)
To run it, we can use the following command from the terminal:
python3 file_name.py
If everything went well, it should return: "Sent successfully!".

Sending an email in HTML format
To send an email with HTML content, we can use this variant of the code. The HTML format allows us to add styles to the message:
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
# Configuración de la connexión
sender = '[email protected]'
password = 'Password'
server = 'mail.swmanuales.com'
port = 465
# Configuración del destinatario
to = '[email protected]'
# Configuración de las cabeceras y del mensaje
message = MIMEMultipart("alternative")
message["Subject"] = "Welcome to SWPanel"
message["From"] = sender
message["To"] = to
body = """\
<html>
<head>
<style>
.boton {
background-color: #089cff;
color: white;
padding: 10px 20px;
text-decoration: none;
border-radius: 5px;
}
</style>
</head>
<body>
<h1>Welcome to SWPanel</h1>
<p><a href="https://swpanel.com" class="boton">Access your SWPanel</a></p>
</body>
</html>
"""
part = MIMEText(body, "html")
message.attach(part)
# Envío del mensaje
try:
smtp_server = smtplib.SMTP_SSL(server, port)
smtp_server.ehlo()
smtp_server.login(sender, password)
smtp_server.sendmail(sender, to, message.as_string())
smtp_server.close()
print ("Sent successfully!")
except Exception as ex:
print ("An error has occurred...",ex)
To run it, we can use the following command from the terminal:
python3 file_name.py
If everything went well, it should return: "Sent successfully!".

Automation of email sending
Based on the previous code, we can define a function that will handle sending the email and, through a list, we can define the content of the emails we will send.
In this way, we will not need to edit the document to change the recipient or the email content.
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
import sys
# Creación de la función que realiza el envío
def send_mail(destination, subject, body):
# Configuración de la conexión
sender = '[email protected]'
password = 'Password'
server = 'mail.swmanuales.com'
port = 465
# Configuración de las cabeceras y del mensaje
message = MIMEMultipart("alternative")
message["Subject"] = subject
message["From"] = sender
message["To"] = destination
part = MIMEText(body, "html")
message.attach(part)
# Envío del mensaje
try:
smtp_server = smtplib.SMTP_SSL(server, port)
smtp_server.ehlo()
smtp_server.login(sender, password)
smtp_server.sendmail(sender, destination, message.as_string())
smtp_server.close()
print ("Sent successfully!")
except Exception as ex:
print ("An error has occurred...",ex)
text = {
#'identificador': ['Asunto', 'cuerpo']
'welcome': ['Welcome to SWPanel', '<html> <head> <style> .boton { background-color: #089cff; color: white; padding: 10px 20px; text-decoration: none; border-radius: 5px; } </style> </head> <body> <h1>Welcome to SWPanel</h1> <p><a href="https://swpanel.com" class="boton">Access your SWPanel</a></p> </body> </html>'],
'other_text': 'other text',
'other_text2': 'other text 2',
}
message = text[sys.argv[0]]
subject = message[0]
body = message[1]
# Configuración del destinatario
destination = sys.argv[1]
send_mail(destination, subject, body)
When executing the file via console, we will specify what type of email we want to send and the recipient to whom it should be sent.
To run it, we can use the following command from the terminal:
python3 file_name.py welcome [email protected]
Where 'welcome' is the identifier in the list and '[email protected]' is the destination account.
If everything went well, it should return: "Sent successfully!".
