Skip to content

Commit f697a6e

Browse files
committed
Updated exampels
1 parent 61e4073 commit f697a6e

1 file changed

Lines changed: 30 additions & 25 deletions

File tree

README.md

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,45 +12,50 @@ Or you can install it from source:
1212
pip install git+https://github.com/cubicbyte/tempmail-python.git
1313
```
1414

15-
## Usage example
15+
## Examples
1616

17+
Receive a message (e.g. activation code)
1718
```python
18-
import tempmail
19+
from tempmail import EMail
1920

20-
# Create a new email address
21-
email = tempmail.get_email()
22-
print(email)
21+
email = EMail()
2322

24-
# Wait for a new message
25-
msg = tempmail.wait_for_message(email)
26-
print(msg['body'])
23+
# ... request some email ...
2724

25+
msg = email.wait_for_message()
26+
print(msg.body) # Hello World!\n
2827
```
29-
Output:
28+
29+
Get all messages in the inbox
3030
```python
31-
# vhpeptbsne@1secmail.com
32-
# Hello World!
31+
from tempmail import EMail
32+
33+
email = EMail(username='example', domain='1secmail.com')
34+
inbox = email.get_inbox()
35+
36+
for msg_info in inbox:
37+
print(msg_info.subject, msg_info.message.body)
3338
```
3439

35-
Using message filters:
40+
Download an attachment
3641
```python
37-
import tempmail
42+
from tempmail import EMail
3843

39-
email = tempmail.get_email()
40-
print(email)
44+
email = EMail('example@1secmail.com')
45+
msg = email.wait_for_message()
4146

42-
# Wait for a new message from a specific sender
43-
msg = tempmail.wait_for_message(email, filter=lambda m: m['from'] == 'no-reply@example.com')
44-
print(msg['body'])
45-
```
47+
if msg.attachments:
48+
attachment = msg.attachments[0]
49+
data = attachment.download()
4650

47-
## API
51+
# Print
52+
print(data) # b'Hello World!\n'
53+
print(data.decode('utf-8')) # Hello World!\n
4854

49-
- `tempmail.get_email(username=None, domain=None)`: Generate a new email address.
50-
- `tempmail.get_inbox(email)`: Retrieve a list of message IDs for the specified email address.
51-
- `tempmail.get_message(email, id)`: Retrieve the contents of a message with the specified ID.
52-
- `tempmail.wait_for_message(email, timeout=None, filter=None)`: Wait for a new message to arrive at the specified email address. You can optionally provide a timeout (in seconds) and a filter function to check the contents of the message.
53-
- `tempmail.DOMAINS`: List of available email domains.
55+
# Save to file
56+
with open(attachment.filename, 'wb') as f:
57+
f.write(data)
58+
```
5459

5560
## License
5661
tempmail-python is licensed under the MIT License. See the [LICENSE](LICENSE) file for more information.

0 commit comments

Comments
 (0)