Package: mailslurp-client
MailSlurp Python ClientCreate real email addresses on demand. Send and receive emails and attachments from code and tests using Python.MailSlurp is an email API service that lets you create real email addresses in code. You can then send and receive emails and attachments in Python applications and tests.Video tutorialQuick linksAPI documentationMethod DocumentationPyPI PackageGithub SourceSend email using SMTP in PythonSMTP access detailsGet startedThis section describes how to get up and running with the Python client.See theexamples pagefor more examples and use with common frameworks such as Django, Flask and Pytest.See the method documentation for alist of all functionsor jump to common controllers below:InboxControllerEmailControllerSMSControllerWaitForControllerCreate API KeyFirst you'll need an API Key.Create a free accountand copy the key from your dashboard.Install packageMailSlurp has an official PyPI package calledmailslurp-client. It supports Python version 2 and 3.pipinstallmailslurp-clientOn some systems you may need to installdistutils. If you encounter aCERTIFICATE_VERIFY_VALIDerror you can installcertifiandcertifi-win32for Windows:pipinstallpython-certifi-win32ConfigureOnce installed you can importmailslurp_clientand create aconfigurationwith yourAPI Key.importmailslurp_clientconfiguration=mailslurp_client.Configuration()configuration.api_key["x-api-key"]=YOUR_API_KEYThen you can create API controller instances using the configuration:withmailslurp_client.ApiClient(configuration)asapi_client:api_instance=mailslurp_client.InboxControllerApi(api_client)See thecontrollers overviewfor all API methods.Email usage examplesMailSlurp can be used to create email addresses than can send and receive real emails, SMS, and attachments in Python.Create an email addressCreate an inbox using the inbox controller:inbox_controller=mailslurp_client.InboxControllerApi(api_client)inbox=inbox_controller.create_inbox_with_defaults()self.assertTrue("@mailslurp"ininbox.email_address)You can pass options using theCreateInboxOptionsclass:options=mailslurp_client.CreateInboxDto()options.name="Test inbox"options.inbox_type="SMTP_INBOX"inbox=inbox_controller.create_inbox_with_options(options)self.assertTrue("@mailslurp"ininbox.email_address)See theinbox controllerfor more methods.Access inbox using SMTPsmtp_access=inbox_controller.get_imap_smtp_access(inbox_id=inbox.id)self.assertIsNotNone(smtp_access.secure_smtp_server_host)Send with SMTP client# configure smtp client using access detailsfromsmtplibimportSMTPwithSMTP(host=smtp_access.secure_smtp_server_host,port=smtp_access.secure_smtp_server_port,)assmtp:msg="Subject: Test subjectThisisthebody"smtp.login(user=smtp_access.secure_smtp_username,password=smtp_access.secure_smtp_password,)smtp.sendmail(from_addr=inbox.email_address,to_addrs=[inbox.email_address],msg=msg,)smtp.quit()List inboxesList inboxes using theinbox controller:inboxes=inbox_controller.get_all_inboxes(page=0)# pagination propertiesself.assertTrue(inboxes.total_pages>0)self.assertTrue(inboxes.total_elements>0)# view contentsself.assertIsNotNone(inboxes.content[0].email_address)Get an inboxinbox=inbox_controller.get_inbox(inbox_id=inbox.id)self.assertTrue("@mailslurp"ininbox.email_address)# get by email addressinbox_by_email=inbox_controller.get_inbox_by_email_address(inbox.email_address)self.assertTrue(inbox_by_email.exists)# get by nameinbox_by_name=inbox_controller.get_inbox_by_name(inbox.name)self.assertTrue(inbox_by_name.exists)Delete an inboxinbox_controller.delete_inbox(inbox_id=inbox.id)Upload attachmentsTo send attachments first upload the attachments as base64 encoded strings and use the returned attachment IDs when sending.importbase64attachment_controller=mailslurp_client.AttachmentControllerApi(api_client)options=mailslurp_client.UploadAttachmentOptions(filename="test.txt",content_type="text/plain",base64_contents=base64.b64encode("Hello world".encode("utf-8")).decode("utf-8"),)attachment_ids=attachment_controller.upload_attachment(options)self.assertTrue(len(attachment_ids)==1)Send emailsSend emails with theinbox controller:send_options=mailslurp_client.SendEmailOptions(to=[recipient.email_address],subject="Hello",body="Here is your email body",attachments=attachment_ids,)sent=inbox_controller.send_email_and_confirm(inbox_id=inbox.id,send_email_options=send_options)self.assertTrue(sent.sent_atisnotNone)Receive emails and extract contentUse thewait for controllerto wait for an expected email count to be satisfied and then return those emails.wait_for_controller=mailslurp_client.WaitForControllerApi(api_client)email=wait_for_controller.wait_for_latest_email(inbox_id=inbox.id,timeout=60_000,unread_only=True)self.assertTrue("Hello"inemail.subject)Email content matchingmatching_emails=wait_for_controller.wait_for_matching_emails(inbox_id=inbox.id,timeout=60_000,unread_only=False,match_options=mailslurp_client.MatchOptions(conditions=[mailslurp_client.ConditionOption(condition="HAS_ATTACHMENTS",value="TRUE")],matches=[mailslurp_client.MatchOption(field="SUBJECT",should="CONTAIN",value="Hello")],),count=1,)self.assertTrue(len(matching_emails)>0)Download attachmentsattachment_content=attachment_controller.download_attachment_as_base64_encoded(email.attachments[0])attachment_metadata=attachment_controller.get_attachment_info(email.attachments[0])self.assertEqual(attachment_metadata.content_type,"text/plain")Fetch email by IDemail_controller=mailslurp_client.EmailControllerApi(api_client)email=email_controller.get_email(email_id=email_id)self.assertTrue("Hello"inemail.subject)Verify email addressYou can verify email addresses with MailSlurp. This will perform SMTP queries for the email address on your behalf.deftest_validate_email(self):withmailslurp_client.ApiClient(configuration)asapi_client:mailserver_controller=mailslurp_client.MailServerControllerApi(api_client)verify_options=mailslurp_client.VerifyEmailAddressOptions(email_address="test@gmail.com")result=mailserver_controller.verify_email_address(verify_options=verify_options)assertresult.errorisNoneassertresult.is_validisTrueSDK DocumentationSee theguides pageorthe examplesor theMethod Documentationfor full usage.