-
Notifications
You must be signed in to change notification settings - Fork 17
/
Attachements.py
55 lines (49 loc) · 1.77 KB
/
Attachements.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
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders
host = "smtp.gmail.com"
port = 587
username = str(input("Enter Gmail Username : "))
password = str(input("Enter Gmail Password : "))
_to = username # Add any other username you want by adding a comma.
_from = username
try:
connection = smtplib.SMTP(host, port)
connection.ehlo()
connection.starttls()
connection.login(username, password)
print("\nCONNECTION SUCCESSFUL")
message = MIMEMultipart("alternate") # For the mail to understand HTML
message["Subject"] = "Html Message" # You can add Subjects as well.
message["From"] = _from
message["To"] = _to
html_message = """
<html>
<body>
<h3>ATTACHMENT</h3>
<p>Hey! This Email contains an Attachment file.</p>
</body>
</html>
"""
msg = MIMEText(html_message, "html")
message.attach(msg)
filename = "Mail Attachments/name.txt" # File path.
openfile = open(filename, "rb") # Opening the file.
mimref = MIMEBase(
"applicaiton", "octect_stream"
) # Telling the browser the way to open the file.
mimref.set_payload(openfile.read()) # Setting the file as payload to be sent.
encoders.encode_base64(mimref) # Mail has to be encoded.
mimref.add_header(
"Content-Disposition", "openfile;filename=name.txt"
) # Setting attachmenet name and type.
message.attach(mimref) # Attaching the file.
connection.sendmail(_from, _to, message.as_string())
print("Message has been sent Successfully")
connection.quit()
print("Connection has been closed.")
except:
print("CONNECTION ERROR")
print("PROGRAM TERMINATED")