How to Send Email from Python in Termux?

Python in Termux can be used to send emails automatically using a simple SMTP script. You can use Python libraries to send test emails, notifications, and automated messages easily from your Android device. It is a good way to practice Python programming and create Python automation scripts. Here are the commands to send email from Python in Termux.

Commands to Send Email

Update Termux packages:

pkg update && pkg upgrade -y

Install Python:

pkg install python -y

Create a Python file:

nano sendmail.py

Paste this Python code:

import smtplib

email = "your_email@gmail.com"
password = "your_app_password"

receiver = "receiver@gmail.com"

subject = "Test Email"
message = "Hello from Python in Termux!"

text = f"Subject: {subject}\n\n{message}"

server = smtplib.SMTP("smtp.gmail.com", 587)
server.starttls()
server.login(email, password)
server.sendmail(email, receiver, text)

print("Email sent successfully!")

server.quit()

Replace:

  • your_email@gmail.com with your Gmail address
  • your_app_password with your Gmail App Password
  • receiver@gmail.com with the receiver email address

Save and exit Nano:

Press

CTRL + X
Y
ENTER

Run the script:

python sendmail.py

For Gmail accounts, generate an App Password:

https://myaccount.google.com/apppasswords

Now you can send emails directly from Python in Termux using a simple SMTP script. You can also modify the script to create notification systems, automation projects, and other Python-based tools directly on your Android device.

SHARE THIS POST: