Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement email notifications #50

Closed
wasimsandhu opened this issue Nov 19, 2022 · 2 comments
Closed

Implement email notifications #50

wasimsandhu opened this issue Nov 19, 2022 · 2 comments
Labels
new feature New feature or request

Comments

@wasimsandhu
Copy link
Contributor

wasimsandhu commented Nov 19, 2022

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 wasimsandhu self-assigned this Nov 19, 2022
@wasimsandhu wasimsandhu added the new feature New feature or request label Nov 19, 2022
@wasimsandhu
Copy link
Contributor Author

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): iterative/PyDrive2#179

@wasimsandhu
Copy link
Contributor Author

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

@wasimsandhu wasimsandhu removed their assignment Feb 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
new feature New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant