don t base64encode utf8 messages so that we avoid unwanted attention from some spam filters default mime type to use on attachments if it is not explicitly given and cannot be guessed header names that contain structured address data rfc 5322 forbid multiline headers to prevent header injection encoding encoding or settings defaultcharset val strval val may be lazy if n in val or r in val raise badheadererror header values can t contain newlines got r for header r val name try val encodeascii except unicodeencodeerror if name lower in addressheaders val join sanitizeaddressaddr encoding for addr in getaddressesval else val headerval encoding encode else if name lower subject val headerval encode return name val def sanitizeaddressaddr encoding address none if not isinstanceaddr tuple addr forcestraddr try token rest parser getmailboxaddr except headerparseerror valueerror indexerror raise valueerror invalid address s addr else if rest the entire email address must be parsed raise valueerror invalid address only s could be parsed from s token addr nm token displayname or localpart token localpart domain token domain or else nm address addr if not in address raise valueerrorf invalid address address localpart domain address rsplit 1 addressparts nm localpart domain if n in addressparts or r in addressparts raise valueerrorinvalid address address parts cannot contain newlines avoid utf8 encode if it s possible try nm encodeascii nm headernm encode except unicodeencodeerror nm headernm encoding encode try localpart encodeascii except unicodeencodeerror localpart headerlocalpart encoding encode domain punycodedomain parsedaddress addressusernamelocalpart domaindomain return formataddrnm parsedaddress addrspec class mimemixin def asstringself unixfromfalse linesepn fp stringio g generator generatorfp manglefromfalse g flattenself unixfromunixfrom lineseplinesep return fp getvalue def asbytesself unixfromfalse linesepn fp bytesio g generator bytesgeneratorfp manglefromfalse g flattenself unixfromunixfrom lineseplinesep return fp getvalue class safemimemessagemimemixin mimemessage def setitemself name val messagerfc822 attachments must be ascii name val forbidmultilineheadersname val ascii mimemessage setitemself name val class safemimetextmimemixin mimetext def initself text subtypeplain charsetnone self encoding charset mimetext initself text subtypesubtype charsetcharset def setitemself name val name val forbidmultilineheadersname val self encoding mimetext setitemself name val def setpayloadself payload charsetnone if charset utf8 and not isinstancecharset charset charset haslonglines any lenline encode rfc5322emaillinelengthlimit for line in payload splitlines quotedprintable encoding has the side effect of shortening long lines if any 22561 charset utf8charsetqp if haslonglines else utf8charset mimetext setpayloadself payload charsetcharset class safemimemultipartmimemixin mimemultipart def init self subtypemixed boundarynone subpartsnone encodingnone params self encoding encoding mimemultipart initself subtype boundary subparts params def setitemself name val name val forbidmultilineheadersname val self encoding mimemultipart setitemself name val class emailmessage initialize a single email message which can be sent to multiple recipients email header names are caseinsensitive rfc 2045 so we have to accommodate that when doing comparisons formatdate uses stdlib methods to format the date which use the stdlibos concept of a timezone however django sets the tz environment variable based on the timezone setting which will get picked up by formatdate use cached dnsname for performance return a list of all recipients of the email includes direct addressees as well as cc and bcc entries send the email message if not self recipients don t bother creating the network connection if there s nobody to send to return 0 return self getconnectionfailsilently sendmessagesself def attachself filenamenone contentnone mimetypenone if isinstancefilename mimebase if content is not none or mimetype is not none raise valueerror content and mimetype must not be given when a mimebase instance is provided self attachments appendfilename elif content is none raise valueerrorcontent must be provided else mimetype mimetype or mimetypes guesstypefilename0 or defaultattachmentmimetype basetype subtype mimetype split 1 if basetype text if isinstancecontent bytes try content content decode except unicodedecodeerror if mimetype suggests the file is text but it s actually binary read raises a unicodedecodeerror mimetype defaultattachmentmimetype self attachments appendfilename content mimetype def attachfileself path mimetypenone path pathpath with path openrb as file content file read self attachpath name content mimetype def createmessageself msg return self createattachmentsmsg def createattachmentsself msg if self attachments encoding self encoding or settings defaultcharset bodymsg msg msg safemimemultipartsubtypeself mixedsubtype encodingencoding if self body or bodymsg ismultipart msg attachbodymsg for attachment in self attachments if isinstanceattachment mimebase msg attachattachment else msg attachself createattachmentattachment return msg def createmimeattachmentself content mimetype basetype subtype mimetype split 1 if basetype text encoding self encoding or settings defaultcharset attachment safemimetextcontent subtype encoding elif basetype message and subtype rfc822 bug 18967 per rfc 2046 section 5 2 1 messagerfc822 attachments must not be base64 encoded if isinstancecontent emailmessage convert content into an email message first content content message elif not isinstancecontent message for compatibility with existing code parse the message into an email message object if it is not one already content messagefromstringforcestrcontent attachment safemimemessagecontent subtype else encode nontext attachments with base64 attachment mimebasebasetype subtype attachment setpayloadcontent encoders encodebase64attachment return attachment def createattachmentself filename content mimetypenone attachment self createmimeattachmentcontent mimetype if filename try filename encodeascii except unicodeencodeerror filename utf8 filename attachment addheader contentdisposition attachment filenamefilename return attachment def setlistheaderifnotemptyself msg header values if values try value self extraheadersheader except keyerror value joinstrv for v in values msgheader value class emailmultialternativesemailmessage alternativesubtype alternative def init self subject body fromemailnone tonone bccnone connectionnone attachmentsnone headersnone alternativesnone ccnone replytonone super init subject body fromemail to bcc connection attachments headers cc replyto self alternatives alternatives or def attachalternativeself content mimetype don t base64 encode utf 8 messages so that we avoid unwanted attention from some spam filters python defaults to base64 default mime type to use on attachments if it is not explicitly given and cannot be guessed header names that contain structured address data rfc 5322 forbid multi line headers to prevent header injection val may be lazy format a pair of name address or an email address string the entire email address must be parsed avoid utf 8 encode if it s possible return the entire formatted message as a string optional unixfrom when true means include the unix from_ envelope header this overrides the default as_string implementation to not mangle lines that begin with from see bug 13433 for details return the entire formatted message as bytes optional unixfrom when true means include the unix from_ envelope header this overrides the default as_bytes implementation to not mangle lines that begin with from see bug 13433 for details message rfc822 attachments must be ascii quoted printable encoding has the side effect of shortening long lines if any 22561 a container for email information none use settings default initialize a single email message which can be sent to multiple recipients email header names are case insensitive rfc 2045 so we have to accommodate that when doing comparisons formatdate uses stdlib methods to format the date which use the stdlib os concept of a timezone however django sets the tz environment variable based on the time_zone setting which will get picked up by formatdate use cached dns_name for performance from is already handled return a list of all recipients of the email includes direct addressees as well as cc and bcc entries send the email message don t bother creating the network connection if there s nobody to send to attach a file with the given filename and content the filename can be omitted and the mimetype is guessed if not provided if the first parameter is a mimebase subclass insert it directly into the resulting message attachments for a text mimetype guessed or specified when a bytes object is specified as content decode it as utf 8 if that fails set the mimetype to default_attachment_mime_type and don t decode the content if mimetype suggests the file is text but it s actually binary read raises a unicodedecodeerror attach a file from the filesystem set the mimetype to default_attachment_mime_type if it isn t specified and cannot be guessed for a text mimetype guessed or specified decode the file s content as utf 8 if that fails set the mimetype to default_attachment_mime_type and don t decode the content convert the content mimetype pair into a mime attachment object if the mimetype is message rfc822 content may be an email message or emailmessage object as well as a str bug 18967 per rfc 2046 section 5 2 1 message rfc822 attachments must not be base64 encoded convert content into an email message first for compatibility with existing code parse the message into an email message object if it is not one already encode non text attachments with base64 convert the filename content mimetype triple into a mime attachment object set msg s header either from self extra_headers if present or from the values argument a version of emailmessage that makes it easy to send multipart alternative messages for example including text and html versions of the text is made easier initialize a single email message which can be sent to multiple recipients attach an alternative content representation
import mimetypes
from email import charset as Charset
from email import encoders as Encoders
from email import generator, message_from_string
from email.errors import HeaderParseError
from email.header import Header
from email.headerregistry import Address, parser
from email.message import Message
from email.mime.base import MIMEBase
from email.mime.message import MIMEMessage
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.utils import formataddr, formatdate, getaddresses, make_msgid
from io import BytesIO, StringIO
from pathlib import Path

from django.conf import settings
from django.core.mail.utils import DNS_NAME
from django.utils.encoding import force_str, punycode



utf8_charset = Charset.Charset("utf-8")
utf8_charset.body_encoding = None  
utf8_charset_qp = Charset.Charset("utf-8")
utf8_charset_qp.body_encoding = Charset.QP



DEFAULT_ATTACHMENT_MIME_TYPE = "application/octet-stream"

RFC5322_EMAIL_LINE_LENGTH_LIMIT = 998


class BadHeaderError(ValueError):
    pass



ADDRESS_HEADERS = {
    "from",
    "sender",
    "reply-to",
    "to",
    "cc",
    "bcc",
    "resent-from",
    "resent-sender",
    "resent-to",
    "resent-cc",
    "resent-bcc",
}


def forbid_multi_line_headers(name, val, encoding):
    
    encoding = encoding or settings.DEFAULT_CHARSET
    val = str(val)  
    if "\n" in val or "\r" in val:
        raise BadHeaderError(
            "Header values can't contain newlines (got %r for header %r)" % (val, name)
        )
    try:
        val.encode("ascii")
    except UnicodeEncodeError:
        if name.lower() in ADDRESS_HEADERS:
            val = ", ".join(
                sanitize_address(addr, encoding) for addr in getaddresses((val,))
            )
        else:
            val = Header(val, encoding).encode()
    else:
        if name.lower() == "subject":
            val = Header(val).encode()
    return name, val


def sanitize_address(addr, encoding):
    
    address = None
    if not isinstance(addr, tuple):
        addr = force_str(addr)
        try:
            token, rest = parser.get_mailbox(addr)
        except (HeaderParseError, ValueError, IndexError):
            raise ValueError('Invalid address "%s"' % addr)
        else:
            if rest:
                
                raise ValueError(
                    'Invalid address; only %s could be parsed from "%s"' % (token, addr)
                )
            nm = token.display_name or ""
            localpart = token.local_part
            domain = token.domain or ""
    else:
        nm, address = addr
        if "@" not in address:
            raise ValueError(f'Invalid address "{address}"')
        localpart, domain = address.rsplit("@", 1)

    address_parts = nm + localpart + domain
    if "\n" in address_parts or "\r" in address_parts:
        raise ValueError("Invalid address; address parts cannot contain newlines.")

    
    try:
        nm.encode("ascii")
        nm = Header(nm).encode()
    except UnicodeEncodeError:
        nm = Header(nm, encoding).encode()
    try:
        localpart.encode("ascii")
    except UnicodeEncodeError:
        localpart = Header(localpart, encoding).encode()
    domain = punycode(domain)

    parsed_address = Address(username=localpart, domain=domain)
    return formataddr((nm, parsed_address.addr_spec))


class MIMEMixin:
    def as_string(self, unixfrom=False, linesep="\n"):
        
        fp = StringIO()
        g = generator.Generator(fp, mangle_from_=False)
        g.flatten(self, unixfrom=unixfrom, linesep=linesep)
        return fp.getvalue()

    def as_bytes(self, unixfrom=False, linesep="\n"):
        
        fp = BytesIO()
        g = generator.BytesGenerator(fp, mangle_from_=False)
        g.flatten(self, unixfrom=unixfrom, linesep=linesep)
        return fp.getvalue()


class SafeMIMEMessage(MIMEMixin, MIMEMessage):
    def __setitem__(self, name, val):
        
        name, val = forbid_multi_line_headers(name, val, "ascii")
        MIMEMessage.__setitem__(self, name, val)


class SafeMIMEText(MIMEMixin, MIMEText):
    def __init__(self, _text, _subtype="plain", _charset=None):
        self.encoding = _charset
        MIMEText.__init__(self, _text, _subtype=_subtype, _charset=_charset)

    def __setitem__(self, name, val):
        name, val = forbid_multi_line_headers(name, val, self.encoding)
        MIMEText.__setitem__(self, name, val)

    def set_payload(self, payload, charset=None):
        if charset == "utf-8" and not isinstance(charset, Charset.Charset):
            has_long_lines = any(
                len(line.encode()) > RFC5322_EMAIL_LINE_LENGTH_LIMIT
                for line in payload.splitlines()
            )
            
            
            charset = utf8_charset_qp if has_long_lines else utf8_charset
        MIMEText.set_payload(self, payload, charset=charset)


class SafeMIMEMultipart(MIMEMixin, MIMEMultipart):
    def __init__(
        self, _subtype="mixed", boundary=None, _subparts=None, encoding=None, **_params
    ):
        self.encoding = encoding
        MIMEMultipart.__init__(self, _subtype, boundary, _subparts, **_params)

    def __setitem__(self, name, val):
        name, val = forbid_multi_line_headers(name, val, self.encoding)
        MIMEMultipart.__setitem__(self, name, val)


class EmailMessage:
    

    content_subtype = "plain"
    mixed_subtype = "mixed"
    encoding = None  

    def __init__(
        self,
        subject="",
        body="",
        from_email=None,
        to=None,
        bcc=None,
        connection=None,
        attachments=None,
        headers=None,
        cc=None,
        reply_to=None,
    ):
        
        if to:
            if isinstance(to, str):
                raise TypeError('"to" argument must be a list or tuple')
            self.to = list(to)
        else:
            self.to = []
        if cc:
            if isinstance(cc, str):
                raise TypeError('"cc" argument must be a list or tuple')
            self.cc = list(cc)
        else:
            self.cc = []
        if bcc:
            if isinstance(bcc, str):
                raise TypeError('"bcc" argument must be a list or tuple')
            self.bcc = list(bcc)
        else:
            self.bcc = []
        if reply_to:
            if isinstance(reply_to, str):
                raise TypeError('"reply_to" argument must be a list or tuple')
            self.reply_to = list(reply_to)
        else:
            self.reply_to = []
        self.from_email = from_email or settings.DEFAULT_FROM_EMAIL
        self.subject = subject
        self.body = body or ""
        self.attachments = []
        if attachments:
            for attachment in attachments:
                if isinstance(attachment, MIMEBase):
                    self.attach(attachment)
                else:
                    self.attach(*attachment)
        self.extra_headers = headers or {}
        self.connection = connection

    def get_connection(self, fail_silently=False):
        from django.core.mail import get_connection

        if not self.connection:
            self.connection = get_connection(fail_silently=fail_silently)
        return self.connection

    def message(self):
        encoding = self.encoding or settings.DEFAULT_CHARSET
        msg = SafeMIMEText(self.body, self.content_subtype, encoding)
        msg = self._create_message(msg)
        msg["Subject"] = self.subject
        msg["From"] = self.extra_headers.get("From", self.from_email)
        self._set_list_header_if_not_empty(msg, "To", self.to)
        self._set_list_header_if_not_empty(msg, "Cc", self.cc)
        self._set_list_header_if_not_empty(msg, "Reply-To", self.reply_to)

        
        
        header_names = [key.lower() for key in self.extra_headers]
        if "date" not in header_names:
            
            
            
            
            msg["Date"] = formatdate(localtime=settings.EMAIL_USE_LOCALTIME)
        if "message-id" not in header_names:
            
            msg["Message-ID"] = make_msgid(domain=DNS_NAME)
        for name, value in self.extra_headers.items():
            if name.lower() != "from":  
                msg[name] = value
        return msg

    def recipients(self):
        
        return [email for email in (self.to + self.cc + self.bcc) if email]

    def send(self, fail_silently=False):
        
        if not self.recipients():
            
            
            return 0
        return self.get_connection(fail_silently).send_messages([self])

    def attach(self, filename=None, content=None, mimetype=None):
        
        if isinstance(filename, MIMEBase):
            if content is not None or mimetype is not None:
                raise ValueError(
                    "content and mimetype must not be given when a MIMEBase "
                    "instance is provided."
                )
            self.attachments.append(filename)
        elif content is None:
            raise ValueError("content must be provided.")
        else:
            mimetype = (
                mimetype
                or mimetypes.guess_type(filename)[0]
                or DEFAULT_ATTACHMENT_MIME_TYPE
            )
            basetype, subtype = mimetype.split("/", 1)

            if basetype == "text":
                if isinstance(content, bytes):
                    try:
                        content = content.decode()
                    except UnicodeDecodeError:
                        
                        
                        mimetype = DEFAULT_ATTACHMENT_MIME_TYPE

            self.attachments.append((filename, content, mimetype))

    def attach_file(self, path, mimetype=None):
        
        path = Path(path)
        with path.open("rb") as file:
            content = file.read()
            self.attach(path.name, content, mimetype)

    def _create_message(self, msg):
        return self._create_attachments(msg)

    def _create_attachments(self, msg):
        if self.attachments:
            encoding = self.encoding or settings.DEFAULT_CHARSET
            body_msg = msg
            msg = SafeMIMEMultipart(_subtype=self.mixed_subtype, encoding=encoding)
            if self.body or body_msg.is_multipart():
                msg.attach(body_msg)
            for attachment in self.attachments:
                if isinstance(attachment, MIMEBase):
                    msg.attach(attachment)
                else:
                    msg.attach(self._create_attachment(*attachment))
        return msg

    def _create_mime_attachment(self, content, mimetype):
        
        basetype, subtype = mimetype.split("/", 1)
        if basetype == "text":
            encoding = self.encoding or settings.DEFAULT_CHARSET
            attachment = SafeMIMEText(content, subtype, encoding)
        elif basetype == "message" and subtype == "rfc822":
            
            
            if isinstance(content, EmailMessage):
                
                content = content.message()
            elif not isinstance(content, Message):
                
                
                content = message_from_string(force_str(content))

            attachment = SafeMIMEMessage(content, subtype)
        else:
            
            attachment = MIMEBase(basetype, subtype)
            attachment.set_payload(content)
            Encoders.encode_base64(attachment)
        return attachment

    def _create_attachment(self, filename, content, mimetype=None):
        
        attachment = self._create_mime_attachment(content, mimetype)
        if filename:
            try:
                filename.encode("ascii")
            except UnicodeEncodeError:
                filename = ("utf-8", "", filename)
            attachment.add_header(
                "Content-Disposition", "attachment", filename=filename
            )
        return attachment

    def _set_list_header_if_not_empty(self, msg, header, values):
        
        if values:
            try:
                value = self.extra_headers[header]
            except KeyError:
                value = ", ".join(str(v) for v in values)
            msg[header] = value


class EmailMultiAlternatives(EmailMessage):
    

    alternative_subtype = "alternative"

    def __init__(
        self,
        subject="",
        body="",
        from_email=None,
        to=None,
        bcc=None,
        connection=None,
        attachments=None,
        headers=None,
        alternatives=None,
        cc=None,
        reply_to=None,
    ):
        
        super().__init__(
            subject,
            body,
            from_email,
            to,
            bcc,
            connection,
            attachments,
            headers,
            cc,
            reply_to,
        )
        self.alternatives = alternatives or []

    def attach_alternative(self, content, mimetype):
        
        if content is None or mimetype is None:
            raise ValueError("Both content and mimetype must be provided.")
        self.alternatives.append((content, mimetype))

    def _create_message(self, msg):
        return self._create_attachments(self._create_alternatives(msg))

    def _create_alternatives(self, msg):
        encoding = self.encoding or settings.DEFAULT_CHARSET
        if self.alternatives:
            body_msg = msg
            msg = SafeMIMEMultipart(
                _subtype=self.alternative_subtype, encoding=encoding
            )
            if self.body:
                msg.attach(body_msg)
            for alternative in self.alternatives:
                msg.attach(self._create_mime_attachment(*alternative))
        return msg