通过SMTP服务器发送mime邮件并通过POP3和IMAP检索邮件
SMTPEmail的Python项目详细描述
通过smtp服务器发送mime消息并通过pop3检索消息的简单脚本
安装pip install SMTPEmail
最新测试版本pip install -i https://test.pypi.org/simple/ SMTPEmail
使用量
通过SMTP发送电子邮件
>>>from SMTPEmail import SMTP
>>>client = SMTP(SMTP_server= <domain> or <IP_address>,
SMTP_account= <account_name>,
SPMP_password= <SMTP_password>
)
>>>client.create_mime(recipient_email_addr='Jane.Doe@jane.com',
sender_email_addr='John.Doe@john.com',
subject='subject',
sender_display_name='John Doe',
recipient_display_name='Jane Doe',
content_html='<p>hello world</p>',
content_text='hello world')
>>>print(client)#print the message
Subject: subject
From: John Doe <John.Doe@John.com>
To: Jane Doe <Jane.Doe@jane.com>
Date: Tue, 13 Nov 201817:52:01 +0800
Message-Id: 154210272151.1976.14038029430513051529@Jane.com
MIME-Version: 1.0
Content-Type: multipart/alternative;boundary="===============0340024224523524971=="
--===============0340024224523524971==
Content-Type: text/plain;charset="utf-8"
Content-Transfer-Encoding: 7bit
hello world
--===============0340024224523524971==
Content-Type: text/html;charset="utf-8"
Content-Transfer-Encoding: 7bit
MIME-Version: 1.0
<p>hello world</p>
--===============0340024224523524971==--
>>>client.send_msg()
通过POP3检索电子邮件
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}
通过IMAP检索电子邮件
Additonal features:
- 指定变量
delete=True
以删除已检索的消息,默认值为False
- 指定变量
msg_id='all'
以检索所有消息
>>>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: 123456 ...
Please select mail ID to retrieve email(e.g. 1-5,6,7): 1,3-6
通过IMAP搜索电子邮件
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)
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}
通过IMAP检索电子邮件
Additonal features:
- 指定变量
delete=True
以删除已检索的消息,默认值为False
- 指定变量
msg_id='all'
以检索所有消息
>>>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: 123456 ...
Please select mail ID to retrieve email(e.g. 1-5,6,7): 1,3-6
通过IMAP搜索电子邮件
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)
Additonal features:
delete=True
以删除已检索的消息,默认值为False
msg_id='all'
以检索所有消息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)