Advertisement
stuppid_bot

Отправка письма через мобильную версию маила

Oct 24th, 2013
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.16 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. import sys
  3. import httplib
  4. import urllib
  5. import re
  6. import urllib2
  7. import uuid
  8. user = '***@bk.ru'
  9. pswd = '***'
  10. # 1 - если хотите использовать TOR
  11. using_tor = 0
  12. if using_tor:
  13.     socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, '127.0.0.1', 9150)
  14.     socket.socket = socks.socksocket
  15. _ua = 'Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36' # если оставить пустым передаст что-то типа "Python-urllib/2.7"
  16. _cookie = ''
  17.  
  18. def auth(login, password):
  19.     conn = httplib.HTTPConnection('m.mail.ru')
  20.     conn.request('HEAD', '/cgi-bin/auth?Login=' + urllib.quote(login)+ '&Password=' + urllib.quote(password), headers={'User-Agent': _ua})
  21.     res = conn.getresponse()
  22.     if res.status == 302:
  23.         location = res.getheader('location')
  24.         if 'https://e.mail.ru/messages/inbox/?back=1' == location:
  25.             global _cookie
  26.             params = re.findall(r'(?:^|(?<=, ))\w+=[^;]+', res.getheader('set-cookie'))
  27.             _cookie = '; '.join(params)
  28.             return True
  29.     return False
  30.  
  31. def fetch_path(uri, content=None, add_headers={}):
  32.     headers = {
  33.         'User-Agent': _ua,
  34.         'Cookie': _cookie
  35.     }
  36.     headers.update(add_headers)
  37.     print headers
  38.     req = urllib2.Request('https://m.mail.ru/' + uri, content, headers)
  39.     return urllib2.urlopen(req)
  40.  
  41. def send_mail(to, subject, body):
  42.     resp = fetch_path('/compose/')
  43.     # извелекаем значения скрытых полей
  44.     fields = re.findall('<input type="hidden" name="([^"]+)" value="([^"]*)" />', resp.read())
  45.     fields += [
  46.         ('To', to),
  47.         # ('CC', ''),
  48.         # ('BBC', ''),
  49.         ('Subject', subject),
  50.         ('Body', body),
  51.         ('send', '')
  52.     ]
  53.     # boundary = uuid.uuid4().hex
  54.     # resp = fetch_path('/compose/', '', {'Content-Type': 'multipart/form-data; boundary=' + boundary})
  55.     resp = fetch_path('/compose/', urllib.urlencode(dict(fields)), {'Content-Type': 'application/x-www-form-urlencoded'})
  56.     if resp.code == 200:
  57.         s = resp.read().decode('utf-8')
  58.         if s.find(u'Письмо отправлено — Почта Mail.Ru</title>') > 0:
  59.             return True
  60.     return False
  61.  
  62. if auth(user, pswd) == False:
  63.     print u'Ошибка: неправильный логин либо пароль'.encode(sys.stdin.encoding)
  64.     sys.exit(0)
  65.  
  66. to = raw_input(u'Введите адрес получателя: '.encode(sys.stdin.encoding))
  67. subject = raw_input(u'Тема письма: '.encode(sys.stdin.encoding))
  68. print u'Текст письма:\n\n'.encode(sys.stdin.encoding)
  69. body = ''
  70. while True:
  71.     body += raw_input() + '\n'
  72.     if body[-2:] == '\n\n':
  73.         break
  74. body = body.strip()
  75. # заебался с этими кодировками
  76. subject = subject.decode(sys.stdin.encoding).encode('utf-8')
  77. body = body.decode(sys.stdin.encoding).encode('utf-8')
  78. if send_mail(to, subject, body):
  79.     print u'Письмо отправлено'.encode(sys.stdin.encoding)
  80. else:
  81.     print u'Ошибка при отправке письма'.encode(sys.stdin.encoding)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement