J2897 icon

MailSlurp - Receive

J2897 | PRO | 06/16/24 12:36:37 AM UTC (Edited) | 0 ⭐ | 1074 👁️ | Never ⏰ | []
Python |

2.63 KB

|

None

|

0 👍

/

0 👎

#!python3
import mailslurp_client
import os
 
"""
This script is designed to:
 
    1. Connect to the MailSlurp API using an API key stored in the `MAILSLURP_API_KEY` environment variable.
    2. Retrieve an inbox with a specific ID (`inbox_id`).
    3. Wait for a specified number of emails (`email_num`) to arrive in the inbox.
    4. Fetch the full email content for each email in the inbox.
    5. Print the subject and body of each email.
 
The script uses the `wait_for_email_count` method to wait for the specified number of emails to arrive in the inbox.
 
This method takes the following parameters:
 
    - `count`: The number of emails to wait for.
    - `inbox_id`: The ID of the inbox to monitor.
    - `timeout`: The time in milliseconds to wait for the emails to arrive.
    - `unread_only`: A boolean indicating whether to only consider unread emails.
 
If 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`.
 
If any other error occurs, the script prints an error message with the exception details.
"""
 
# Create a MailSlurp configuration
configuration = mailslurp_client.Configuration()
configuration.api_key['x-api-key'] = os.getenv("MAILSLURP_API_KEY")
inbox_id = "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX"
password = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
email_num = 2   # Number of emails to fetch
wait = 1        # Timeout in minutes
 
try:
    with mailslurp_client.ApiClient(configuration) as api_client:
        inbox_controller = mailslurp_client.InboxControllerApi(api_client)
        inbox = inbox_controller.get_inbox(inbox_id)  # Replace with your inbox ID
 
        # Wait for multiple emails to arrive in the inbox
        wait_for_controller = mailslurp_client.WaitForControllerApi(api_client)
        emails = wait_for_controller.wait_for_email_count(
            count=email_num,
            inbox_id=inbox.id,
            timeout=wait*60*1000, # Milliseconds
            unread_only=False
        )
 
        # Create an instance of EmailControllerApi
        email_controller = mailslurp_client.EmailControllerApi(api_client)
 
        for email_preview in emails:
            # Fetch the full email using its ID
            email = email_controller.get_email(email_id=email_preview.id)
            print(email.subject)
            print(email.body)
 
except mailslurp_client.exceptions.ApiException as e:
    if e.status == 408:
        print(f"Request timed out: There are not enough emails in the inbox for 'email_num' set to {email_num}.")
    else:
        print(f"An error occurred: {e}")
 

Comments

  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎