How to send email using Python.

How to send email using Python.

Folks,

In this blog we will explore how send email to single/multiple email id using python Script.


Python Script: See detailed comments in the Python Script.


# import the smtplib module.
import smtplib

# from user details
from_user_email_id =  [provide_your_email_id_here]
from_user_email_pwd = [provide_your_email_id_password]

# Email Subject
email_subject = [provide_your_email_subject]

# To user email ids
to_user_email_id = [provide_list_of_to_email_ids_comma_seperated]

# set up the SMTP server
s = smtplib.SMTP(host=[provide_smtp_host_address_here], port=[provide_smtp_port_here]])
s.ehlo()
s.starttls()
s.ehlo

# login the SMTP server using from email id and password.
s.login(from_user_email_id, from_user_email_pwd)

# Preparing email message header and body.
email_header = 'To:' + ", ".join(to_user_email_id)  + '\n' + 'From: ' + from_user_email_id + '\n' + 'Subject: ' +  email_subject + '  \n'
email_body = email_header + [provide_here_email_text]

# sending email
s.sendmail(from_user_email_id, to_user_email_id, email_body)

s.close()

 

After providing all the required information & save your file  in .py extension. Refer below example.

email

Execute Script:

execute

Result:

email4


Thanks!

Happy Learning! Your feedback would be appreciated!

Advertisements

Python [Post 1] | Overview & Basics

What is Python? 

python-logo.png

Python is a general purpose object-oriented programming language with high-level programming capabilities. Developed by Guido van Rossum in the late eighties and early nineties at the National Research Institute for Mathematics and Computer Science in the Netherlands.

  • Open Source general-purpose programming language.
  • High-level programming language (i.e. independent of the computer’s hardware).
  • Interpreted (i.e. not need to compile your program before executing it, Python is processed at run-time by the interpreter)
  • Multiple Programming Paradigms ( i.e. Python supports object oriented programming, imperative and functional programming or procedural styles)
  • Code Readability – Python is easy to read, write & maintain.
  • Interactive –   IDLE is Python’s built-in Integrated Development Environment. It has a Python shell window, which gives you access to the Python interactive mode.

Python is fully-functional programming language that can do almost anything.

You can install latest Python from this website – https://www.python.org/

Please see below YouTube video for Python Installation on you Windows Machine in 2 minutes.

After installation you can check the Python Version using Command Prompt.

1.PNG

Hello, World! in Python programming.

print("Hello World!")

2.PNG


Thanks!

Happy Learning! Your feedback would be appreciated!