core.models.Email module

Module with the Email model class.

core.models.Email.logger = <Logger core.models.Email (INFO)>

The logger instance for this module.

class core.models.Email.Email(*args, **kwargs)[source]

Bases: ExportModelOperationsMixin('email'), DownloadMixin, ThumbnailMixin, URLMixin, FavoriteModelMixin, FilePathModelMixin, TimestampModelMixin, Model

Database model for an email.

BASENAME = 'email'
DELETE_NOTICE = 'This will delete the records of this email and all its attachments but not its correspondents.'
DELETE_NOTICE_PLURAL = 'This will delete the records of these emails and all their attachments but not their correspondents.'
message_id

The messageID header of the mail. Unique together with mailbox.

datetime

The Date header of the mail.

subject

The subject header of the mail.

plain_bodytext

The plain bodytext of the mail. Can be blank.

html_bodytext

The html bodytext of the mail. Can be blank.

in_reply_to: models.ManyToManyField[Email, Email]

The mails that this mail is a response to. Technically just a single mail, but as a mail can exist in multiple mailboxes, this needs to be able to reference multiples.

references: models.ManyToManyField[Email, Email]

The mails that this email references.

datasize

The bytes size of the mail.

correspondents: models.ManyToManyField[Correspondent, Correspondent]

The correspondents that are mentioned in this mail. Bridges through core.models.EmailCorrespondent.

mailbox: models.ForeignKey[Mailbox]

The mailbox that this mail has been found in. Unique together with message_id. Deletion of that mailbox deletes this mail.

headers

All other header fields of the mail. Can be null.

x_spam_flag

The x_spam header of this mail. Can be null.

__str__()[source]

Returns a string representation of the model data.

Return type:

str

Returns:

The string representation of the email, using message_id, datetime and mailbox.

save(*args, **kwargs)[source]

Extended :django:django.models.Model.save() method.

Saves the data to eml if configured.

Return type:

None

Parameters:
_get_storage_file_name()[source]

Create the filename for the stored eml.

Return type:

str

fill_from_email_bytes(email_bytes)[source]

Fills the core.models.Email with data from an email in bytes form.

Parameters:

email_bytes (bytes) – The email bytes data.

Return type:

Email

Returns:

The core.models.Email instance with data from the bytes.

exception DoesNotExist

Bases: ObjectDoesNotExist

exception MultipleObjectsReturned

Bases: MultipleObjectsReturned

_meta = <Options for Email>
add_correspondents()[source]

Adds the correspondents from the headerfields to the model.

Return type:

None

attachments

Accessor to the related objects manager on the reverse side of a many-to-one relation.

In the example:

class Child(Model):
    parent = ForeignKey(Parent, related_name='children')

Parent.children is a ReverseManyToOneDescriptor instance.

Most of the implementation is delegated to a dynamically defined manager class built by create_forward_many_to_many_manager() defined below.

created

The datetime the model instance was created. Is set automatically.

emailcorrespondents

Accessor to the related objects manager on the reverse side of a many-to-one relation.

In the example:

class Child(Model):
    parent = ForeignKey(Parent, related_name='children')

Parent.children is a ReverseManyToOneDescriptor instance.

Most of the implementation is delegated to a dynamically defined manager class built by create_forward_many_to_many_manager() defined below.

file_path

The relative path in the storage where the file is stored. Can be null if no file has been saved (null does not collide with the unique constraint.).

get_next_by_created(*, field=<django.db.models.fields.DateTimeField: created>, is_next=True, **kwargs)
get_next_by_datetime(*, field=<django.db.models.fields.DateTimeField: datetime>, is_next=True, **kwargs)
get_next_by_updated(*, field=<django.db.models.fields.DateTimeField: updated>, is_next=True, **kwargs)
get_previous_by_created(*, field=<django.db.models.fields.DateTimeField: created>, is_next=False, **kwargs)
get_previous_by_datetime(*, field=<django.db.models.fields.DateTimeField: datetime>, is_next=False, **kwargs)
get_previous_by_updated(*, field=<django.db.models.fields.DateTimeField: updated>, is_next=False, **kwargs)
id

A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.

is_favorite

Flags favorite accounts. False by default.

mailbox_id
objects = <django.db.models.manager.Manager object>
referenced_by

Accessor to the related objects manager on the forward and reverse sides of a many-to-many relation.

In the example:

class Pizza(Model):
    toppings = ManyToManyField(Topping, related_name='pizzas')

Pizza.toppings and Topping.pizzas are ManyToManyDescriptor instances.

Most of the implementation is delegated to a dynamically defined manager class built by create_forward_many_to_many_manager() defined below.

replies

Accessor to the related objects manager on the forward and reverse sides of a many-to-many relation.

In the example:

class Pizza(Model):
    toppings = ManyToManyField(Topping, related_name='pizzas')

Pizza.toppings and Topping.pizzas are ManyToManyDescriptor instances.

Most of the implementation is delegated to a dynamically defined manager class built by create_forward_many_to_many_manager() defined below.

updated

The datetime the model instance entry was last updated. Is set automatically.

add_in_reply_to()[source]

Adds the in-reply-to emails from the headerfields to the model.

Return type:

None

add_references()[source]

Adds the references from the headerfields to the model.

Return type:

None

reprocess()[source]

Reprocesses the mails connections to other emails in the database.

Return type:

None

restore_to_mailbox()[source]

Restores the email to its mailbox.

Raises:
Return type:

None

property conversation: QuerySet[source]

Recursively gets all emails that are part of this emails conversation, connected through references or in_reply_to.

Returns:

Queryset of all mails in the conversation.

property has_thumbnail: bool

Checks whether a thumbnail download is possible for the instance.

property can_be_restored: bool

Checks if the email can be restored to its mailbox.

Returns:

Whether the email can be restored.

property html_version: str[source]

Renders a html version of this email.

Uses the template and css from constance settings.

Returns:

The emails html version.

property is_spam: bool

Checks the spam headers to decide whether the mail is spam.

Returns:

Whether the mail is considered spam.

classmethod create_from_email_bytes(email_bytes, mailbox)[source]

Creates an core.models.Email from an email in bytes form.

Parameters:
  • email_bytes (bytes) – The email bytes to parse the emaildata from.

  • mailbox (Mailbox) – The mailbox the email is in.

Return type:

Email | None

Returns:

The core.models.Email instance with data from the bytes. None if there is no Message-ID header in email_message, if the mail already exists in the db or if the mail is spam and is supposed to be thrown out.

static _queryset_as_zip_eml(queryset)[source]

Parses a queryset of emails into a zip of eml files.

Note

Does not validate args! This has to be done beforehand.

Return type:

_TemporaryFileWrapper

Parameters:

queryset (QuerySet)

static _queryset_as_mailbox_file(queryset, file_format)[source]

Parses a queryset of emails into a mailbox file.

Note

Does not validate args! This has to be done beforehand.

Return type:

_TemporaryFileWrapper

Parameters:
static _queryset_as_mailbox_zip(queryset, file_format)[source]

Parses a queryset of emails into a zipped mailbox dir.

Note

Does not validate args! This has to be done beforehand.

Return type:

_TemporaryFileWrapper

Parameters:
static queryset_as_file(queryset, file_format)[source]

Processes the files of the emails in the queryset into a temporary file.

Parameters:
Return type:

_TemporaryFileWrapper

Returns:

The temporary file wrapper.

Raises: