diff --git a/mail/mail.py b/mail/mail.py index d500bb7..289079e 100644 --- a/mail/mail.py +++ b/mail/mail.py @@ -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() diff --git a/mail/mime/Mime.py b/mail/mime/Mime.py new file mode 100644 index 0000000..6227606 --- /dev/null +++ b/mail/mime/Mime.py @@ -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() diff --git a/mail/smtp/smtp.py b/mail/smtp/smtp.py index fcd544f..2108b6c 100644 --- a/mail/smtp/smtp.py +++ b/mail/smtp/smtp.py @@ -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): + """ + must be a list + """ + self.smtp_obj.sendmail(frm, to, content) def quit(self): + """ + Terminate the session + """ self.smtp_obj.quit() diff --git a/tests/mail_test.py b/tests/mail_test.py index a054b62..aa05593 100644 --- a/tests/mail_test.py +++ b/tests/mail_test.py @@ -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) diff --git a/tests/mail_testsuite/dummy_mail/smtp/dummy.py b/tests/mail_testsuite/dummy_mail/smtp/dummy.py index f04a7ea..18d4423 100644 --- a/tests/mail_testsuite/dummy_mail/smtp/dummy.py +++ b/tests/mail_testsuite/dummy_mail/smtp/dummy.py @@ -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