pip install SMTPEmail
git clone https://github.com/Weizhang2017/SMTPEmail.git
cd SMTPEmail
python install setup.py
from SMTPEmail import SMTP
client = SMTP(
SMTP_server=<domain> or <IP_address>,
SMTP_account=<account_name>,
SMTP_password=<SMTP_password>
)
client.create_mime(
recipient_email_addr='[email protected]',
sender_email_addr='[email protected]',
subject='subject',
sender_display_name='John Doe',
recipient_display_name='Jane Doe',
content_html='<p>hello world</p>',
content_text='hello world'
)
client.send_msg()
custom_header = {'Sender': '[email protected]'}
client.create_mime(
recipient_email_addr='[email protected]',
sender_email_addr='[email protected]',
subject='subject',
sender_display_name='John Doe',
recipient_display_name='Jane Doe',
content_html='<p>hello world</p>',
content_text='hello world',
custom_header=custom_header
)
messages will be returned in byte object
from SMTPEmail import POP3
client = POP3(
POP3_server = <domain> or <IP_address>,
POP3_account = <account_name>,
POP3_password = <POP3_password>
)
msgs = client.retrieve_msg()
for msg in msgs:
print(msg)
>>>print(client.mailbox_size())
{'message count': 804, 'mailbox size': 18096539}
from SMTPEmail import IMAP
client = IMAP(
IMAP_server = <domain> or <IP_address>,
IMAP_account = <account_name>,
IMAP_password = <IMAP_password>
)
#you can select which mailbox to access and which email to retrieve by passing parameters 'mailbox_name' and 'msg_id'
for msg in client.retrieve_msg(mailbox_name='inbox', msg_id='1,3,5-8,11'):
print(msg)
#'mailbox_name' and 'msg_id' are optional, the user will be promted to input the two parameters if either is missing
>>>msgs = client.retrieve_msg()
0. "INBOX"
1. "Templates"
...
Please select a mailbox:1
message id: 1 2 3 4 5 6 ...
Please select mail ID to retrieve email(e.g. 1-5,6,7): 1,3-6
Additonal features:
- Specify variable
delete=True
to delete the messages that have been retrieved, default value isFalse
- Specify variable
msg_id='all'
to retrieve all messages
Refer to RFC3501 for more search options
>>>for msg in client.retrieve_msg(mailbox_name='inbox', search_section='body', search_text='test_search'):
print(msg)