-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
61 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
#!/usr/bin/env python | ||
# coding: utf-8 | ||
|
||
from .shell_commander import ShellCommander | ||
|
||
|
||
class SendmailNotFound(Exception): | ||
pass | ||
|
||
|
||
class Sendmail(ShellCommander): | ||
""" | ||
""" | ||
CMD = 'sendmail' | ||
|
||
def __init__(self): | ||
""" | ||
""" | ||
super().__init__() | ||
|
||
async def send(self, msg): | ||
""" | ||
""" | ||
args = ['-t', '-oi'] | ||
|
||
return await self.start(__class__.CMD, args, msg) | ||
|
||
def handle_error(self, err): | ||
""" | ||
""" | ||
msg = err.decode() | ||
|
||
print(f"FIXME (raise a custom Exception) - {msg}") | ||
|
||
|
||
async def send(from_addr, to_addr, subject='Ellis', msg='', **kwargs): | ||
""" | ||
Uses `sendmail` to send an e-mail to the provided address. | ||
""" | ||
headers = f"To: {to_addr}" | ||
headers = f"{headers}\nFrom: {from_addr}" | ||
headers = f"{headers}\nSubject: {subject}" | ||
|
||
msg = f"{headers}\n\n{msg}" | ||
|
||
if kwargs: | ||
# To append kwargs to the given message, we first transform it | ||
# into a more human friendly string: | ||
values = "\n".join([f"{k}: {v}" for k, v in kwargs.items()]) | ||
|
||
# Actually append caught values to the message: | ||
msg = f"{msg}\n\nThe following variables have been caught:\n{values}" | ||
|
||
# Message MUST be `bytes`: | ||
email = msg.encode('utf-8') | ||
|
||
# Finally, we can send the e-mail: | ||
sendmail = Sendmail() | ||
|
||
return await sendmail.send(email) |