Lab04-ML 3040
Lab04-ML 3040
LAB No: 04
Lab 04: Python Data Reading, Creating Instances, and Transfer through SMTP
Objective:
To familiarize students with the process of reading data from files in Python, creating class instances
to represent this data, and transmitting data through email using the SMTP protocol.
Tools/Software Requirements:
• Python 3.x
• Integrated Development Environment (IDE) such as PyCharm or Visual Studio Code
• Libraries: csv for reading CSV files, smtplib for SMTP operations, and email for email message
composition
How to use the csv library to read and write CSV files
Python's csv module, which is part of the standard library, provides functionality to both read from
and write to CSV files. You can read a CSV file by using the csv.reader object and write to a CSV
file using the csv.writer object. Here's a brief overview of how these are used:
National University of Technology (NUTECH)
Electrical Engineering Department
reader = csv.reader(csvfile)
for row in reader:
print(row)
import smtplib
Start TLS Encryption: Secure the connection to the SMTP server using the starttls() method. This
ensures that the subsequent login credentials and email contents are encrypted.
National University of Technology (NUTECH)
Electrical Engineering Department
server.starttls()
Log in to the SMTP Server: Use the login() method of the SMTP object to log in with your email
username and password.
server.login('yourusername@example.com', 'yourpassword')
Send message using SPTP server: use the send_message method with the objects containing the
details of email to be sent.
server.send_message(msg)
server.quit()
Import the EmailMessage Class: The first step is to import the EmailMessage class from the
email.message module.
Create an EmailMessage Object: Instantiate an EmailMessage object. This object will hold all parts
of the email being constructed.
msg = EmailMessage()
Set the Email Content (Body): Use the set_content() method to add a plain text body to the email.
Add Headers to the Email: Set the standard email headers: 'From', 'To', and 'Subject'.
National University of Technology (NUTECH)
Electrical Engineering Department
msg['From']='sender@example.com' msg['To']
= 'recipient@example.com' msg['Subject'] =
'Your subject goes here'
Example
Lab Task
sends an email to a list of email addresses in csv file. The csv file
will contain data columns (name, email_address, subject, message).
National University of Technology (NUTECH)
Electrical Engineering Department
Simple Email
import smtplib
from email.message import EmailMessage
msg = EmailMessage()
msg.set_content(‘hey dear’)
msg['From']=‘ahmedmustafaf20@nutech.edu.
msg['To'] = ‘talhakhalilf20@nutech.edu.pk'
msg['Subject'] = 'hello'
Output:
Lab Task:
National University of Technology (NUTECH)
Electrical Engineering Department
Code:
import smtplib from email.message import
as csvfile:
reader = csv.reader(csvfile)
message):
= smtplib.SMTP(host='smtp.gmail.com', port=587)
server.ehlo()
server.starttls()
server.send_message(msg)
server.quit()
National University of Technology (NUTECH)
Electrical Engineering Department
Output:
National University of Technology (NUTECH)
Electrical Engineering Department
Conclusion:
In conclusion, this lab successfully achieved its objectives by
providing students with a hands-on experience in reading data
from files using Python, creating class instances to represent the
extracted information, and facilitating the transmission of data
through email using the SMTP protocol. Through the utilization of
essential tools and software, including Python 3.x, IDEs like
PyCharm or Visual Studio Code, and key libraries such as csv,
smtplib, and email, participants gained practical insights into
realworld applications of programming. The combination of file
handling and email communication skills developed in this lab
equips students with valuable competencies applicable across
various domains, fostering a well-rounded understanding of
Python's capabilities for data manipulation and communication.