- Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.py
More file actions
Latest commit
55 lines (38 loc) · 1.24 KB
/
Copy pathmain.py
File metadata and controls
55 lines (38 loc) · 1.24 KB
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
45
46
47
48
49
50
51
52
53
54
55
importsmtplib
fromemail.mime.multipartimportMIMEMultipart
fromemail.mime.textimportMIMEText
fromemail.mime.baseimportMIMEBase
fromemailimportencoders
fromaddr="your email id here"
password="email id password here"
toaddr="receiver Email id here"
# instance of MIMEMultipart
msg=MIMEMultipart()
# storing the senders email address
msg['From'] =fromaddr
msg['To'] =toaddr
msg['Subject'] ="hii this is mail"
# string to store the body of the mail
body="Body_of_the_mail"
# attach the body with the msg instance
msg.attach(MIMEText(body, 'plain'))
# open the file to be sent
filename="attachments name with file extention eg. home.png"
attachment=open(filename, "rb")
# instance of MIMEBase and named as p
p=MIMEBase('application', 'octet-stream')
# To change the payload into encoded form
p.set_payload((attachment).read())
# encode into base64
encoders.encode_base64(p)
p.add_header('Content-Disposition', "attachment; filename= %s"%filename)
# attach the instance 'p' to instance 'msg'
msg.attach(p)
# creates SMTP session
server=smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(fromaddr,password)
# Converts the Multipart msg into a string
text=msg.as_string()
server.send_message(msg)
server.quit()