czbiohub-sf / Rapid-QC-MS

Realtime quality control for mass spectrometry data acquisition
https://czbiohub-sf.github.io/Rapid-QC-MS
Other
15 stars 2 forks source link

Implement email notifications #50

Closed wasimsandhu closed 1 year ago

wasimsandhu commented 2 years ago
import smtplib, ssl

port = 465  # For SSL
smtp_server = "smtp.gmail.com"
sender_email = "my@gmail.com"  # Enter your address
receiver_email = "your@gmail.com"  # Enter receiver address
password = input("Type your password and press enter: ")
message = """\
Subject: Hi there

This message is sent from Python."""

context = ssl.create_default_context()
with smtplib.SMTP_SSL(smtp_server, port, context=context) as server:
    server.login(sender_email, password)
    server.sendmail(sender_email, receiver_email, message)
wasimsandhu commented 2 years ago

The above code snippet will not work. It's also not ideal to have to encrypt / obscure the user's email account password for this one feature. In the interest of time, going to see if I can just use the user's authenticated Google account for this...

See issue here regarding using Google credentials (since entire authentication process is handled by PyDrive): https://github.com/iterative/PyDrive2/issues/179

wasimsandhu commented 1 year ago
def send_email(subject, message_body):

    """
    Sends email using Google authenticated credentials
    """

    try:
        credentials = google_auth.load_credentials_from_file(alt_credentials)[0]

        service = build("gmail", "v1", credentials=credentials)
        message = EmailMessage()

        message.set_content(message_body)

        message["Subject"] = subject
        message["To"] = get_email_notifications_list(as_string=True)

        encoded_message = base64.urlsafe_b64encode(message.as_bytes()).decode()
        create_message = { "raw": encoded_message }

        send_message = (service.users().messages().send(userId="me", body=create_message).execute())

    except Exception as error:
        traceback.print_exc()
        send_message = None

    return send_message