0% found this document useful (0 votes)
20 views9 pages

Lab04-ML 3040

Uploaded by

akbarmughal2824
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views9 pages

Lab04-ML 3040

Uploaded by

akbarmughal2824
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

National University of Technology (NUTECH)

Electrical Engineering Department

EE4407-Machine Learning Lab

LAB No: 04

Name: Muhammad Ahmed Mustafa

Student ID: F20603040

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

Explanation of the CSV File Format


CSV (Comma-Separated Values) files are a type of plain text file that uses specific structuring to
arrange tabular data. Each line in a CSV file corresponds to a row in a table, and each field in that
row or line is separated by a comma. This format is widely supported by spreadsheets and
databases, which provide functionality to convert content into CSV format. Despite the name, CSV
files can use delimiters other than commas, such as tabs or spaces, and often include a header
row that contains column names.

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

EE4407-Machine Learning Lab

import csv with open('example.csv', newline='')


as csvfile:

reader = csv.reader(csvfile)
for row in reader:
print(row)

Setting Up SMTP for Email Transmission in Python


Simple Mail Transfer Protocol (SMTP) is the standard protocol for sending emails across the Internet.
Python provides the smtplib library, which allows a straightforward way to set up an SMTP connection
and send emails programmatically.

Using smtplib to Connect to an SMTP Server:


The smtplib library contains the SMTP class, which is used to create objects that can connect to and
communicate with mail servers. Here are the steps to establish this connection:

Import the smtplib Library:


Begin by importing the smtplib library, which is part of Python's standard library and does not require
separate installation.

import smtplib

Create an SMTP Object:


Instantiate an SMTP object by providing the SMTP host and port number. This object will represent
the server connection.

server = smtplib.SMTP(host='smtp.example.com', port=587)

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

EE4407-Machine Learning Lab

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)

Terminate the SMTP session and close the connection

server.quit()

Setting Up an Email Message in Python


The EmailMessage class is a flexible and modern approach to email composition in Python. It
represents an email message and provides methods for adding content, headers, and attachments.

Import the EmailMessage Class: The first step is to import the EmailMessage class from the
email.message module.

from email.message import EmailMessage

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.

msg.set_content('Your email body goes here')

Add Headers to the Email: Set the standard email headers: 'From', 'To', and 'Subject'.
National University of Technology (NUTECH)
Electrical Engineering Department

EE4407-Machine Learning Lab

msg['From']='sender@example.com' msg['To']
= 'recipient@example.com' msg['Subject'] =
'Your subject goes here'

Example

def setup_email(username, recipient, subject, message):


# Create the EmailMessage object
msg = EmailMessage()

# Set the content of the email


msg.set_content(message)

# Set the email headers


msg['From'] = username
msg['To'] = recipient
msg['Subject'] = subject
return msg

Lab Task

Write a python script in which a function

def send_email( relevant arguments )

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

EE4407-Machine Learning Lab

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'

server = smtplib.SMTP(host='smtp.gmail.com', port=587) server.starttls()


server.login(‘ahmedmustafaf20@nutech.edu.pk', 'wscwrwjnsunp ')
server.send_message(msg)
server.quit()
National University of Technology (NUTECH)
Electrical Engineering Department

EE4407-Machine Learning Lab

Output:

Lab Task:
National University of Technology (NUTECH)
Electrical Engineering Department

EE4407-Machine Learning Lab

Code:
import smtplib from email.message import

EmailMessage import csv

data = [] with open('ahmed11.csv', 'r')

as csvfile:

reader = csv.reader(csvfile)

next(reader) data = list(reader)

print(data) for a in data: email = a[1]

subject = a[2] message = a[3]

send_email(email, subject, message)

def send_email(email, subject,

message):

msg = EmailMessage() msg['To'] = email

msg['Subject'] = subject msg.set_content(message)

msg['From'] = 'ahmedmustafaf20@nutech.edu.pk' server

= smtplib.SMTP(host='smtp.gmail.com', port=587)

server.ehlo()

server.starttls()

server.login('ahmedmustafa20@nutech.edu.pk', 'wscwrwjnsunp ')

server.send_message(msg)

server.quit()
National University of Technology (NUTECH)
Electrical Engineering Department

EE4407-Machine Learning Lab

Output:
National University of Technology (NUTECH)
Electrical Engineering Department

EE4407-Machine Learning Lab

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.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy