This is a very interesting topic in terms of pure implementation of computer networks. Here we see how we use different layers of computer networks for communication using various protocols like HTTP, TCP/IP, UDP, etc. Here we will apply SMTP protocol for sending content such as dynamic data, PDFs, posters, images, etc., using client server architecture
Here we see the roles, responsibilities, and characteristics of all the layers, like bit data to signal conversion, error detection, routing, synchronization, encryption-decryption, etc.
Now our message moves to the network layer. There we use concepts of routing and fragmentation of our message into small datagrams, which is then sent to the data link layer. Here we do error checking correction and get the Mac address of the device and other details. Finally it is sent to the physical layer. Here we use a guided or unguided medium to send our message to the receiver. For guided medium we use the wired network and for unguided we use the wireless network. We do the same for the receiver end, from down to up.
Now we see some protocols that we use in email sending in Python.
SMTP is used to send mail to any user from the server. Here we generally send seven bit ASCII code.
SMTP commands
HELO AND EHLO
MAIL FROM
RCPT
RSET
Quit
In this step we create a connection between two senders and receivers. For this we have to know the SMTP server name, port address, and sender and receiver mail ID.
1
2
3
4
5
6
7
8
9
10
import smtplib
sever_name = ”smtp.gmail.com”
port = 465
server = smtplib.SMTP_SSL(server_name, port)
senders_emailid = ”senderid @gmail.com”
receivers_emailid = ”receivers @gmail.com”
message_content = ”Hello How are you!”
password = ”xxxxxxxx”
server.login(sender_emailid, password)
server.sendmail(senders_emailid, receivers_emailid, message_content)
Above we can see the simple way to send a message to any Gmail ID. If you are using a Gmail account you have to switch off the two-factor verification, otherwise it will show an error.
If you want to send a message in the form of head, body, subject, or attach files like a PDF or image, you will use some more Python script and more Python libraries for that.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import smtplib
import os
import imghdr
from email.message
import EmailMessage
EMAIL_ADDRESS = ”senderid @gmail.com”
EMAIL_PASSWORD = ”xxxxxxxx”
msg = EmailMessage()
msg['Subject'] = 'Lets us Learn The smtp protocol'
msg['From'] = EMAIL_ADDRESS
msg['To'] = ”receivers @gmail.com”
msg.set_content('How about dinner at 6pm this saturday')
with open('image.jpg', 'rb') as f:
file_data = f.read()
file_type = imghdr.what(f.name)
file_name = f.name
msg.add_attchment(file_data, maintype = 'image', subtype = file_type, filename = file_name)
with smtplib.SMTP_SSL('smtp.gmail.com', 465) as smtp:
smtp.login(EMAIL_ADDRESS, EMAIL_PASSWORD)
smtp.send_message(msg)
Above we see how simple and static data is sent over email using SMTP protocol. Now we will see our dynamic and HTML data sent over mail using Python HTML and CSS scripts.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import smtplib
import os
import imghdr
from email.message
import EmailMessage
EMAIL_ADDRESS = ”senderid @gmail.com”
EMAIL_PASSWORD = ”xxxxxxxx”
msg = EmailMessage()
msg['Subject'] = 'Lets us Learn The smtp protocol'
msg['From'] = EMAIL_ADDRESS
msg['To'] = ”receivers @gmail.com”
msg.set_content('How about dinner at 6pm this saturday')
msg.add_alternative(""
"\
<!Doctype html> <
html >
<body>
<p>Hi Good Morning.</p>
<p>Here is your data:</p>
<p>Thanks & Regards,</p>
<p>Shubham Kumar Shukla</p>
</body> <
/html>
""
",subtype='html')
with smtplib.SMTP_SSL('smtp.gmail.com', 465) as smtp:
smtp.login(EMAIL_ADDRESS, EMAIL_PASSWORD) smtp.send_message(msg)