2018-11-01 11:40:54 +02:00
|
|
|
import smtplib
|
|
|
|
import imaplib
|
|
|
|
import time
|
2018-11-03 19:15:05 +02:00
|
|
|
import sys
|
2018-11-05 17:28:40 +02:00
|
|
|
from email.mime.multipart import MIMEMultipart
|
|
|
|
from email.mime.text import MIMEText
|
|
|
|
import ntpath
|
|
|
|
from email.mime.base import MIMEBase
|
|
|
|
from email import encoders
|
2018-11-01 11:40:54 +02:00
|
|
|
|
2018-11-05 17:28:40 +02:00
|
|
|
msg = MIMEMultipart()
|
|
|
|
msg['From'] = "admin@mailu.io"
|
|
|
|
msg['To'] = "user@mailu.io"
|
|
|
|
msg['Subject'] = "File Test"
|
|
|
|
msg.attach(MIMEText(sys.argv[1], 'plain'))
|
2018-11-01 11:40:54 +02:00
|
|
|
|
2018-11-05 17:28:40 +02:00
|
|
|
if len(sys.argv) == 3:
|
|
|
|
part = MIMEBase('application', 'octet-stream')
|
|
|
|
part.set_payload((open(sys.argv[2], "rb")).read())
|
|
|
|
encoders.encode_base64(part)
|
|
|
|
part.add_header('Content-Disposition', "attachment; filename=%s" % ntpath.basename(sys.argv[2]))
|
|
|
|
msg.attach(part)
|
|
|
|
|
2023-11-15 11:15:34 +02:00
|
|
|
for i in range(5):
|
|
|
|
try:
|
|
|
|
smtp_server = smtplib.SMTP('localhost')
|
|
|
|
smtp_server.set_debuglevel(1)
|
|
|
|
smtp_server.connect('localhost', 587)
|
|
|
|
smtp_server.ehlo()
|
|
|
|
smtp_server.starttls()
|
|
|
|
smtp_server.ehlo()
|
|
|
|
smtp_server.login("admin@mailu.io", "password")
|
2019-01-05 16:12:29 +02:00
|
|
|
|
2023-11-15 11:15:34 +02:00
|
|
|
smtp_server.sendmail("admin@mailu.io", "user@mailu.io", msg.as_string())
|
|
|
|
smtp_server.quit()
|
|
|
|
except smtplib.SMTPDataError as e:
|
|
|
|
if e.smtp_code == 451:
|
|
|
|
print(f"Not ready attempt {i}")
|
|
|
|
time.sleep(5)
|
|
|
|
continue
|
|
|
|
sys.exit(25)
|
|
|
|
except:
|
|
|
|
sys.exit(25)
|
|
|
|
break
|
2018-11-01 11:40:54 +02:00
|
|
|
|
2018-11-05 17:28:40 +02:00
|
|
|
time.sleep(30)
|
2018-11-01 11:40:54 +02:00
|
|
|
|
2018-11-05 17:28:40 +02:00
|
|
|
try:
|
|
|
|
imap_server = imaplib.IMAP4_SSL('localhost')
|
|
|
|
imap_server.login('user@mailu.io', 'password')
|
|
|
|
except:
|
|
|
|
sys.exit(110)
|
2018-11-01 11:40:54 +02:00
|
|
|
|
2018-11-05 17:28:40 +02:00
|
|
|
stat, count = imap_server.select('inbox')
|
|
|
|
try:
|
|
|
|
stat, data = imap_server.fetch(count[0], '(UID BODY[TEXT])')
|
|
|
|
except :
|
2019-01-05 16:12:29 +02:00
|
|
|
print("Couldn’t list email in imap inbox")
|
2018-11-05 17:28:40 +02:00
|
|
|
sys.exit(99)
|
2019-01-05 16:12:29 +02:00
|
|
|
|
2018-11-05 17:28:40 +02:00
|
|
|
if sys.argv[1] in str(data[0][1]):
|
|
|
|
print("Success sending and receiving email!")
|
|
|
|
else:
|
2019-01-05 16:12:29 +02:00
|
|
|
print("Failed receiving email with message %s, message not contained" % sys.argv[1])
|
2018-11-05 17:28:40 +02:00
|
|
|
sys.exit(99)
|
2019-01-05 16:12:29 +02:00
|
|
|
|
|
|
|
typ, data = imap_server.search(None, 'ALL')
|
|
|
|
for num in data[0].split():
|
2019-01-11 12:50:41 +02:00
|
|
|
imap_server.store(num, '+FLAGS', '\\Deleted')
|
2019-01-05 16:12:29 +02:00
|
|
|
imap_server.expunge()
|
|
|
|
|
2018-11-05 17:28:40 +02:00
|
|
|
imap_server.close()
|
|
|
|
imap_server.logout()
|