Api for sending mail

This commit is contained in:
Gabriel Detraz 2018-11-14 17:08:25 +01:00 committed by root
parent c3277c2e6e
commit b4906d8b25
2 changed files with 26 additions and 3 deletions

View file

@ -1,4 +1,4 @@
from .client import Re2oAPIClient
from .client import ApiSendMail, Re2oAPIClient
from . import exceptions
__all__ = ['Re2oAPIClient', 'exceptions']
__all__ = ['Re2oAPIClient', 'ApiSendMail', 'exceptions']

View file

@ -5,9 +5,11 @@ from pathlib import Path
import stat
import json
import requests
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from requests.exceptions import HTTPError
from . import endpoints
from . import exceptions
# Number of seconds before expiration where renewing the token is done
@ -556,3 +558,24 @@ class Re2oAPIClient:
self.log.debug("Viewing object under '{}' successful"
.format(endpoint))
return ret
class ApiSendMail:
"""Basic api for sending mails"""
def __init__(self, server, port, starttls=False):
"""Give here the server, the port and tls or not"""
self.connection = smtplib.SMTP(server, port)
if starttls:
self.connection.starttls()
def send_mail(self, email_from, email_to, subject, body, mode='html'):
"""Sending mail from from, to, subject and body"""
self.msg = MIMEMultipart()
self.msg['From'] = email_from
self.msg['To'] = email_to
self.msg['Subject'] = subject
self.msg.attach(MIMEText(body, mode))
self.connection.sendmail(email_from, email_to, self.msg.as_string())
def close(self):
self.connection.quit()