resources/send_message_and_files/email_sender.py

This module is used to send Email messages. It knows how to check the validity of email accounts, as well as it can split attachments into several messages, according to the established limit on the maximum size of attachments. The main working class is EmailSender.

Class arguments

Class attributes

  • max_attachments_bytes (int): Maximum size of attachments  in bytes,  25 * 1024 * 1024 by default.
  • default_subject (str): Standard message subject. {server_name} -> {script_name} by default.

Class methods

  • email_is_valid(email) → bool - Returns True if correct email is specified.
    Method arguments:
    • email (str): Email address, which is required to check.
  • parse_mails(emails) → List[str]Parses email addresses from a string or list / tuple while checking each address with the email_is_valid method. 
    Method arguments:
    • emails (str | List[str] | Tuple[str])A string, with comma-separated email addresses or a tuple / list of email addresses.
  • send(mails, text="", attachments=None, subject=None): Sends email message.
    Method arguments:
    • mails (str, Tuple[str], List[str]): List of email addresses of receivers.
    • text (str, optional): Message text. Empty string by default.
    • attachments (List[str], optional)List of full paths to files to send as attachments.
    • subject (str,optional): Message subject, default_subject if not specified.

When initializing an instance of the class, it searches for the selected Email account among the existing ones.
If it does not find one, it will raise an EmailAccountNotFound error.

Пример кода
from email_sender import EmailSender

mail = EmailSender("FakeAccount")
# EmailAccountNotFound: FakeAccount

Code examples

Sending email message
from email_sender import EmailSender

mail = EmailSender("Unnamed Account")

mail.send("example@dssl.ru", text="Hello World!", subject="Test message")
Sending attachments

When sending attachments, most email services limit the size of the attachments. The script allows to set the maximum attachment value (25 MB by default). And when sending multiple files, it will automatically split messages into multiple (taking into account that the total size of attachments will be less than the specified one).

from email_sender import EmailSender

mail = EmailSender("Unnamed Account")

# Set max attachments size = 10 MB
mail.max_attachments_bytes = 10 * 1024 * 1024

mail.send("example@dssl.ru", attachments=[r".../bigImage1.jpg", r".../bigImage2.jpg", r".../bigImage3.jpg"])


  • Нет меток