-
Notifications
You must be signed in to change notification settings - Fork 0
/
send_email.py
executable file
·89 lines (74 loc) · 2.7 KB
/
send_email.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
#!/usr/bin/env python
import time
import glob
import shutil
import os
import sys
import json
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders
def mailit(file: str) -> None :
# Send email, if we can
filename = os.path.basename(file)
# Create the MIMEMultipart message object
to = "".join(open(file,"r").readlines()).strip()
msg = MIMEMultipart()
msg['From'] = config["email"]["from"]
msg['To'] = to
msg['Subject'] = config["email"]["subject"]
# Attach the body text to the email
msg.attach(MIMEText(config["email"]["message"], 'plain'))
# Attach picture
with open(f"done/{filename[0:-6]}.jpg", "rb") as fh:
# Create a MIMEBase instance and attach the text file
part = MIMEBase('img', 'jpg')
part.set_payload(fh.read())
# Encode the payload to base64
encoders.encode_base64(part)
# Add header for the attachment
part.add_header(
'Content-Disposition',
f'attachment; filename={filename[0:-6]}.jpg',
)
# Attach the file to the message
msg.attach(part)
# Attach text
with open(f"done/{filename[0:-6]}.txt", "r") as fh:
# Create a MIMEBase instance and attach the text file
part = MIMEBase('application', 'octet-stream')
part.set_payload(fh.read())
# Encode the payload to base64
encoders.encode_base64(part)
# Add header for the attachment
part.add_header(
'Content-Disposition',
f'attachment; filename={filename[0:-6]}.txt',
)
# Attach the file to the message
msg.attach(part)
try:
with smtplib.SMTP(config["email"]["server"], config["email"]["port"]) as server:
server.starttls() # Upgrade the connection to TLS
server.login(config["email"]["user"], config["email"]["pass"]) # Log in to the email account
text = msg.as_string() # Convert the message to string format
server.sendmail(config["email"]["from"], to, text) #Send the email
shutil.move(file, "done/")
print("done")
except Exception as e:
print(f"Emailing failed: {e}")
shutil.move(file, "failed/")
print("failed")
config = json.load(open("config.json", "r"))
while True:
files = glob.glob("process/*.email")
if "email" in config :
for file in files:
filename = os.path.basename(file)
if os.path.exists(f"done/{filename[0:-6]}.txt") :
print(f"Emailing '{file}'...", end="")
sys.stdout.flush()
mailit(file)
time.sleep(1)