MCPcopy
hub / github.com/django/django / attach

Method attach

django/core/mail/message.py:413–463  ·  view source on GitHub ↗

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 (gue

(self, filename=None, content=None, mimetype=None)

Source from the content-addressed store, hash-verified

411 return connection.send_messages([self])
412
413 def attach(self, filename=None, content=None, mimetype=None):
414 """
415 Attach a file with the given filename and content. The filename can
416 be omitted and the mimetype is guessed, if not provided.
417
418 If the first parameter is a MIMEBase subclass, insert it directly
419 into the resulting message attachments.
420
421 For a text/* mimetype (guessed or specified), when a bytes object is
422 specified as content, decode it as UTF-8. If that fails, set the
423 mimetype to DEFAULT_ATTACHMENT_MIME_TYPE and don't decode the content.
424 """
425 if isinstance(filename, email.message.MIMEPart):
426 if content is not None or mimetype is not None:
427 raise ValueError(
428 "content and mimetype must not be given when a MIMEPart "
429 "instance is provided."
430 )
431 self.attachments.append(filename)
432 elif isinstance(filename, MIMEBase):
433 warnings.warn(
434 "MIMEBase attachments are deprecated."
435 " Use an email.message.MIMEPart instead.",
436 RemovedInDjango70Warning,
437 )
438 if content is not None or mimetype is not None:
439 raise ValueError(
440 "content and mimetype must not be given when a MIMEBase "
441 "instance is provided."
442 )
443 self.attachments.append(filename)
444 elif content is None:
445 raise ValueError("content must be provided.")
446 else:
447 mimetype = (
448 mimetype
449 or mimetypes.guess_type(filename)[0]
450 or DEFAULT_ATTACHMENT_MIME_TYPE
451 )
452 basetype, subtype = mimetype.split("/", 1)
453
454 if basetype == "text":
455 if isinstance(content, bytes):
456 try:
457 content = content.decode()
458 except UnicodeDecodeError:
459 # If mimetype suggests the file is text but it's
460 # actually binary, read() raises a UnicodeDecodeError.
461 mimetype = DEFAULT_ATTACHMENT_MIME_TYPE
462
463 self.attachments.append(EmailAttachment(filename, content, mimetype))
464
465 def attach_file(self, path, mimetype=None):
466 """

Calls 3

appendMethod · 0.45
splitMethod · 0.45
decodeMethod · 0.45