| 1 | import email.policy
|
|---|
| 2 | import django
|
|---|
| 3 | from django.conf import settings
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 | settings.configure(DEBUG=True, SECRET_KEY="poc-only", ROOT_URLCONF="urls_stub",
|
|---|
| 7 | EMAIL_BACKEND="django.core.mail.backends.locmem.EmailBackend")
|
|---|
| 8 | django.setup()
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 | from django.core.mail import EmailMessage, send_mail
|
|---|
| 12 | from django.core.validators import URLValidator, validate_domain_name, validate_email
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 | SEP = "\u2028" # LINE SEPARATOR (category Zl)
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 | for v in [
|
|---|
| 19 | f"user@example{SEP}.com",
|
|---|
| 20 | "user@example\u2029.com", # PARAGRAPH SEPARATOR (Zp)
|
|---|
| 21 | "user@exa\u1680mple.com", # OGHAM SPACE MARK (Zs)
|
|---|
| 22 | "user@exa\u2000mple.com", # EN QUAD (Zs)
|
|---|
| 23 | "user@example\u3000.com", # IDEOGRAPHIC SPACE (Zs)
|
|---|
| 24 | "user@example\ufeff.com", # ZERO WIDTH NO-BREAK SPACE (Cf)
|
|---|
| 25 | ]:
|
|---|
| 26 | try:
|
|---|
| 27 | validate_email(v)
|
|---|
| 28 | print(f"EmailValidator ACCEPT {v!r}")
|
|---|
| 29 | except Exception:
|
|---|
| 30 | print(f"EmailValidator reject {v!r}")
|
|---|
| 31 |
|
|---|
| 32 |
|
|---|
| 33 | for d in ["example\u2028.com", "example\u2029.com", "exa\u1680mple.com"]:
|
|---|
| 34 | try:
|
|---|
| 35 | validate_domain_name(d)
|
|---|
| 36 | print(f"DomainNameValidator ACCEPT {d!r}")
|
|---|
| 37 | except Exception:
|
|---|
| 38 | print(f"DomainNameValidator reject {d!r}")
|
|---|
| 39 |
|
|---|
| 40 |
|
|---|
| 41 | uv = URLValidator()
|
|---|
| 42 | for u in ["http://example\u2028.com/", "http://exa\u1680mple.com/"]:
|
|---|
| 43 | try:
|
|---|
| 44 | uv(u)
|
|---|
| 45 | print(f"URLValidator ACCEPT {u!r}")
|
|---|
| 46 | except Exception:
|
|---|
| 47 | print(f"URLValidator reject {u!r}")
|
|---|
| 48 |
|
|---|
| 49 |
|
|---|
| 50 | print("splitlines:", "a\u2028b".splitlines())
|
|---|
| 51 |
|
|---|
| 52 |
|
|---|
| 53 | addr = f"user@example{SEP}.com"
|
|---|
| 54 | for pol in (None, email.policy.SMTP):
|
|---|
| 55 | m = EmailMessage(subject="s", body="b", to=[addr])
|
|---|
| 56 | try:
|
|---|
| 57 | m.message(policy=pol)
|
|---|
| 58 | print(f"message(policy={pol!r}): built OK")
|
|---|
| 59 | except Exception as e:
|
|---|
| 60 | print(f"message(policy={pol!r}) -> {type(e).__name__}: {e}")
|
|---|
| 61 |
|
|---|
| 62 |
|
|---|
| 63 | try:
|
|---|
| 64 | send_mail("s", "b", "from@example.com", [addr])
|
|---|
| 65 | print("send_mail(): sent OK")
|
|---|
| 66 | except Exception as e:
|
|---|
| 67 | print(f"send_mail() -> {type(e).__name__}: {e}")
|
|---|
| 68 |
|
|---|
| 69 |
|
|---|
| 70 | from django.core.mail.backends.smtp import EmailBackend
|
|---|
| 71 | be = EmailBackend.__new__(EmailBackend)
|
|---|
| 72 | print("prep_address(%r) = %r" % (addr, be.prep_address(addr)))
|
|---|