Created
December 15, 2011 12:34
-
-
Save dketov/1480947 to your computer and use it in GitHub Desktop.
Почтовые протоколы
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- encoding: utf-8 -*- | |
""" | |
Отправка почты по протоколу SMTP | |
""" | |
import smtplib | |
def prompt(prompt): | |
return raw_input(prompt).strip() | |
fromaddr = prompt("From: ") | |
toaddrs = prompt("To: ").split() | |
print "Enter message, end with ^D (Unix) or ^Z (Windows):" | |
# Add the From: and To: headers at the start! | |
msg = ("From: %s\r\nTo: %s\r\n\r\n" | |
% (fromaddr, ", ".join(toaddrs))) | |
while 1: | |
try: | |
line = raw_input() | |
except EOFError: | |
break | |
if not line: | |
break | |
msg = msg + line | |
print "Message length is " + repr(len(msg)) | |
server = smtplib.SMTP('localhost') | |
server.set_debuglevel(1) | |
server.sendmail(fromaddr, toaddrs, msg) | |
server.quit() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- encoding: utf-8 -*- | |
""" | |
Получение почты по протоколу POP3 | |
""" | |
import getpass, poplib | |
M = poplib.POP3('localhost') | |
M.user(getpass.getuser()) | |
M.pass_(getpass.getpass()) | |
numMessages = len(M.list()[1]) | |
for i in range(numMessages): | |
for j in M.retr(i+1)[1]: | |
print j |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- encoding: utf-8 -*- | |
""" | |
Получение почты по протоколу IMAP | |
""" | |
import getpass, imaplib | |
M = imaplib.IMAP4() | |
M.login(getpass.getuser(), getpass.getpass()) | |
M.select() | |
typ, data = M.search(None, 'ALL') | |
for num in data[0].split(): | |
typ, data = M.fetch(num, '(RFC822)') | |
print 'Message %s\n%s\n' % (num, data[0][1]) | |
M.close() | |
M.logout() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment