receive emails in django

Django 5.1.3
Python 3.12.4

Topic: Trying to set up a POC for receiving emails using django_mailbox and gmail addy, or should I be using Gmail API?

Problem: Almost no information available to do this. What there is is old, outdated or does not apply. Yet I read receiving email is the easy part. Sending is harder. According to google i need to use oauth specifically oauth2.py file I can download. Wait what? I thought Allauth is a Python library, specifically designed for Django? Also I don’t see anything in the Django backend where I would set that up. So much confusing info out there to just set up a POC to receive email. It’s even confusing on how to get an email from gmail to do this. Once again Django docs show pretty much nothing. I appreciate any and all help.

I have installed django-mailbox and went to Django admin and see the mailbox config info.

To be clear: emails are not sent to Django. Emails get delivered to a mailbox somewhere.

The package you’re referencing (django-mailbox) reads emails from that mailbox, copies them to a Model, then deletes it from the server.

For it to do that, it needs to contact the email server, with access to the mailbox. This means that some kind of authentication to the email server will be required. Whatever Google is telling you to do, is what you should do.

oauth is an authentication protocol, which can be implemented by any number of libraries. There would not be any requirement to use any specific library if they all adhere to that protocol. (However, it’s not uncommon for library authors to add extensions, in which case it may be necessary to use specific implementations.)

Once you have authenticated to the email server, your code can then read the mailbox. The two standard protocols for this are named POP3 and IMAP.

Side note: It’s the responsibility of the authors of third-party libraries to document how their libraries are to be configured and used. It’s simply not possible for the Django documentation to do that. In this case, using django-mailbox with gmail is documented at Supported Mailbox Types — django-mailbox 3.3 documentation

Side note 2: The django-mailbox library identifies compatibility with up to Django 5.0. They do not document compatibility with Django 5.1 You could encounter version-related issues with it.

If you’re just looking for a quick POC, I would avoid Gmail or M365, since having to deal with their specific auth flows is just an unnecessary headache. Most other email hosts will let you access a mailbox with regular IMAP settings, and then you can just follow the docs to set up the URI - there’s even some help text under the URI field in your screenshot to show what it should look like. I haven’t used django-mailbox, but it looks fairly straight forward (if you avoid the Oauth flows.)

For a more robust solution for inbound mail, I would probably look to use an API-based solution like SendGrid, Mailgun, or countless others. With an API solution, the email provider will receive the email, and send it to your Django site via an API endpoint that you host. There are other ways of doing it - e.g. a webhook to notify you that there are new messages. Have a look at the Mailgun docs for some examples: Recieving, Forwarding,and Storing Messages

Thank you Ken. I appreciate your help.

Thank you so much Stuart. I will look into some of your suggestions.