core.utils.mail
View Source
import json from urllib.parse import quote_plus import requests from core.utils.external import NumpyEncoder class Mail(): """ The mail class provides the possibility to send mails via an API. """ _TOKEN = "sometoken" _API = "https://api.example.com/mail/send.php" DEFAULT_TO = "mail@example.com" """ The default to-address. """ DEFAULT_SUBJECT = "MA-Evaluation" """ The default subject. """ def _run_request(content, subject, to): url = Mail._API + "?" + '&'.join([ "t=" + quote_plus(Mail._TOKEN), "to=" + quote_plus(to), "subject=" + quote_plus(subject) ]) try: r = requests.post(url, data={"content" : content}, timeout=5) ok = r.status_code == 200 except: ok = False return ok def send(content, subject=None, to=None): """ Sends an e-mail. Args: content (string): The content of the mail as *plain text* subject (string): The subject of the mail, else will use `DEFAULT_SUBJECT` to (string): The receiver of the mail, else will use `DEFAULT_TO` Returns: bool if successful """ if subject == None: subject = Mail.DEFAULT_SUBJECT if to == None: to = Mail.DEFAULT_TO if not Mail._run_request(content, subject, to): print("Error while sending Mail!") return False else: return True def send_variable(variable, **kwargs): """ Sends an e-mail. Args: variable (dict, list, ...): The content of the mail to send, will be json-encoded to string **kwargs: passed to `send()` Returns: bool if successful """ return Mail.send(json.dumps(variable, indent=2, cls=NumpyEncoder), **kwargs)
View Source
class Mail(): """ The mail class provides the possibility to send mails via an API. """ _TOKEN = "sometoken" _API = "https://api.example.com/mail/send.php" DEFAULT_TO = "mail@example.com" """ The default to-address. """ DEFAULT_SUBJECT = "MA-Evaluation" """ The default subject. """ def _run_request(content, subject, to): url = Mail._API + "?" + '&'.join([ "t=" + quote_plus(Mail._TOKEN), "to=" + quote_plus(to), "subject=" + quote_plus(subject) ]) try: r = requests.post(url, data={"content" : content}, timeout=5) ok = r.status_code == 200 except: ok = False return ok def send(content, subject=None, to=None): """ Sends an e-mail. Args: content (string): The content of the mail as *plain text* subject (string): The subject of the mail, else will use `DEFAULT_SUBJECT` to (string): The receiver of the mail, else will use `DEFAULT_TO` Returns: bool if successful """ if subject == None: subject = Mail.DEFAULT_SUBJECT if to == None: to = Mail.DEFAULT_TO if not Mail._run_request(content, subject, to): print("Error while sending Mail!") return False else: return True def send_variable(variable, **kwargs): """ Sends an e-mail. Args: variable (dict, list, ...): The content of the mail to send, will be json-encoded to string **kwargs: passed to `send()` Returns: bool if successful """ return Mail.send(json.dumps(variable, indent=2, cls=NumpyEncoder), **kwargs)
The mail class provides the possibility to send mails via an API.
The default to-address.
The default subject.
View Source
def send(content, subject=None, to=None): """ Sends an e-mail. Args: content (string): The content of the mail as *plain text* subject (string): The subject of the mail, else will use `DEFAULT_SUBJECT` to (string): The receiver of the mail, else will use `DEFAULT_TO` Returns: bool if successful """ if subject == None: subject = Mail.DEFAULT_SUBJECT if to == None: to = Mail.DEFAULT_TO if not Mail._run_request(content, subject, to): print("Error while sending Mail!") return False else: return True
Sends an e-mail.
Args
- content (string): The content of the mail as plain text
- subject (string): The subject of the mail, else will use
DEFAULT_SUBJECT
- to (string): The receiver of the mail, else will use
DEFAULT_TO
Returns
bool if successful
View Source
def send_variable(variable, **kwargs): """ Sends an e-mail. Args: variable (dict, list, ...): The content of the mail to send, will be json-encoded to string **kwargs: passed to `send()` Returns: bool if successful """ return Mail.send(json.dumps(variable, indent=2, cls=NumpyEncoder), **kwargs)
Sends an e-mail.
Args
- variable (dict, list, ...): The content of the mail to send, will be json-encoded to string
- **kwargs: passed to
send()
Returns
bool if successful