Package: scrippy-mail
scrippy_mailSMTP, POP3 and IMAP clients for theScrippyframework.PrerequisitesPython ModulesRequired Modules listNoneInstallationWithpippipinstallscrippy-mailManualgitclonehttps://codeberg.org/scrippy/scrippy-mail.gitcdscrippy-mail
python-mpipinstall-rrequirements.txt
makeinstallscrippy_mailThescrippy_mailmodule offers the following clients objects for sending and retrieving emails:scrippy_mail.smtp.clientfor sending email usingSMTPprotocolscrippy_mail.imap.clientfor email retrieval usingIMAPprotocolscrippy_mail.pop.clientfor email retrieval usingPOP3protocolEach client are able to connect to remote server using secure connection withSSLorSTARTTLS.Thescrippy_mail.mailmodule offers a simplified interface for sending emails via theMailerobject.Each client use the same following parameter set:host: The name of the mail server to use (default: localhost)port: The port number to contact the mail server (default: 25)username: The username to authenticate on the mail serverpassword: The password to authenticate on the mail serverssl: A boolean indicating whether to use SSL (default: False)starttls: A boolean indicating whether to use STARTTLS (default: False)ssl_verify: A boolean indicating whether to verify remote serverSSLcertificate (default:True)timeout: An integer indicating the maximum delay in seconds to succeed in a connection (default: 60)exit_on_error: A boolean. If set toFalse, any error encountered will be logged as a warning. When set toTrue, exit the workflow if any error is encountered (Default:True)NOTE:sslandstarttlsare mutualy exclusive. Using both at the same time raises an error.In doubt usesslfor maximum secure connection.Sending MailNOTE:Recipients, Cc or BCc addresses must be provided as lists.Each email address must be an email address that complies withRFC 5322.Attachments must be a list of file pathfromscrippy_mailimportScrippyMailError,loggerfromscrippy_mail.smtpimportclientasSmtpClientmail_username="luiggi.vercotti@flying.circus"mail_password="D3ADP4ARR0T"mail_host="smtp.flying.circus"mail_port="465"mail_ssl=Truemail_from="luigi.vercotti@flying.circus"mail_to="harry.fink@flying.circus"mail_subject="Error report"mail_body="""Dear Harry Fink,You receive this email because you are one of the functional administrators of the Dead Parrot application.The script execution ended with the following error:- It's not pinin’! It's passed on! This parrot is no more!--Kind regards,Luiggi Vercotti"""withSmtpClient(host=mail_host,port=mail_port,username=mail_user,password=mail_password,ssl=mail_ssl)assmtp_client:recipients=[mail_to]try:smtp_client.send(subject=mail_subject,body=mail_body,sender=mail_from,recipients=recipients,cc=None,bcc=None,attachments=None,exit_on_error=True)logger.info(f"Mail successfully sent to{mail_to}")exceptScrippyMailErroraserr:logger.error(err)raiseerrIMAPoperationsTheIMAPclient allows all operations expected from anIMAPclient such as but not limited to:Get unread messagesMark message as read/unreadDelete messagesSave a message in a local filesystemfromscrippy_mailimportScrippyMailError,loggerfromscrippy_mail.imapimportclientasImapClientmail_user="luiggi.vercotti@flying.circus"mail_password="D3ADP4ARR0T"mail_host="imap.flying.circus"mail_port="993"mail_tls=Truemail_box="INBOX"mail_file="/tmp/message.txt"withImapClient(host=mail_host,port=mail_port,username=mail_user,password=mail_password,starttls=mail_tls)asimap_client:try:fornumberinimap_client.get_unread_messages_numbers(mailbox=mail_box):message=imap_client.get_message(number=number,mailbox=mail_box)# log message:logger.info(message)# Mark message as readimap_client.mark_message_as_read(number=number,mailbox=mail_box)# Mark message as unreadimap_client.mark_message_as_unread(number=number,mailbox=mail_box)# Save message (attachments will be saved in a directory netx to the message)imap_client.save_message(number=number,filename=mail_file,mailbox=mail_box)# Delete messageimap_client.delete_message(number=number,mailbox=mail_box)exceptScrippyMailErroraserr:logger.error(err)raiseerrPOP3 operationsThePOP3client allows all operations expected from anPOP3client such as but not limited to:Get messagesDelete messagesSave a message in a local filesystemNOTE:ThePOP3protocol does not manage message states asreadorunread. This feature is only avalaible withIMAP.fromscrippy_mailimportScrippyMailError,loggerfromscrippy_mail.popimportclientasPopClientmail_user="luiggi.vercotti@flying.circus"mail_password="D3ADP4ARR0T"mail_host="imap.flying.circus"mail_port="995"mail_ssl=Truemail_box="INBOX"mail_file="/tmp/message.txt"withPopClient(host=mail_host,port=mail_port,username=mail_user,password=mail_password,ssl=mail_ssl)aspop_client:try:# Get message countcount=pop_client.get_message_count()logger.info(f"{count}messages are available in this mailbox.")# Get all available messages and work on each of themfornumberinpop_client.get_messages_numbers():message=pop_client.get_message(number=number)# log message:logger.info(message)# Save message (attachments will be saved in a directory netx to the message)pop_client.save_message(number=number,filename=mail_file)# Delete messagepop_client.delete_message(number=number)exceptScrippyMailErroraserr:logger.error(err)raiseerrConvenient wrapper methods:PopClient.get_all_messages(): Returns all mesasges in a listPopClient.delete_all_messages(): Deletes all messages