Skip to content
This repository was archived by the owner on Nov 16, 2023. It is now read-only.

Commit

Permalink
Send mail at the end of program
Browse files Browse the repository at this point in the history
  • Loading branch information
qhy040404 committed May 11, 2023
1 parent fed0517 commit d6d825b
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 15 deletions.
37 changes: 37 additions & 0 deletions src/mail.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import smtplib

import reserve


class Mail:
def __init__(self, host, user, passwd, message):
self.host = host
self.user = user
self.passwd = passwd
self.message = message

def send(self, count):
print(f'Sending email#${count}...')
reserve.logging.info(f'Sending email#${count}...')
try:
smtp_obj = smtplib.SMTP_SSL(self.host, 465)
smtp_obj.login(self.user, self.passwd)
smtp_obj.sendmail(self.user, self.user, self.message)
smtp_obj.quit()
print(f'Email #${count} succeed')
reserve.logging.info(f'Email #${count} succeed')
except smtplib.SMTPException as e:
print(f'Email #${count} error', e)
reserve.logging.error(f'Email #${count} error. ' + str(e))


mails: list[Mail] = []


def send_all():
if len(mails) == 0:
return
count = 1
for mail in mails:
mail.send(count)
count += 1
27 changes: 12 additions & 15 deletions src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
# import
import os
import platform
import smtplib
import sys
import time
from email.mime.text import MIMEText

import reserve
from mail import Mail, mails, send_all
from utils import has_more

# pre-define
Expand Down Expand Up @@ -36,6 +36,12 @@
'202': '181'}
}


def send_and_exit():
send_all()
reserve.logging.info('Exiting...')


# Read config
reserve.logging.info('Importing data from config.conf')
with open("config.conf", "r") as config:
Expand Down Expand Up @@ -75,8 +81,8 @@ def send_email(seat=None, success=False, error=None):
mail_user, mail_pass = mail_data[0], mail_data[1]
mail_host = f"smtp.{mail_user.split('@')[1]}"

print('Sending email...')
reserve.logging.info('Sending email...')
print('Generating email...')
reserve.logging.info('Generating email...')

if success:
reserve.logging.info('Success = \'True\'')
Expand All @@ -90,16 +96,7 @@ def send_email(seat=None, success=False, error=None):
message['From'] = mail_user
message['To'] = mail_user

try:
smtp_obj = smtplib.SMTP_SSL(mail_host, 465)
smtp_obj.login(mail_user, mail_pass)
smtp_obj.sendmail(mail_user, mail_user, message.as_string())
smtp_obj.quit()
print('Email succeed')
reserve.logging.info('Email succeed')
except smtplib.SMTPException as e:
print('Email error', e)
reserve.logging.error('Email error. ' + str(e))
mails.append(Mail(mail_host, mail_user, mail_pass, message.as_string()))


# main
Expand All @@ -119,6 +116,6 @@ def send_email(seat=None, success=False, error=None):
reserve.logging.info('Detected. Returning...')
reserve.logging.info('')
else:
reserve.logging.info('Exiting...')
send_and_exit()
else:
reserve.logging.info('Exiting...')
send_and_exit()

0 comments on commit d6d825b

Please sign in to comment.