Skip to content

Commit ed48acf

Browse files
committedOct 4, 2022
Send Mails with attachment using Python
1 parent bbb70ed commit ed48acf

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed
 

‎Mail Sender/mail_sender.py

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import smtplib
2+
from email.mime.multipart import MIMEMultipart
3+
from email.mime.text import MIMEText
4+
from email.mime.base import MIMEBase
5+
from email import encoders
6+
body = '''
7+
Hello, Admin
8+
I am attaching The Sales Files With This Email.
9+
This Year We Got a Wooping 200% Profit One Our Sales.
10+
11+
Regards,
12+
Team Sales
13+
xyz.com
14+
'''
15+
# Sender Email addresses and password
16+
senders_email = ''
17+
sender_password = ''
18+
reveiver_email = ''
19+
20+
message = MIMEMultipart()
21+
message['From'] = senders_email
22+
message['To'] = reveiver_email
23+
message['Subject'] = 'Sales Report 2021-- Team Sales'
24+
message.attach(MIMEText(body, 'plain'))
25+
26+
attach_file_name = 'temp.txt'
27+
attach_file = open(attach_file_name, 'rb')
28+
payload = MIMEBase('application', 'octate-stream')
29+
payload.set_payload((attach_file).read())
30+
encoders.encode_base64(payload)
31+
payload.add_header('Content-Decomposition', 'attachment',
32+
filename=attach_file_name)
33+
message.attach(payload)
34+
35+
session = smtplib.SMTP('smtp.gmail.com', 587) # use gmail with port
36+
session.starttls() # enable security
37+
# login with mail_id and password
38+
session.login(senders_email, sender_password)
39+
text = message.as_string()
40+
session.sendmail(senders_email, reveiver_email, text)
41+
session.quit()
42+
print('Mail Sent')

‎Mail Sender/temp.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Hello

0 commit comments

Comments
 (0)
Please sign in to comment.