Python Forum

Full Version: How to send notifications to gmail from contact form using Django and pushbullet
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I'm helping someone update a website that was built a few years ago using Django. On the site there is a contact form that a user fills out and the request is sent to the site admin. My friend wants to get notifications to their gmail account also when the contact form is submitted. The previous developer wrote a code that seems to attempt to do this with Pushbullet API. I'm not familiar with using Pushbullet so I'm trying to find out how this is supposed to work. If anyone is familiar with this way of sending notifications to gmail via the Pushbullet API please enlighten me a bit. I've included the code that is supposed to accomplish the goal.

  

import logging
import os
from typing import List

from django.core.mail import EmailMessage
from pushbullet import PushBullet

# todo Add mr tires api to .env
pb = PushBullet(os.getenv("PUSHBULLET_API"))

# Get an instance of a logger
logger = logging.getLogger('mail_admins')


class Notify:
    subject: str
    message: str
    from_: str = "[email protected]"
    to: List[str] = [
        '[email protected]'
        '[email protected]',
        # '[email protected]',
    ]
    bcc: List[str] = [
        # '[email protected]',
        '[email protected]'
    ]
    reply_to: List[str] = ['[email protected]']

    @staticmethod
    def send(subject: str, message=''):
        try:
            logger.info(f"Sending email:\nSubject: {subject}\nMessage:{message}...")
            email = EmailMessage(subject, message, Notify.from_, Notify.to, Notify.bcc, reply_to=Notify.reply_to)
            email.send(fail_silently=False)
        except Exception as e:
            logger.error("Failed to send email", e)
        try:
            logger.info("Attempting to send pushbullet message.")
            pb.push_note(subject, message, email="[email protected]")
            pb.push_note(subject, message)
        except Exception as e:
            logger.error("Failed to Pushbullet message", e)
            pass


if __name__ == '__main__':
    # notify = Notify()
    # notify.send("TestSubject", "TestMessage")
    push = pb.push_note("Test", "This is the body", email="[email protected]")