Skip to content
This repository has been archived by the owner on Dec 4, 2022. It is now read-only.

Commit

Permalink
Modify how the email is sent and the variable for the address
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurcerveira committed May 4, 2020
1 parent c42b05a commit c07861a
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 39 deletions.
14 changes: 10 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,10 @@ $ pip install email-function-logger

## Email setup

You can set the email information as environment variables on your machine. To do this, add the following lines to `~/.bashrc` (or `~/.bash_profile` depending on your OS).
You can set the email information as environment variable on your machine. To do this, add the following line to `~/.bashrc` (or `~/.bash_profile` depending on your OS).

```bash
export LOG_SENDER_EMAIL_ADDRESS="example_sender@gmail.com"
export LOG_SENDER_EMAIL_PASSWORD="password"
export LOG_RECEIVER_EMAIL_ADDRESS="example_receiver@gmail.com"
export EMAIL_ADDRESS="example@domain.com"
```

This step is optional since you can input the information during the function runtime.
Expand Down Expand Up @@ -56,10 +54,18 @@ End time: Mar 16 18:32:16
Total execution time: 00:00:00
```

## How it works

The decorator receives the function information and send a request to a Flask API hosted on Heroku (available [here](https://github.com/arthurcerveira/Email-Bot)). This API then sends the email to the address specified in the `EMAIL_ADDRESS` variable.

## Supported versions

- Python 3.2 and above

## Author

- [Arthur Cerveira](https://github.com/arthurcerveira)

## License

- MIT License
Expand Down
60 changes: 25 additions & 35 deletions email_function_logger/function_logger.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,26 @@
import smtplib
import requests

import os
import io
import sys

from datetime import datetime
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

ENVIRON_VAR = ['LOG_SENDER_EMAIL_ADDRESS',
'LOG_SENDER_EMAIL_PASSWORD',
'LOG_RECEIVER_EMAIL_ADDRESS']

RECIPIENT_VAR = 'EMAIL_ADDRESS'


def log_function(function):
email, password, recipient = get_email_info()
session = authenticate_email(email, password)
message = create_message(email, recipient)
recipient = get_recipient()

def log_function_wrapper(*args, **kwargs):
# If recipient is not defined, just run the function
if recipient == "":
return function(*args, **kwargs)

arguments = get_function_arguments(args, kwargs)

# Add subject to email
subject = "Function '{}' execution log".format(function.__name__)
message['Subject'] = subject

# Start email body text
text = "Function {}({}) finished its execution.\n\n".format(
function.__name__, arguments)

Expand All @@ -37,6 +33,7 @@ def log_function_wrapper(*args, **kwargs):
if text_output:
text += 'Function text output:\n'
for op in text_output:
print(op)
text += op + '\n'
else:
text += 'No text output\n'
Expand All @@ -53,35 +50,27 @@ def log_function_wrapper(*args, **kwargs):
text += '\nTotal execution time: {0:02d}:{1:02d}:{2:02d}\n'.format(
hours, minutes, seconds)

# Add body to email
message.attach(MIMEText(text, 'plain'))
body = message.as_string()
send_email(recipient, text, subject)

session.sendmail(email, recipient, body)
session.quit()
return return_value

return log_function_wrapper


def get_email_info():
for var in ENVIRON_VAR:
yield os.environ.get(var) if os.environ.get(var) else input('{}: '.format(var))
def send_email(recipient, text, subject):
data = {
"recipient": recipient,
"text": text,
"subject": subject
}

requests.post("https://arthur-email-bot.herokuapp.com/send", json=data)

def authenticate_email(email, password):
session = smtplib.SMTP('smtp.gmail.com', 587)
session.starttls()
session.login(email, password)

return session


def create_message(email, recipient):
message = MIMEMultipart()
message['From'] = email
message['To'] = recipient

return message
def get_recipient():
return os.environ.get(RECIPIENT_VAR) \
if os.environ.get(RECIPIENT_VAR) \
else input('{}: '.format(RECIPIENT_VAR))


def get_function_arguments(args, kwargs):
Expand All @@ -97,7 +86,8 @@ def __enter__(self):
self._stdout = sys.stdout
sys.stdout = self._stringio = io.StringIO()
return self

def __exit__(self, *args):
self.extend(self._stringio.getvalue().splitlines())
del self._stringio
sys.stdout = self._stdout
sys.stdout = self._stdout

0 comments on commit c07861a

Please sign in to comment.