MCPcopy
hub / github.com/django/django / attach_file

Method attach_file

django/core/mail/message.py:465–479  ·  view source on GitHub ↗

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 DEF

(self, path, mimetype=None)

Source from the content-addressed store, hash-verified

463 self.attachments.append(EmailAttachment(filename, content, mimetype))
464
465 def attach_file(self, path, mimetype=None):
466 """
467 Attach a file from the filesystem.
468
469 Set the mimetype to DEFAULT_ATTACHMENT_MIME_TYPE if it isn't specified
470 and cannot be guessed.
471
472 For a text/* mimetype (guessed or specified), decode the file's content
473 as UTF-8. If that fails, set the mimetype to
474 DEFAULT_ATTACHMENT_MIME_TYPE and don't decode the content.
475 """
476 path = Path(path)
477 with path.open("rb") as file:
478 content = file.read()
479 self.attach(path.name, content, mimetype)
480
481 def _add_bodies(self, msg):
482 if self.body or not self.attachments:

Callers 1

test_attach_fileMethod · 0.95

Calls 3

attachMethod · 0.95
openMethod · 0.45
readMethod · 0.45

Tested by 1

test_attach_fileMethod · 0.76