-
Notifications
You must be signed in to change notification settings - Fork 0
/
mail2.py
34 lines (33 loc) · 881 Bytes
/
mail2.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
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders
# sender
fromadd = ""
# receiver
toadd = ""
msg = MIMEMultipart()
msg['from'] = fromadd
msg['to'] = toadd
# subject
msg["subject"] = ""
body = "body_of_mail"
msg.attach(MIMEText(body, "plain"))
#path of the file
filename = ""
# open file in read and binary mode
attachment = open(filename, "rb")
p = MIMEBase("application", "octet-stream")
p.set_payload(attachment.read())
encoders.encode_base64(p)
p.add_header("Content-Disposition", "attachment; filename=" + filename)
msg.attach(p)
# 587 is the port
mainserver = smtplib.SMTP('smtp.gmail.com', 587)
mainserver.starttls()
# senders gmail and password
mainserver.login(fromadd, "")
text = msg.as_string()
mainserver.sendmail(fromadd, toadd, text)
mainserver.close()