Advertisement
Guest User

Untitled

a guest
Apr 19th, 2017
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.59 KB | None | 0 0
  1. import socket
  2. import ssl
  3. import re
  4. import base64
  5. import time
  6. import mimetypes
  7.  
  8. def command_HELO(sock, name):
  9.     sock.send(b'EHLO ' + name.encode('utf-8') + b'\r\n')
  10.     sock.recv(4096)
  11.     print('Инициализация')
  12.  
  13. def command_MAILFROM(sock, addr):
  14.     sock.send(b'MAIL FROM:' + addr.encode('utf-8') + b' BODY=8BITMIME\r\n')
  15.     sock.recv(4096)
  16.  
  17. def command_RCPTTO(sock, addr):
  18.     sock.send(b'RCPT TO:' + addr.encode('utf-8') + b'\r\n')
  19.     sock.recv(4096)
  20.  
  21. def command_DATA(sock, message):
  22.     sock.send(b'DATA\r\n')
  23.     sock.recv(4096)
  24.     sock.send(message)
  25.     sock.recv(4096)
  26.  
  27. def command_AUTHLOGIN(sock, user, password):
  28.     sock.send(b'AUTH LOGIN\r\n')
  29.     sock.recv(4096)
  30.     user = base64.b64encode(user.encode('utf-8'))
  31.     sock.send(user + b'\r\n')
  32.     sock.recv(4096)
  33.     password = base64.b64encode(password.encode('utf-8'))
  34.     sock.send(password + b'\r\n')
  35.     sock.recv(4096)
  36.  
  37. def command_QUIT(sock):
  38.     sock.send(b'QUIT\r\n')
  39.     sock.recv(4096)
  40.     print('Выход')
  41.     print('---------------------------')
  42.  
  43. def command_NOOP(sock):
  44.     sock.send(b'NOOP\r\n')
  45.     data = sock.recv(4096)
  46.     print(data)
  47.  
  48. def command_RSET(sock):
  49.     sock.send(b'RSET\r\n')
  50.     data = sock.recv(4096)
  51.     print(data)
  52.  
  53. def send_mail(namefile, user, to_name):
  54.     config = ''
  55.     with open(namefile + '/config.txt') as file:
  56.         config = file.readlines()
  57.     message = ''
  58.  
  59.     from_mail = b'From: ' + user.encode() + b'\r\n'
  60.     to_mail = b'To: ' + to_name.encode() + b'\r\n'
  61.     subject = ''
  62.     for string in config:
  63.         match = re.search('Subject: (.*)\n', string)
  64.         if match:
  65.             subject = base64.b64encode(string[match.start(1): match.end(1)].encode())
  66.             break
  67.     subject_mail = b'Subject: ' + b'=?utf-8?B?' + subject + b'?=\r\n'
  68.     mime_version = b'MIME-Version: 1.0\r\n'
  69.     date_mail = b'Date: ' + time.ctime(time.time()).encode() + b'\r\n'
  70.     content_type = b'Content-Type: multipart/mixed; boundary="----KYJSJE9iRgbLwJdmIO9w69Xi359qsDEW-RYtZBRHAE1N2K1ZG-1490934070"'
  71.     headers = from_mail + to_mail + subject_mail + mime_version + date_mail + content_type + b'\r\n'
  72.     message += headers.decode()
  73.  
  74.     skoba = b'------KYJSJE9iRgbLwJdmIO9w69Xi359qsDEW-RYtZBRHAE1N2K1ZG-1490934070\r\n'
  75.     content_type = b'Content-Type: text/plain; charset=utf-8\r\n'
  76.     content_transfer_encoding = b'Content-Transfer-Encoding: base64\r\n\r\n'
  77.     with open(namefile + '/msg_text.txt') as file:
  78.         text_plain = file.read()
  79.     text_plain = text_plain.encode()
  80.     text_plain = base64.b64encode(text_plain)
  81.     text = b'\r\n' + skoba + content_type + content_transfer_encoding + text_plain + b'\r\n' + skoba
  82.     message += text.decode()
  83.  
  84.     skoba = b'------KYJSJE9iRgbLwJdmIO9w69Xi359qsDEW-RYtZBRHAE1N2K1ZG-1490934070\r\n'
  85.     attachs = ''
  86.     for string in config:
  87.         match = re.search('Attachments: (.*)\n', string)
  88.         if match:
  89.             attachs = string[match.start(1): match.end(1)]
  90.             break
  91.     full_attachs = b''
  92.     for attach in attachs.split(', '):
  93.         with open(namefile + attach, 'rb') as file:
  94.             data = base64.b64encode(file.read())
  95.             name = b'=?utf-8?B?' + base64.b64encode(attach.split('/')[-1].encode()) + b'?="\r\n'
  96.             cont_file = mimetypes.guess_type(namefile + attach)
  97.             content_type = b'Content-Type: ' + cont_file[0].encode() + b'; name="' + name
  98.             content_dispos = b'Content-Disposition: attachment; filename="' + name
  99.             content_transfer_encoding = b'Content-Transfer-Encoding: base64\r\n\r\n'
  100.             full_data = (content_type + content_dispos + content_transfer_encoding +
  101.                         data + b'\r\n' + skoba)
  102.             full_attachs += full_data
  103.     message += full_attachs.decode()
  104.  
  105.     return message.encode() + b'.\r\n'
  106.  
  107. def start(smtp_server, user, password, mail):
  108.     try:
  109.         socket_smtp = socket.socket(socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_TCP)
  110.         socket_smtp = ssl.wrap_socket(socket_smtp)
  111.         socket_smtp.connect((smtp_server, 465))
  112.         socket_smtp.recv(4096)
  113.         command_HELO(socket_smtp, 'localhost')
  114.         command_AUTHLOGIN(socket_smtp, user, password)
  115.         arr_names = []
  116.         with open(mail + '/config.txt') as file:
  117.             strings = file.readlines()
  118.             for string in strings:
  119.                 match = re.search('To: (.*)', string)
  120.                 if match:
  121.                     to_names = string[match.start(1): match.end(1)]
  122.                     for to_name in to_names.split(', '):
  123.                         arr_names.append(to_name)
  124.                     break
  125.         for to_name in arr_names:
  126.             command_MAILFROM(socket_smtp, user)
  127.             message = send_mail(mail, user, to_name)
  128.             command_RCPTTO(socket_smtp, to_name)
  129.             command_DATA(socket_smtp, message)
  130.         print('Все сообщения отправлены')
  131.         command_QUIT(socket_smtp)
  132.     except Exception:
  133.         print('Неверные входные данные')
  134.  
  135. if __name__ == '__main__':
  136.     print('mail.ru, gumarov.radik.96@mail.ru, Palatanomer1')
  137.     print('yandex.ru, ouler111@yandex.ru, Palatanomer1')
  138.     print('rambler.ru, gumarov.radik@rambler.ru, Palatanomer1')
  139.     print('----------------------------------------------------')
  140.     with open('start_point/smtp_server.txt') as file:
  141.         smtp_server = file.read()
  142.     with open('start_point/user.txt') as file:
  143.         user = file.read()
  144.     with open('start_point/password.txt') as file:
  145.         password = file.read()
  146.     start(smtp_server, user, password, 'mail')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement