-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsmtpLab-Media.py
44 lines (35 loc) · 1.21 KB
/
smtpLab-Media.py
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
from socket import *
import base64
import ssl
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders
# instance of MIMEMultipart
message = MIMEMultipart()
message['From'] = "[email protected]"
message['To'] = "[email protected]"
message['Subject'] = "Image Sending"
body = "\r\n I love computer networks! "
# Add Image
message.attach(MIMEText(body, 'plain'))
# open the file to be sent
filename = "Image.png"
attachment = open("Image.png", "rb")
image = MIMEBase('application', 'octet-stream')
image.set_payload((attachment).read())
# encode into base64
encoders.encode_base64(image)
#add filename and positional argument in header
image.add_header('Content-Disposition', "attachment; filename= %s" % filename)
# add image to the message
message.attach(image)
# creates SMTP session
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login("[email protected]", "EK123456")
text = message.as_string()
server.sendmail("[email protected]", "[email protected]", text)
print("\nMail is sended!")
server.quit()