From 055b2ffe21ce32222c88d8905652a964344aa8b9 Mon Sep 17 00:00:00 2001 From: s8w1e2ep Date: Fri, 28 Jul 2017 16:13:18 +0800 Subject: [PATCH 1/5] Extend the function of sending with attachments --- mail/mail.py | 4 ++-- mail/smtp/smtp.py | 39 +++++++++++++++++++++++++++++++++++++-- 2 files changed, 39 insertions(+), 4 deletions(-) diff --git a/mail/mail.py b/mail/mail.py index d500bb7..16c6b16 100644 --- a/mail/mail.py +++ b/mail/mail.py @@ -5,8 +5,8 @@ def __init__(self, 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, subject, content, *attachment): + self.prot.send(frm, to, subject, content, attachment) def quit(self): self.prot.quit() diff --git a/mail/smtp/smtp.py b/mail/smtp/smtp.py index fcd544f..1f012f2 100644 --- a/mail/smtp/smtp.py +++ b/mail/smtp/smtp.py @@ -1,4 +1,11 @@ +import os +import sys import smtplib +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 smtp: # smtp_obj = @@ -16,8 +23,36 @@ def default_setup(self): def login(self, account, passwd): 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, subject, content, *attachments): + msg = self.render_msg(frm, to, subject, content, attachments) + self.smtp_obj.sendmail(frm, to, msg) + + def render_msg(self, frm, to, subject, content, attachments): + msg = MIMEMultipart() + msg['Subject'] = subject + msg['To'] = ', '.join(to) + msg['Date'] = str(date.today()) + msg['From'] = frm + msg.attach(MIMEText(content)) + msg = self.append_attachments(msg, attachments) + + return msg.as_string() + + def append_attachments(self, msg, attachments): + for attachment in attachments: + try: + with open(attachment, 'rb') as fp: + file = MIMEBase('application', "octet-stream") + file.set_payload(fp.read()) + encoders.encode_base64(file) + file.add_header('Content-Disposition', 'attachment', + filename=os.path.basename(attachment)) + msg.attach(file) + except: + print("Unable to open one of the attachments. Error: ", sys.exc_info()[0]) + raise + + return msg def quit(self): self.smtp_obj.quit() From e0aef1046253b6eb1986325147b11cf6b1d876e0 Mon Sep 17 00:00:00 2001 From: s8w1e2ep Date: Fri, 28 Jul 2017 16:22:04 +0800 Subject: [PATCH 2/5] [Unittest] add a variable of attachments for send testing --- tests/mail_test.py | 3 ++- tests/mail_testsuite/dummy_mail/smtp/dummy.py | 9 +++------ 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/tests/mail_test.py b/tests/mail_test.py index a054b62..50fd61c 100644 --- a/tests/mail_test.py +++ b/tests/mail_test.py @@ -5,9 +5,10 @@ class mail_testcase(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 diff --git a/tests/mail_testsuite/dummy_mail/smtp/dummy.py b/tests/mail_testsuite/dummy_mail/smtp/dummy.py index f04a7ea..d4031db 100644 --- a/tests/mail_testsuite/dummy_mail/smtp/dummy.py +++ b/tests/mail_testsuite/dummy_mail/smtp/dummy.py @@ -1,16 +1,13 @@ from mail import mail from mail.smtp import smtp -def smtp_dummy(acnt, pswd): +def smtp_dummy(acnt, pswd, *attachments): frm = acnt - to = acnt + to = [acnt] Mail = mail.mail(smtp.smtp) - Mail.login(acnt, pswd) - - Mail.send(frm, to, 'Dummy mail from USCC LAB', 'Hello members : )') - + Mail.send(frm, to, 'Dummy mail from USCC LAB', 'Hello members : )', attachments) Mail.quit() return 0 From 77b5b8db1b0917eb7ee7cec35cc9599af95a0f97 Mon Sep 17 00:00:00 2001 From: s8w1e2ep Date: Wed, 6 Sep 2017 17:39:57 +0800 Subject: [PATCH 3/5] Divide formatMsg from Smtp to Mime --- mail/mime/Mime.py | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 mail/mime/Mime.py 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() From c0e9ce09b117d2877603dd5f7b1b605251d07310 Mon Sep 17 00:00:00 2001 From: s8w1e2ep Date: Wed, 6 Sep 2017 17:41:44 +0800 Subject: [PATCH 4/5] Simplify Mail class and Smtp class --- mail/mail.py | 11 ++++++--- mail/smtp/smtp.py | 59 ++++++++++++----------------------------------- 2 files changed, 23 insertions(+), 47 deletions(-) diff --git a/mail/mail.py b/mail/mail.py index 16c6b16..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, *attachment): - self.prot.send(frm, to, subject, content, attachment) + def send(self, frm, to, content): + self.prot.send(frm, to, content) def quit(self): self.prot.quit() diff --git a/mail/smtp/smtp.py b/mail/smtp/smtp.py index 1f012f2..2108b6c 100644 --- a/mail/smtp/smtp.py +++ b/mail/smtp/smtp.py @@ -1,58 +1,29 @@ -import os -import sys import smtplib -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 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, *attachments): - msg = self.render_msg(frm, to, subject, content, attachments) - self.smtp_obj.sendmail(frm, to, msg) - - def render_msg(self, frm, to, subject, content, attachments): - msg = MIMEMultipart() - msg['Subject'] = subject - msg['To'] = ', '.join(to) - msg['Date'] = str(date.today()) - msg['From'] = frm - msg.attach(MIMEText(content)) - msg = self.append_attachments(msg, attachments) - - return msg.as_string() - - def append_attachments(self, msg, attachments): - for attachment in attachments: - try: - with open(attachment, 'rb') as fp: - file = MIMEBase('application', "octet-stream") - file.set_payload(fp.read()) - encoders.encode_base64(file) - file.add_header('Content-Disposition', 'attachment', - filename=os.path.basename(attachment)) - msg.attach(file) - except: - print("Unable to open one of the attachments. Error: ", sys.exc_info()[0]) - raise - - return msg + 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() From 4f4cbc6bd3ae8c65a37d070e2fd75cabec55c145 Mon Sep 17 00:00:00 2001 From: s8w1e2ep Date: Wed, 6 Sep 2017 17:42:27 +0800 Subject: [PATCH 5/5] Make all python files follow PEP8 code style --- tests/mail_test.py | 9 ++++--- tests/mail_testsuite/dummy_mail/smtp/dummy.py | 25 +++++++++++-------- 2 files changed, 19 insertions(+), 15 deletions(-) diff --git a/tests/mail_test.py b/tests/mail_test.py index 50fd61c..aa05593 100644 --- a/tests/mail_test.py +++ b/tests/mail_test.py @@ -1,11 +1,12 @@ 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 = '' + attachments = [] assert account != '' assert password != '' self.args = (account, password, attachments) @@ -15,5 +16,5 @@ def tearDown(self): 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 d4031db..18d4423 100644 --- a/tests/mail_testsuite/dummy_mail/smtp/dummy.py +++ b/tests/mail_testsuite/dummy_mail/smtp/dummy.py @@ -1,13 +1,16 @@ -from mail import mail -from mail.smtp import smtp - -def smtp_dummy(acnt, pswd, *attachments): - frm = acnt - to = [acnt] - - Mail = mail.mail(smtp.smtp) - Mail.login(acnt, pswd) - Mail.send(frm, to, 'Dummy mail from USCC LAB', 'Hello members : )', attachments) - Mail.quit() +from mail.Mail import Mail +from mail.smtp.Smtp import Smtp +from mail.mime.Mime import Mime + + +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