forked from testcontainers/testcontainers-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_basic.py
More file actions
62 lines (47 loc) · 1.95 KB
/
example_basic.py
File metadata and controls
62 lines (47 loc) · 1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import smtplib
import time
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import requests
from testcontainers.mailpit import MailpitContainer
def basic_example():
with MailpitContainer() as mailpit:
# Get SMTP and API endpoints
smtp_host = mailpit.get_container_host_ip()
smtp_port = mailpit.get_exposed_smtp_port()
api_url = mailpit.get_base_api_url()
# Create email message
msg = MIMEMultipart()
msg["From"] = "sender@example.com"
msg["To"] = "recipient@example.com"
msg["Subject"] = "Test Email"
body = "This is a test email sent to Mailpit."
msg.attach(MIMEText(body, "plain"))
# Send email using SMTP
with smtplib.SMTP(smtp_host, smtp_port) as server:
server.send_message(msg)
print("Email sent successfully")
# Wait for email to be processed
time.sleep(1)
# Check received emails using API
response = requests.get(f"{api_url}/api/v1/messages")
messages = response.json()
print("\nReceived emails:")
for message in messages["messages"]:
print(f"From: {message['From']['Address']}")
print(f"To: {message['To'][0]['Address']}")
print(f"Subject: {message['Subject']}")
print(f"Body: {message['Text']}")
print("---")
# Get specific email details
if messages["messages"]:
first_message = messages["messages"][0]
message_id = first_message["ID"]
response = requests.get(f"{api_url}/api/v1/messages/{message_id}")
message_details = response.json()
print("\nDetailed message info:")
print(f"Size: {message_details['Size']} bytes")
print(f"Created: {message_details['Created']}")
print(f"Attachments: {len(message_details['Attachments'])}")
if __name__ == "__main__":
basic_example()