Skip to content
This repository was archived by the owner on Nov 30, 2022. It is now read-only.

Commit f409b76

Browse files
authored
Merge pull request #86 from sanskarvijpuria/master
Automatic python script for sending mails with attachment
2 parents f46dfa8 + 0cc6a15 commit f409b76

File tree

1 file changed

+93
-0
lines changed

1 file changed

+93
-0
lines changed

Automation-Scripts/automail.py

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
#import all the necessary libraries
2+
import smtplib, ssl
3+
from email import encoders
4+
from email.mime.base import MIMEBase
5+
from email.mime.multipart import MIMEMultipart
6+
from email.mime.text import MIMEText
7+
import csv
8+
9+
#creating the object of class MIMEMultipart
10+
message = MIMEMultipart("alternative")
11+
12+
#Body of message in HTML format
13+
html = """\
14+
<html>
15+
<body>
16+
<p>Some Text in HTML</p>
17+
</body>
18+
</html>
19+
"""
20+
21+
part2 = MIMEText(html, "html")
22+
23+
#attaching the html message to object
24+
message.attach(part2)
25+
26+
port = 465 # For SSL
27+
smtp_server = "smtp.gmail.com"
28+
29+
#Enter email and password
30+
sender_email = "[email protected]" # Enter your address
31+
password = "passworddd"
32+
33+
34+
print("Starting")
35+
context = ssl.create_default_context()
36+
with smtplib.SMTP_SSL(smtp_server, port, context=context) as server:
37+
server.login(sender_email, password)
38+
39+
#Enter your CSV file here
40+
with open("testt.csv") as file:
41+
reader = csv.reader(file)
42+
next(reader) # Skip header row
43+
44+
#Enter the attributes. Here we have attributes as serial name and email
45+
for serial,name,email in reader:
46+
47+
#creating another MIMIMultipart object
48+
msg= MIMEMultipart("alternative")
49+
50+
#Enter your subject here
51+
msg["Subject"] = "Enter your subject here"
52+
msg.attach(message)
53+
54+
#Here we have different file for different serial number. Thus in each loop we are accessing new file
55+
filename = str(serial)+str(".pdf") # In same directory as script
56+
57+
# Open PDF file in binary mode
58+
with open(filename, "rb") as attachment:
59+
# Add file as application/octet-stream
60+
# Email client can usually download this automatically as attachment
61+
part = MIMEBase("application", "octet-stream")
62+
part.set_payload(attachment.read())
63+
64+
# Encode file in ASCII characters to send by email
65+
encoders.encode_base64(part)
66+
67+
# Add header as key/value pair to attachment part
68+
part.add_header(
69+
"Content-Disposition",
70+
f"attachment; filename= {filename}",
71+
)
72+
73+
# Add attachment to message and convert message to string
74+
msg.attach(part)
75+
76+
#this tell the library that mark sender email as from and the email to which the mail has to sent is set to "to".
77+
msg["From"] = sender_email
78+
msg["To"] = email
79+
80+
81+
#Copying everything in msg object as string to text
82+
text = msg.as_string()
83+
84+
#Finally sending mail.Phew!!!
85+
server.sendmail(sender_email, email, text)
86+
print(name)
87+
88+
#deleting the msg object and part. This is necessary otherwise
89+
# every file will get attach with previous file.
90+
# Thus 1st recipient will receive one file and second will receive 2 and third three and so on.
91+
del msg
92+
del part
93+
print("Done")

0 commit comments

Comments
 (0)