DEV Community

Cover image for Sending E-mail📨Using Python
Raghav Mrituanjaya
Raghav Mrituanjaya

Posted on • Edited on • Originally published at thegogamicblog.xyz

4 1

Sending E-mail📨Using Python

So, Have you guys ever wondered how do companies like Amazon, Walmart, eBay send emails to your inbox immediately after your purchase. Do you think it's an email sent by an human. I know most of you must have guessed it's an Automated E-mail but how do they do that?

typing-fast

In order to solve the question that was raised we are gonna be trying to send a email using simple Python Code. Feel free to customize the code according to your needs.

A Simple Program

# !/usr/bin/python
import smtplib
sender = 'sender@fromdomain.com'
receivers = ['receiver@todomain.com']
# Your Email Meta Data and your message
message = """
From: From Person <sender@fromdomain.com>
To: To Person <receiver@todomain.com>
Subject: SMTP e-mail test
This is a test e-mail message.
"""
try:
# use SMTP_SSL() for secure connections
smtpObj = smtplib.SMTP(
host="smtp.gmail.com", # The host of your SMTP server
port=25, # The port of your SMTP server(usually 25, 465, 587)
timeout=10 # The timeout in seconds
) # Creates a SMTP object
smtpObj.login(
user="admin", # Your username
password="this_is_a_secure_password" # Your password
) # Logins into your SMTP server
smtpObj.sendmail(sender, receivers, message) # Sends the email
print("Successfully sent email")
except smtplib.SMTPException:
print("Error: unable to send email")
view raw python_email.py hosted with ❤ by GitHub

Points To Note

  • Do let me know where I can improve in writing posts in the comment section below as I am too learning new things :)
  • Thanks For Reading ♥!

Video Reference

Top comments (0)

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay