+
Skip to content

Use Django's Email Functionality #220

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

Merged
merged 30 commits into from
Jan 16, 2022
Merged

Use Django's Email Functionality #220

merged 30 commits into from
Jan 16, 2022

Conversation

thebeanogamer
Copy link
Member

@thebeanogamer thebeanogamer commented Oct 30, 2021

Currently we have a completely custom mail backend which was ripped from ractf/mail-usv and as a result barely works. This PR replaces that with Django's internal mail backend combined with Anymail. This means that instead of just supporting SMTP and SES (and Sendgrid, but it was never tested), we now support all of the following:

  • SMTP
  • AWS
  • MAILGUN
  • SENDGRID
  • CONSOLE
  • MAILJET
  • MANDRILL
  • POSTAL
  • POSTMARK
  • SENDINBLUE
  • SPARKPOST
  • TEST
  • DISABLED

I've tested that SMTP and AWS work, as these are the ones we officially use and I'm reasonably confident that the others will work as that's abstracted away by Anymail. I've kept the existing environment variable names, so this should be backwards compatible.

Given that we can now inline images, I've also put the RACTF logo back into emails.

image

I've also added a dev mail page to /api/v2/admin/list/ which is only enabled when the anymail.backends.test.EmailBackend email backend is used, which will show the emails the platform has sent. This should make testing around emails easier.

image

Resolves #217

@codecov
Copy link

codecov bot commented Oct 30, 2021

Codecov Report

Merging #220 (791c9e9) into master (000fa7e) will increase coverage by 0.00%.
The diff coverage is 96.96%.

Impacted file tree graph

@@           Coverage Diff           @@
##           master     #220   +/-   ##
=======================================
  Coverage   94.38%   94.38%           
=======================================
  Files         107      107           
  Lines        4699     4721   +22     
  Branches      270      270           
=======================================
+ Hits         4435     4456   +21     
- Misses        221      222    +1     
  Partials       43       43           
Impacted Files Coverage Δ
src/authentication/tests.py 100.00% <ø> (ø)
src/admin/views.py 93.33% <75.00%> (-6.67%) ⬇️
src/authentication/serializers.py 100.00% <100.00%> (ø)
src/authentication/views.py 100.00% <100.00%> (ø)
src/backend/mail.py 100.00% <100.00%> (ø)
src/backend/settings/__init__.py 76.54% <100.00%> (+3.68%) ⬆️
src/backend/settings/lint.py 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 000fa7e...791c9e9. Read the comment docs.

Copy link
Contributor

@0xAda 0xAda left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please don't make a new app for this, it will just lead to merge conflicts when we move it into core in app restructure. IMO the best place for now is in the admin app

@thebeanogamer
Copy link
Member Author

Moving it into admin is easy enough, will do that

Now we assume email is disabled if `EMAIL_PROVIDER` is unset, and send
emails to `django.core.mail.backends.dummy.EmailBackend` instead.
@thebeanogamer
Copy link
Member Author

I'm going to have another look at this once I'm awake and try and port the email templates to Django (which will also mean we can inline images), but this is more or less done.

@404dcd
Copy link
Member

404dcd commented Oct 31, 2021

Have you considered #12

@thebeanogamer
Copy link
Member Author

@404dcd This PR is just to resolve #217

@thebeanogamer
Copy link
Member Author

I'm fairly certain codecov is wrong here.

How was this in the code base before...
@thebeanogamer
Copy link
Member Author

thebeanogamer commented Dec 11, 2021

What on earth is this

except get_user_model().DoesNotExist:
password_reset_start_reject.send(sender=self.__class__, email=email)
uid = -1
token = ""
email = "noreply@ractf.co.uk"

Feels like we should alert the user somehow if they've not set this
@thebeanogamer thebeanogamer requested a review from 0xAda December 11, 2021 16:19
@thebeanogamer thebeanogamer requested a review from 0xAda December 11, 2021 16:37
Copy link
Contributor

@0xAda 0xAda left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@jchristgit jchristgit self-assigned this Jan 9, 2022
"BACKEND": "django.template.backends.jinja2.Jinja2", # TODO: This is terrible
"DIRS": [os.path.join(BASE_DIR, "templates")],
"APP_DIRS": True,
},
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be great if we could remove this in a subsequent PR.

Copy link
Collaborator

@jchristgit jchristgit left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@jchristgit
Copy link
Collaborator

Hi @thebeanogamer, can you check the code coverage error?

@jchristgit jchristgit assigned 0xAda and jchristgit and unassigned jchristgit Jan 14, 2022
@jchristgit jchristgit added the enhancement New feature or request label Jan 15, 2022
@thebeanogamer
Copy link
Member Author

thebeanogamer commented Jan 15, 2022

Remaining work here:

  • Fix coverage (or just bypass)
  • Update error handling
  • Work out what on earth this does
    except get_user_model().DoesNotExist:
    password_reset_start_reject.send(sender=self.__class__, email=email)
    uid = -1
    token = ""
    email = "noreply@ractf.co.uk"

@thebeanogamer thebeanogamer dismissed stale reviews from Bentechy66 and jerbob January 16, 2022 16:46

Stale

Copy link
Collaborator

@jchristgit jchristgit left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You really knocked it out of the park this time. Just like you did last time,
and the time before that. Knowing you and that drive of yours, we’ll be
celebrating you again next week! Congratulations on this achievement. I already
know it will be one of many.

I am not sure how, but you did. Actually, now that I think about it, how did
you do it? Maybe one day you will share your secrets, and at least one person
will be able to compete with you (preferably me). Congratulations. You well
deserve it.

@jchristgit jchristgit merged commit b383bdb into master Jan 16, 2022
@jchristgit jchristgit deleted the feature/mail-refactor branch January 16, 2022 17:07
@jchristgit jchristgit assigned thebeanogamer and unassigned 0xAda and jchristgit Jan 18, 2022
0xAda pushed a commit that referenced this pull request Sep 26, 2024
* Add Django Anymail

* Don't just disable email, use Django's builtin

* Add a page showing sent emails

This doesn't work yet, as it depends on Django's builtin email stuff. That'll come next

* Remove all our custom email code

* Actually install the Mail app

* Remove unused tests file

* Remove now-needed code from staging.py

* Remove dumb import catching

* Remove unused imports

* Just use getattr

* Make sure we have anymail extras

Currently just ses, mailgun, and sendgrid. We can add more easily.

* Enable all the different AnyMail backends

* Merge `mail` app into `admin` app

* Swap to `EmailMultiAlternatives`

* Remove some unused variables

* Add SMTP settings

* Replace references to settings.MAIL["SEND"]

Now we assume email is disabled if `EMAIL_PROVIDER` is unset, and send
emails to `django.core.mail.backends.dummy.EmailBackend` instead.

* Fix AWS variable name

* Revert change to Docker Compose

* Remove random newline

* Bring back the image in emails

* Default mail provider in dev to console

* Sendgrid is no longer a dependency

* Swap email for loop to empty

* Force email_enabled to false in testing

* Issue users tokens even when mail is disabled
How was this in the code base before...

* Change default outbound email
Feels like we should alert the user somehow if they've not set this

* Don't break email verification

* Catch SMTP errors or any errors from AnyMail

* Fix email enabled syntax in tests
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Migrate to built in django email backend
6 participants
点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载