-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSendMailWithAttachmennt1.py
59 lines (36 loc) · 1.3 KB
/
SendMailWithAttachmennt1.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders
def sendMail():
fromEmail = "Sidheshwarj2001@gmail.com"
EmailPawword = ""
sendToEmail = "Sidjawale007@gmail.com"
# making instance of multipart
msg = MIMEMultipart()
msg["From"] = fromEmail
msg["To"] = sendToEmail
msg["Subject"] = " This is mail with attach file"
body = " Body of the mail send by python script"
# open the file which you want to send
filename = "file name with extension"
attachment = open("first.py",'rb')
msg.attach(MIMEText(body, "plain"))
# instance of MINEBase and named as p
p = MIMEBase('application', 'octet-stream')
p.set_payload(attachment.read()) # file attach
encoders.encode_base64(p)
p.add_header('current-Disponsition', 'attachment; filename = %s' % filename)
msg.attach(p)
server = smtplib.SMTP("smtp.gmail.com", 587)
server.starttls()
server.login(fromEmail,EmailPawword)
text = msg.as_string()
server.sendmail(fromEmail,sendToEmail,text)
server.quit()
print("mail successfully send")
def main():
sendMail()
if __name__ == "__main__":
main()