-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
47 lines (38 loc) · 1.63 KB
/
test.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
# smtplib provides functionality to send emails using SMTP.
import smtplib
import os
# MIMEMultipart send emails with both text content and attachments.
from email.mime.multipart import MIMEMultipart
# MIMEText for creating body of the email message.
from email.mime.text import MIMEText
# MIMEApplication attaching application-specific data (like CSV files) to email messages.
from email.mime.application import MIMEApplication
# SMTP server configuration
smtp_server = "harbor.allsunday.io"
smtp_server = "localhost"
smtp_port = 25
subject = "Email Subject"
body = "This is the body of the text message"
sender_email = "[email protected]"
recipient_email = "[email protected]"
# MIMEMultipart() creates a container for an email message that can hold
# different parts, like text and attachments and in next line we are
# attaching different parts to email container like subject and others.
message = MIMEMultipart()
message["Subject"] = subject
message["From"] = sender_email
message["To"] = recipient_email
body_part = MIMEText(body)
message.attach(body_part)
path_to_file = "test.py"
file_name = os.path.basename(path_to_file)
# section 1 to attach file
with open(path_to_file, "rb") as file:
# Attach the file with filename to the email
message.attach(MIMEApplication(file.read(), Name=file_name))
with open(path_to_file, "rb") as file:
# Attach the file with filename to the email
message.attach(MIMEApplication(file.read(), Name=file_name))
# secction 2 for sending email
with smtplib.SMTP(smtp_server, smtp_port) as server:
server.sendmail(sender_email, recipient_email, message.as_string())