-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemial.py
156 lines (119 loc) · 4.36 KB
/
emial.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
def send_mail(mail,name):
import smtplib
import imghdr
from email.message import EmailMessage
Sender_Email = ""
Reciever_Email = mail
Password = ""
newMessage = EmailMessage()
newMessage['Subject'] = "Word Document"
newMessage['From'] = Sender_Email
newMessage['To'] = Reciever_Email
newMessage.set_content('Here is your Image!')
with open(name, 'rb') as f:
image_data = f.read()
image_type = imghdr.what(f.name)
image_name = f.name
newMessage.add_attachment(image_data, maintype='image', subtype=image_type, filename=image_name)
with smtplib.SMTP_SSL('smtp.gmail.com', 465) as smtp:
smtp.login(Sender_Email, Password)
smtp.send_message(newMessage)
print("Sent")
def sendMsg(mail,msg,sub):
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
mail_content = msg
sender_address = '*********@gmail.com'
sender_pass = ''
receiver_address = mail
#Setup the MIME
message = MIMEMultipart()
message['From'] = sender_address
message['To'] = receiver_address
message['Subject'] = sub #The subject line
#The body and the attachments for the mail
message.attach(MIMEText(mail_content, 'plain'))
#Create SMTP session for sending the mail
session = smtplib.SMTP('smtp.gmail.com', 587) #use gmail with port
session.starttls() #enable security
session.login(sender_address, sender_pass) #login with mail_id and password
text = message.as_string()
session.sendmail(sender_address, receiver_address, text)
session.quit()
print('Mail Sent')
def sendpdf(mail,pdfname):
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders
import smtplib
import imghdr
from email.message import EmailMessage
sender = "***********@gmail.com"
password = ''
# put the email of the receiver here
receiver = mail
# Setup the MIME
message = MIMEMultipart()
message['From'] = sender
message['To'] = receiver
message['Subject'] = "Here is your PDF"
body="Hello "+str(mail).split("@")[0]+"!!!"
message.attach(MIMEText(body, 'plain'))
# open the file in bynary
binary_pdf = open(pdfname, 'rb')
payload = MIMEBase('application', 'octate-stream', Name=pdfname)
# payload = MIMEBase('application', 'pdf', Name=pdfname)
payload.set_payload((binary_pdf).read())
# enconding the binary into base64
encoders.encode_base64(payload)
# add header with pdf name
payload.add_header('Content-Decomposition', 'attachment', filename=pdfname)
message.attach(payload)
# use gmail with port
session = smtplib.SMTP('smtp.gmail.com', 587)
# enable security
session.starttls()
# login with mail_id and password
session.login(sender, password)
text = message.as_string()
session.sendmail(sender, receiver, text)
session.quit()
print('Mail Sent')
def sendword(mail,wordname):
import smtplib
from email.mime.base import MIMEBase
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email import encoders
sender = "***********@gmail.com"
password = ''
fromaddr = sender
toaddr = mail
msg = MIMEMultipart()
msg['From'] = fromaddr
msg['To'] = toaddr
msg['Subject'] = "Here is your word Document"
body="Hello "+str(mail).split("@")[0]+"!!!"
msg.attach(MIMEText(body))
files = [wordname]
for filename in files:
attachment = open(filename, 'rb')
part = MIMEBase("application", "octet-stream")
part.set_payload(attachment.read())
encoders.encode_base64(part)
part.add_header("Content-Disposition",
f"attachment; filename= {filename}")
msg.attach(part)
msg = msg.as_string()
try:
server = smtplib.SMTP('smtp.gmail.com:587')
server.ehlo()
server.starttls()
server.login(fromaddr, password)
server.sendmail(fromaddr, toaddr, msg)
server.quit()
print('Email sent successfully')
except:
print("Email couldn't be sent")