|  | 
|  | 1 | +""" | 
|  | 2 | +Tools for sending email. | 
|  | 3 | +""" | 
|  | 4 | +from typing import Any, Iterable, Sequence | 
|  | 5 | + | 
|  | 6 | +from fastapi_django.conf import settings | 
|  | 7 | +# Imported for backwards compatibility and for the sake | 
|  | 8 | +# of a cleaner namespace. These symbols used to be in | 
|  | 9 | +# fastapi_django/core/mail.py before the introduction of email | 
|  | 10 | +# backends and the subsequent reorganization (See #10355) | 
|  | 11 | +from fastapi_django.mail.message import ( | 
|  | 12 | +    DEFAULT_ATTACHMENT_MIME_TYPE, | 
|  | 13 | +    BadHeaderError, | 
|  | 14 | +    EmailAlternative, | 
|  | 15 | +    EmailAttachment, | 
|  | 16 | +    EmailMessage, | 
|  | 17 | +    EmailMultiAlternatives, | 
|  | 18 | +    SafeMIMEMultipart, | 
|  | 19 | +    SafeMIMEText, | 
|  | 20 | +    forbid_multi_line_headers, | 
|  | 21 | +    make_msgid, | 
|  | 22 | +) | 
|  | 23 | +from fastapi_django.mail.utils import DNS_NAME, CachedDnsName, get_provider, get_required, get_option | 
|  | 24 | + | 
|  | 25 | +__all__ = [ | 
|  | 26 | +    "CachedDnsName", | 
|  | 27 | +    "DNS_NAME", | 
|  | 28 | +    "EmailMessage", | 
|  | 29 | +    "EmailMultiAlternatives", | 
|  | 30 | +    "SafeMIMEText", | 
|  | 31 | +    "SafeMIMEMultipart", | 
|  | 32 | +    "DEFAULT_ATTACHMENT_MIME_TYPE", | 
|  | 33 | +    "make_msgid", | 
|  | 34 | +    "BadHeaderError", | 
|  | 35 | +    "forbid_multi_line_headers", | 
|  | 36 | +    "get_connection", | 
|  | 37 | +    "send_mail", | 
|  | 38 | +    "send_mass_mail", | 
|  | 39 | +    "EmailAlternative", | 
|  | 40 | +    "EmailAttachment", | 
|  | 41 | +    "outbox" | 
|  | 42 | +] | 
|  | 43 | + | 
|  | 44 | +from fastapi_django.utils.module_loading import import_string | 
|  | 45 | + | 
|  | 46 | +outbox: list = [] | 
|  | 47 | + | 
|  | 48 | + | 
|  | 49 | +def get_connection(fail_silently: bool = False, provider: str = settings.DEFAULT_EMAIL_PROVIDER_ALIAS, **kw: Any): | 
|  | 50 | +    backend = get_required(provider, "BACKEND") | 
|  | 51 | +    klass = import_string(backend) | 
|  | 52 | +    return klass(fail_silently=fail_silently, provider=provider, **kw) | 
|  | 53 | + | 
|  | 54 | + | 
|  | 55 | +async def send_mail( | 
|  | 56 | +    subject: str, | 
|  | 57 | +    body: str, | 
|  | 58 | +    recipient_list: Sequence[str], | 
|  | 59 | +    from_email: str | None = None, | 
|  | 60 | +    fail_silently: bool = False, | 
|  | 61 | +    html_message=None, | 
|  | 62 | +    connection=None, | 
|  | 63 | +    provider: str = settings.DEFAULT_EMAIL_PROVIDER_ALIAS, | 
|  | 64 | +): | 
|  | 65 | +    """ | 
|  | 66 | +    Если передан connection, то using игнорируется | 
|  | 67 | +    """ | 
|  | 68 | +    connection = connection or get_connection(fail_silently=fail_silently, provider=provider) | 
|  | 69 | +    from_email = from_email or get_option(provider, "from_email") | 
|  | 70 | +    mail = EmailMultiAlternatives( | 
|  | 71 | +        subject, body, from_email, recipient_list, connection=connection | 
|  | 72 | +    ) | 
|  | 73 | +    if html_message: | 
|  | 74 | +        mail.attach_alternative(html_message, "text/html") | 
|  | 75 | + | 
|  | 76 | +    return await mail.send() | 
|  | 77 | + | 
|  | 78 | + | 
|  | 79 | +async def send_mass_mail( | 
|  | 80 | +    datatuple, fail_silently=False, connection=None, provider: str = settings.DEFAULT_EMAIL_PROVIDER_ALIAS, | 
|  | 81 | +): | 
|  | 82 | +    """ | 
|  | 83 | +    Given a datatuple of (subject, message, from_email, recipient_list), send | 
|  | 84 | +    each message to each recipient list. Return the number of emails sent. | 
|  | 85 | +
 | 
|  | 86 | +    If from_email is None, use the DEFAULT_FROM_EMAIL setting. | 
|  | 87 | +    If auth_user and auth_password are set, use them to log in. | 
|  | 88 | +    If auth_user is None, use the EMAIL_HOST_USER setting. | 
|  | 89 | +    If auth_password is None, use the EMAIL_HOST_PASSWORD setting. | 
|  | 90 | +
 | 
|  | 91 | +    Note: The API for this method is frozen. New code wanting to extend the | 
|  | 92 | +    functionality should use the EmailMessage class directly. | 
|  | 93 | +    """ | 
|  | 94 | +    connection = connection or get_connection(fail_silently=fail_silently, provider=provider) | 
|  | 95 | +    messages = [ | 
|  | 96 | +        EmailMessage(subject, body, sender, recipient, connection=connection, prodiver=provider) | 
|  | 97 | +        for subject, body, sender, recipient in datatuple | 
|  | 98 | +    ] | 
|  | 99 | +    return await connection.send_messages(messages) | 
0 commit comments