Added method for send email
This commit is contained in:
parent
97ab25c162
commit
1a0d11d829
2 changed files with 119 additions and 0 deletions
102
paramecio/citoplasma/sendmail.py
Normal file
102
paramecio/citoplasma/sendmail.py
Normal file
|
|
@ -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
|
||||
|
||||
|
||||
17
tests/sendmailtest.py
Normal file
17
tests/sendmailtest.py
Normal file
|
|
@ -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 <b>html</b>', content_type='html', attachments=[]) )
|
||||
|
||||
s.smtp.quit()
|
||||
Loading…
Add table
Add a link
Reference in a new issue