diff --git a/paramecio/citoplasma/sendmail.py b/paramecio/citoplasma/sendmail.py new file mode 100644 index 0000000..402d905 --- /dev/null +++ b/paramecio/citoplasma/sendmail.py @@ -0,0 +1,102 @@ +#!/usr/bin/python3 + +import smtplib +import mimetypes +from email import encoders +from email.message import Message +from email.mime.audio import MIMEAudio +from email.mime.base import MIMEBase +from email.mime.image import MIMEImage +from email.mime.multipart import MIMEMultipart +from email.mime.text import MIMEText + +class SendMail: + + port=25 + + host='localhost' + + username='' + + password='' + + ssl=False + + txt_error='' + + def __init__(self): + + self.smtp=smtplib.SMTP(host=self.host, port=self.port) + + def send(self, from_address, to_address, subject, message, content_type='plain', attachments=[]): + + if self.ssl==True: + + try: + + self.smtp.starttls() + + except SMTPHeloError: + + self.txt_error='Error: cannot make HELO to this server' + + return False + + except SMTPNotSupportedError: + + self.txt_error='Error: SSL/TLS is not supported' + + return False + + except RuntimeError: + + self.txt_error='Error: SSL/TLS is not supported in your python interpreter' + + return False + + if self.username!='': + + try: + + self.smtp.login(self.username, self.password) + + except SMTPHeloError: + + self.txt_error='Error: cannot make HELO to this server' + + return False + + except SMTPAuthenticationError: + + self.txt_error='Error: cannot login. Wrong username or password' + + return False + + except SMTPNotSupportedError: + + self.txt_error='Error: AUTH is not supported' + + return False + + except SMTPException: + + self.txt_error='Error: any method for login is avaliable' + + return False + + COMMASPACE=', ' + + if len(attachments)==0: + + msg=MIMEText(message, content_type) + + msg['Subject']=subject + msg['From']=from_address + + msg['To']=COMMASPACE.join(to_address) + + self.smtp.send_message(msg) + + return True + + \ No newline at end of file diff --git a/tests/sendmailtest.py b/tests/sendmailtest.py new file mode 100644 index 0000000..d5f28b1 --- /dev/null +++ b/tests/sendmailtest.py @@ -0,0 +1,17 @@ +from settings import config +from paramecio.citoplasma import sendmail +import unittest + +class TestFieldMethods(unittest.TestCase): + + def test_sendmail(self): + + s=sendmail.SendMail() + + #self.assertEqual(phrase, 'this---is-a-crap-phrase-o---f-oh-yeah--') + + self.assertTrue( s.send(config.portal_email, [config.email_test], 'This is a test', 'A message for test a simple email method', content_type='plain', attachments=[]) ) + + self.assertTrue( s.send(config.portal_email, [config.email_test], 'This is a test', 'A message for test a simple email method in html', content_type='html', attachments=[]) ) + + s.smtp.quit() \ No newline at end of file