Linux

How to send email using python on Linux

We have seen in one of previous post, how to send email on Linux using Telnet.

However Telnet is not a common method nowadays due to security concerns. For that in this blog post we will cover how to send email on Linux using Python.

For the no programmer, don’t worry, you don’t need to be expert to understand the script that we will see in this article.

Using python for scripting

Using Python for scripting is a popular and practical choice. Python’s syntax is straightforward, making it easy to write and understand scripts quickly. It provides a wide range of libraries and modules that simplify common tasks, allowing developers to be productive with less effort.

Python Script to send email

First create a new file with a .py extension

# vi sending_mail.py

Here is an exemple of python script that I have tested to send email :

import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart

# Email configuration
sender_email = "your_email@gmail.com"
receiver_email = "recipient_email@example.com"
subject = "Subject of the email"
body = "Body of the email"

# Create the email message
message = MIMEMultipart()
message["From"] = sender_email
message["To"] = receiver_email
message["Subject"] = subject

# Attach the body of the email
message.attach(MIMEText(body, "plain"))

# Connect to the SMTP server (Gmail example)
smtp_server = "smtp.gmail.com"
smtp_port = 25

with smtplib.SMTP(smtp_server, smtp_port) as server:
    # # Identify yourself to the SMTP server
    server.ehlo()

    # Send the email
    server.sendmail(sender_email, receiver_email, message.as_string())

print("Email sent successfully.")

Don’t forget to make the nececesay modifcation to add you own configuration.

Here, I use port 25 which is traditionally used for unencrypted SMTP communication. It’s the default SMTP port, but many email providers may prefer or require the use of encrypted connections for security reasons.

So if your email server support TLS, use the the script bellow.

Python Script to send email using TLS

To secure the communication with smtp server, use the port 587 instread of 25.

Port 587 is commonly used for email submissions with support for TLS (Transport Layer Security) or STARTTLS, which provides encryption for the communication between the email client (your Python script, in this case) and the email server.

Using encryption is generally considered more secure, especially when you are transmitting sensitive information like email usernames and passwords.

Here is an example of using python to send email with TLS :

import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders

# Email configuration
sender_email = "your_email@gmail.com"
receiver_email = "recipient_email@example.com"
subject = "Subject of the email"
body = "Body of the email"

# Create the email message
message = MIMEMultipart()
message["From"] = sender_email
message["To"] = receiver_email
message["Subject"] = subject

# Attach the body of the email
message.attach(MIMEText(body, "plain"))

# Attach a file
attachment_file_path = "/path/to/your/file.txt"
attachment = open(attachment_file_path, "rb")

part = MIMEBase("application", "octet-stream")
part.set_payload(attachment.read())
encoders.encode_base64(part)
part.add_header(
    "Content-Disposition",
    f"attachment; filename= {attachment_file_path.split('/')[-1]}",
)
message.attach(part)

# Connect to the SMTP server (using port 25 in this example)
smtp_server = "smtp.gmail.com"
smtp_port = 587
smtp_username = "your_email@gmail.com"
smtp_password = "your_email_password"

with smtplib.SMTP(smtp_server, smtp_port) as server:
    # Start TLS for security
    server.starttls()

    # Login to the SMTP server
    server.login(smtp_username, smtp_password)
    server.sendmail(sender_email, receiver_email, message.as_string())

print("Email with attachment sent successfully.")

In this case you will need to insert the username and the password to login to the smtp server.

You might also like

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *