FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
testcontainers-python/modules/mailpit/example_basic.py at main · f18m/testcontainers-python · GitHub
f18m
/
testcontainers-python
Public
forked from
testcontainers/testcontainers-python
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
1
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
testcontainers-python
/
modules
/
mailpit
/
example_basic.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
62 lines (47 loc) · 1.95 KB
Breadcrumbs
testcontainers-python
/
modules
/
mailpit
/
example_basic.py
Copy path
File metadata and controls
62 lines (47 loc) · 1.95 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
(
"
\n
Received 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
(
"
\n
Detailed 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
()
Back
|
FazBrowse Home
|
New Git URL