django的邮件email功能

注意

测试的时候python manage.py test -p "test_tasks.py" -v 3,默认使用的EMAIL_BACKEND配置为:'django.core.mail.backends.locmem.EmailBackend',此时,在settings.py里的配置项EMAIL_BACKEND是不生效的;
如果想在测试的时候剩下,需要另外配置,比如:
#!/usr/bin/env python
# -- coding: utf-8 --

from django.test import TestCase
import unittest
from plugin_security.tasks import send_email
from django.conf import settings

class TestEmail(TestCase):

# @unittest.skip('skip')
def test_send_email(self):
    # 这个配置必须加,因为django测试环境,修改了默认配置,导致邮件发送失败
    # 参考/Users/xxx/.virtualenvs/erebus_app/lib/python3.6/site-packages/django/test/utils.py里的函数setup_test_environment
    settings.EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
    res = send_email()
    self.assertEqual(res, True)

django的默认全局配置:

/Users/xxx/.virtualenvs/erebus_app/lib/python3.6/site-packages/django/conf/global_settings.py

配置文档

https://docs.djangoproject.com/en/2.2/topics/email/

原文地址:https://www.cnblogs.com/shengulong/p/11001000.html