· last year · Jun 16, 2024, 04:40 AM
1#!python3
2import mailslurp_client
3import os
4
5"""
6This script is designed to:
7
8 1. Connect to the MailSlurp API using an API key stored in the `MAILSLURP_API_KEY` environment variable.
9 2. Retrieve an inbox with a specific ID (`inbox_id`).
10 3. Wait for a specified number of emails (`email_num`) to arrive in the inbox.
11 4. Fetch the full email content for each email in the inbox.
12 5. Print the subject and body of each email.
13
14The script uses the `wait_for_email_count` method to wait for the specified number of emails to arrive in the inbox.
15
16This method takes the following parameters:
17
18 - `count`: The number of emails to wait for.
19 - `inbox_id`: The ID of the inbox to monitor.
20 - `timeout`: The time in milliseconds to wait for the emails to arrive.
21 - `unread_only`: A boolean indicating whether to only consider unread emails.
22
23If the script times out waiting for the emails to arrive, it prints a message indicating that there are not enough emails in the inbox for the specified `email_num`.
24
25If any other error occurs, the script prints an error message with the exception details.
26"""
27
28# Create a MailSlurp configuration
29configuration = mailslurp_client.Configuration()
30configuration.api_key['x-api-key'] = os.getenv("MAILSLURP_API_KEY")
31inbox_id = "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX"
32password = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
33email_num = 2 # Number of emails to fetch
34wait = 1 # Timeout in minutes
35
36try:
37 with mailslurp_client.ApiClient(configuration) as api_client:
38 inbox_controller = mailslurp_client.InboxControllerApi(api_client)
39 inbox = inbox_controller.get_inbox(inbox_id) # Replace with your inbox ID
40
41 # Wait for multiple emails to arrive in the inbox
42 wait_for_controller = mailslurp_client.WaitForControllerApi(api_client)
43 emails = wait_for_controller.wait_for_email_count(
44 count=email_num,
45 inbox_id=inbox.id,
46 timeout=wait*60*1000, # Milliseconds
47 unread_only=False
48 )
49
50 # Create an instance of EmailControllerApi
51 email_controller = mailslurp_client.EmailControllerApi(api_client)
52
53 for email_preview in emails:
54 # Fetch the full email using its ID
55 email = email_controller.get_email(email_id=email_preview.id)
56 print(email.subject)
57 print(email.body)
58
59except mailslurp_client.exceptions.ApiException as e:
60 if e.status == 408:
61 print(f"Request timed out: There are not enough emails in the inbox for 'email_num' set to {email_num}.")
62 else:
63 print(f"An error occurred: {e}")
64