Package: mailtrap
Official Mailtrap Python clientThis Python package offers integration with theofficial APIforMailtrap.Quickly add email sending functionality to your Python application with Mailtrap.Compatibility with previous releasesVersions of this package up to 1.0.1 were a different, unrelated project, that is now maintained asSendria. To continue using it, seeinstructions.InstallationPrerequisitesPython version 3.6+Install packagepip install mailtrapUsageMinimalimportmailtrapasmt# create mail objectmail=mt.Mail(sender=mt.Address(email="mailtrap@example.com",name="Mailtrap Test"),to=[mt.Address(email="your@email.com")],subject="You are awesome!",text="Congrats for sending test email with Mailtrap!",)# create client and sendclient=mt.MailtrapClient(token="your-api-key")client.send(mail)Fullimportbase64frompathlibimportPathimportmailtrapasmtwelcome_image=Path(__file__).parent.joinpath("welcome.png").read_bytes()mail=mt.Mail(sender=mt.Address(email="mailtrap@example.com",name="Mailtrap Test"),to=[mt.Address(email="your@email.com",name="Your name")],cc=[mt.Address(email="cc@email.com",name="Copy to")],bcc=[mt.Address(email="bcc@email.com",name="Hidden Recipient")],subject="You are awesome!",text="Congrats for sending test email with Mailtrap!",html="""<!doctype html><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"></head><body style="font-family: sans-serif;"><div style="display: block; margin: auto; max-width: 600px;" class="main"><h1 style="font-size: 18px; font-weight: bold; margin-top: 20px">Congrats for sending test email with Mailtrap!</h1><p>Inspect it using the tabs you see above and learn how this email can be improved.</p><img alt="Inspect with Tabs" src="cid:welcome.png" style="width: 100%;"><p>Now send your email using our fake SMTP server and integration of your choice!</p><p>Good luck! Hope it works.</p></div><!-- Example of invalid for email html/css, will be detected by Mailtrap: --><style>.main { background-color: white; }a:hover { border-left-width: 1em; min-height: 2em; }</style></body></html>""",category="Test",attachments=[mt.Attachment(content=base64.b64encode(welcome_image),filename="welcome.png",disposition=mt.Disposition.INLINE,mimetype="image/png",content_id="welcome.png",)],headers={"X-MT-Header":"Custom header"},custom_variables={"year":2023},)client=mt.MailtrapClient(token="your-api-key")client.send(mail)Using email templateimportmailtrapasmt# create mail objectmail=mt.MailFromTemplate(sender=mt.Address(email="mailtrap@example.com",name="Mailtrap Test"),to=[mt.Address(email="your@email.com")],template_uuid="2f45b0aa-bbed-432f-95e4-e145e1965ba2",template_variables={"user_name":"John Doe"},)# create client and sendclient=mt.MailtrapClient(token="your-api-key")client.send(mail)ContributingBug reports and pull requests are welcome onGitHub. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to thecode of conduct.Development EnvironmentClone the repohttps://github.com/railsware/mailtrap-python.gitcdmailtrap-pythonInstalltoxtoxis an environment orchestrator. We use it to setup local environments, run tests and execute linters.python-mpipinstall--usertox
python-mtox--helpTo setup virtual environments, run tests and linters use:toxIt will create virtual environments with all installed dependencies for each available python interpreter (starting frompython3.6) on your machine.
By default, they will be available in{project}/.tox/directory. So, for instance, to activatepython3.11environment, run the following:source.tox/py311/bin/activateInformation for version 1 usersIf you are a version 1 user, it is advised that you upgrade toSendria, which is the same package, but under a new name, and withnew features. However, you can also continue using the last v1 release by locking the version in pip:# To use the FORMER version of the mailtrap package, now known as Sendria:pipinstall--force-reinstall-v"mailtrap==1.0.1"LicenseThe project is available as open source under the terms of theMIT License.Code of ConductEveryone interacting in the Mailtrap project's codebases, issue trackers, chat rooms and mailing lists is expected to follow thecode of conduct