Skip to content
This repository was archived by the owner on Jan 31, 2025. It is now read-only.
Open

Dev2 #20

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions mail/mail.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
class mail:
class Mail:
"""
This module is to send email by google smtp.
You can use this module to send email.
"""

def __init__(self, prot, *argv):
self.prot = prot(*argv)

def login(self, account, passwd):
self.prot.login(account, passwd)

def send(self, frm, to, subject, content):
self.prot.send(frm, to, subject, content)
def send(self, frm, to, content):
self.prot.send(frm, to, content)

def quit(self):
self.prot.quit()
43 changes: 43 additions & 0 deletions mail/mime/Mime.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import os
import sys
from email import encoders
from email.mime.base import MIMEBase
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from datetime import date


class Mime:

msg = ""

def __init__(self, *argv):
self.msg = MIMEMultipart()

def format_msg(self, frm, to, subject, content, attachments):
self.msg['Subject'] = subject
self.msg['To'] = ', '.join(to)
self.msg['Date'] = str(date.today())
self.msg['From'] = frm
self.msg.attach(MIMEText(content))
self.append_attachments(attachments)

return self.msg.as_string()

def append_attachments(self, attachments):
for attachment in attachments:
try:
with open(attachment, 'rb') as fr:
file = MIMEBase('application', "octet-stream")
file.set_payload(fr.read())
encoders.encode_base64(file)
file.add_header('Content-Disposition', 'attachment',
filename=os.path.basename(attachment))
self.msg.attach(file)
except:
print("Unable to open one of the attachments. Error: ",
sys.exc_info()[0])
raise

def get_msg(self):
return self.msg.as_string()
24 changes: 15 additions & 9 deletions mail/smtp/smtp.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,29 @@
import smtplib

class smtp:
# smtp_obj =

class Smtp:

def __init__(self, *argv):
if () != argv:
self.smtp_obj = smtplib.SMTP_SSL(*argv)
else:
self.smtp_obj = self.default_setup()

# Set Gmail server as default
def default_setup(self):
return smtplib.SMTP_SSL('smtp.gmail.com', 465)
# Set Gmail server as default
self.smtp_obj = smtplib.SMTP_SSL('smtp.gmail.com', 465)

def login(self, account, passwd):
"""
You need login by gmail account.
"""
self.smtp_obj.login(account, passwd)

def send(self, frm, to, subject, content):
self.smtp_obj.sendmail(frm, to, 'Subject:' + subject + '\n' + content)
def send(self, frm, to, content):
"""
<to> must be a list
"""
self.smtp_obj.sendmail(frm, to, content)

def quit(self):
"""
Terminate the <smtp_obj> session
"""
self.smtp_obj.quit()
10 changes: 6 additions & 4 deletions tests/mail_test.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
import unittest
from tests.mail_testsuite.dummy_mail.smtp.dummy import *
from tests.mail_testsuite.dummy_mail.smtp.dummy import SmtpDummy

class mail_testcase(unittest.TestCase):

class MailTestCase(unittest.TestCase):
def setUp(self):
account = ''
password = ''
attachments = []
assert account != ''
assert password != ''
self.args = (account, password)
self.args = (account, password, attachments)

def tearDown(self):
self.args = None

def test_smtp_dummy(self):
expected = 0
result = smtp_dummy(*self.args)
result = SmtpDummy(*self.args)
self.assertEqual(expected, result)
26 changes: 13 additions & 13 deletions tests/mail_testsuite/dummy_mail/smtp/dummy.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
from mail import mail
from mail.smtp import smtp
from mail.Mail import Mail
from mail.smtp.Smtp import Smtp
from mail.mime.Mime import Mime

def smtp_dummy(acnt, pswd):
frm = acnt
to = acnt

Mail = mail.mail(smtp.smtp)

Mail.login(acnt, pswd)

Mail.send(frm, to, 'Dummy mail from USCC LAB', 'Hello members : )')

Mail.quit()

def SmtpDummy(acnt, pswd, *attachments):
_frm = acnt
_to = [acnt]

_mail = Mail(Smtp)
_mail.login(acnt, pswd)
_mail.send(_frm, _to, Mime().format_msg(
_frm, _to, 'Dummy mail from USCC LAB', 'Hello members : )', attachments))
_mail.quit()

return 0