Package: xchange-mail
📧 Sending emails with basic formatting using exchangelib 📧Table of contentsAbout xchange_mailPackage StructureInstalling the PackageExamplesContributionSocial MediaAbout xchange_mailThis python package was build for making the mail sending processing through exchangelib a little bit easier. The idea is to create some custom functions for a limited use cases, so the user won't need to configure or define details on Account or Config exchangelib classes, but rather execute basic functions for sending basic emails.Theexamplessection will clarify some use cases ofxchange_mailpackage for helping users to send basic emails through MS Exchange. Keep watching this documentation.Package StructureAt this time, the package is built around just one module calledmail. This module contains some functions for helping users connecting with Exchange server and also sending basic mails with plain text or HTML body messages. The table below has the explanation of the main componentes of thismailmodule.FunctionShort Descriptionconnect_exchange()Receives some user credentials for connecting to Exchange and returning an Account objectattach_file()Stores a pandas DataFrame object on buffers and returns a two-elements list containing the attach name and the attach objectformat_mail_body()Creates a HTMLBody object. If a DataFrame is passed as an argument, it usespretty_html_tablepackage for customizing a table before creating the HTMLBodysend_simple_mail()Sends a simple mail through exchange with possibilities for attaching one file, sending a DataFrame object on mail body, sending an image on mail body or attached or using html code for customizing mailsend_mail_mult_files()Can send multiple files attached or multiple DataFrames on bodyBiblioteca python construída para facilitar o gerenciamento e envio de e-mails utilizando a bibliotecaexchangelibcomo ORM da caixa de e-mails Exchange.Installing the PackageThe latest version ofxchange_mailpackage are published and available onPyPI repository:pushpin:Note:as a good practice for every Python project, the creation of avirtual environmentis needed to get a full control of dependencies and third part packages on your code. By this way, the code below can be used for creating a new venv on your OS.# Creating and activating venv on Linux$python-mvenv<path_venv>/<name_venv>
$source<path_venv>/<nome_venv>/bin/activate# Creating and activating venv on Windows$python-mvenv<path_venv>/<name_venv>
$<path_venv>/<nome_venv>/Scripts/activateWith the new venv active, all you need is execute the code below using pip for installing the package (upgrading pip is optional):$pipinstall--upgradepip
$pipinstallxchange_mailThe xchange_mail package is built in an upper layer above some other python packages like exchangelib and pandas. So, when installing mlcomposer, the pip utility will also install all dependencies linked to the package.ExamplesAfter introducing the package, it's time to explain it in a deeper way: through examples. On this Github repository, it's possible to find some good uses of xchange_mail onexamples/folder. In practice, for sending a basic email it's possible to execute thesend_simple_mail()function with few parameter configuration as seen below:fromxchange_mail.mailimportsend_simple_mail# Extracting environment variables from a .env file (optional)USERNAME=os.getenv('MAIL_FROM')PWD=os.getenv('PASSWORD')SERVER='outlook.office365.com'MAIL_BOX=os.getenv('MAIL_BOX')MAIL_TO=os.getenv('MAIL_TO')# Sending a basic mailsend_simple_mail(username=USERNAME,password=PWD,server=SERVER,mail_box=MAIL_BOX,subject='This is a xchange_mail test',mail_body='Testing the package by sending a simple mail',mail_signature='Regards, xchange_mail developers',mail_to=MAIL_TO)Done! Almost all other package features are built around thissend_simple_mail()function and the other one calledsend_mail_mult_files(). Just to clarify, there are some parameters that can be set on the function above for sending a pandas DataFrame attached on mail body, for example. There is also a feature for sending an image embedding on mail body. The code below is an example of sending a simple mail with a DataFrame object attached, on mail body with an image saved locally.importpandasaspdfromxchange_mail.mailimportsend_simple_mail# Extracting environment variables from a .env file (optional)USERNAME=os.getenv('MAIL_FROM')PWD=os.getenv('PASSWORD')SERVER='outlook.office365.com'MAIL_BOX=os.getenv('MAIL_BOX')MAIL_TO=os.getenv('MAIL_TO')# Sending a basic mailsend_simple_mail(username=USERNAME,password=PWD,server=SERVER,mail_box=MAIL_BOX,subject='This is a xchange_mail test',mail_body='Testing the package by sending a simple mail',mail_signature='Regards, xchange_mail developers',mail_to=MAIL_TO,df_on_body=True,df_on_attachment=True,df=df,attachment_filename='pandas_dataframe.csv',image_on_body=True,image_location='/home/user/image_dir/image.png')For new use cases, please take a look atexamples/folder on this repository.ContributionThe xchange_mail python package is an open source implementation and the more people use it, the more happy the developers will be. So if you want to contribute with xchange_mail, please feel free to follow the best practices for implementing coding on this github repository through creating new branches, making merge requests and pointig out whenever you think there is a new topic to explore or a bug to be fixed.Thank you very much for reaching this and it will be a pleasure to have you as xchange_mail user or developer.Social MediaFollow me on LinkedIn:https://www.linkedin.com/in/thiago-panini/See my other Python packages:https://github.com/ThiagoPanini