Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import socket
- import ssl
- import re
- import base64
- import time
- import mimetypes
- def command_HELO(sock, name):
- sock.send(b'EHLO ' + name.encode('utf-8') + b'\r\n')
- sock.recv(4096)
- print('Инициализация')
- def command_MAILFROM(sock, addr):
- sock.send(b'MAIL FROM:' + addr.encode('utf-8') + b' BODY=8BITMIME\r\n')
- sock.recv(4096)
- def command_RCPTTO(sock, addr):
- sock.send(b'RCPT TO:' + addr.encode('utf-8') + b'\r\n')
- sock.recv(4096)
- def command_DATA(sock, message):
- sock.send(b'DATA\r\n')
- sock.recv(4096)
- sock.send(message)
- sock.recv(4096)
- def command_AUTHLOGIN(sock, user, password):
- sock.send(b'AUTH LOGIN\r\n')
- sock.recv(4096)
- user = base64.b64encode(user.encode('utf-8'))
- sock.send(user + b'\r\n')
- sock.recv(4096)
- password = base64.b64encode(password.encode('utf-8'))
- sock.send(password + b'\r\n')
- sock.recv(4096)
- def command_QUIT(sock):
- sock.send(b'QUIT\r\n')
- sock.recv(4096)
- print('Выход')
- print('---------------------------')
- def command_NOOP(sock):
- sock.send(b'NOOP\r\n')
- data = sock.recv(4096)
- print(data)
- def command_RSET(sock):
- sock.send(b'RSET\r\n')
- data = sock.recv(4096)
- print(data)
- def send_mail(namefile, user, to_name):
- config = ''
- with open(namefile + '/config.txt') as file:
- config = file.readlines()
- message = ''
- from_mail = b'From: ' + user.encode() + b'\r\n'
- to_mail = b'To: ' + to_name.encode() + b'\r\n'
- subject = ''
- for string in config:
- match = re.search('Subject: (.*)\n', string)
- if match:
- subject = base64.b64encode(string[match.start(1): match.end(1)].encode())
- break
- subject_mail = b'Subject: ' + b'=?utf-8?B?' + subject + b'?=\r\n'
- mime_version = b'MIME-Version: 1.0\r\n'
- date_mail = b'Date: ' + time.ctime(time.time()).encode() + b'\r\n'
- content_type = b'Content-Type: multipart/mixed; boundary="----KYJSJE9iRgbLwJdmIO9w69Xi359qsDEW-RYtZBRHAE1N2K1ZG-1490934070"'
- headers = from_mail + to_mail + subject_mail + mime_version + date_mail + content_type + b'\r\n'
- message += headers.decode()
- skoba = b'------KYJSJE9iRgbLwJdmIO9w69Xi359qsDEW-RYtZBRHAE1N2K1ZG-1490934070\r\n'
- content_type = b'Content-Type: text/plain; charset=utf-8\r\n'
- content_transfer_encoding = b'Content-Transfer-Encoding: base64\r\n\r\n'
- with open(namefile + '/msg_text.txt') as file:
- text_plain = file.read()
- text_plain = text_plain.encode()
- text_plain = base64.b64encode(text_plain)
- text = b'\r\n' + skoba + content_type + content_transfer_encoding + text_plain + b'\r\n' + skoba
- message += text.decode()
- skoba = b'------KYJSJE9iRgbLwJdmIO9w69Xi359qsDEW-RYtZBRHAE1N2K1ZG-1490934070\r\n'
- attachs = ''
- for string in config:
- match = re.search('Attachments: (.*)\n', string)
- if match:
- attachs = string[match.start(1): match.end(1)]
- break
- full_attachs = b''
- for attach in attachs.split(', '):
- with open(namefile + attach, 'rb') as file:
- data = base64.b64encode(file.read())
- name = b'=?utf-8?B?' + base64.b64encode(attach.split('/')[-1].encode()) + b'?="\r\n'
- cont_file = mimetypes.guess_type(namefile + attach)
- content_type = b'Content-Type: ' + cont_file[0].encode() + b'; name="' + name
- content_dispos = b'Content-Disposition: attachment; filename="' + name
- content_transfer_encoding = b'Content-Transfer-Encoding: base64\r\n\r\n'
- full_data = (content_type + content_dispos + content_transfer_encoding +
- data + b'\r\n' + skoba)
- full_attachs += full_data
- message += full_attachs.decode()
- return message.encode() + b'.\r\n'
- def start(smtp_server, user, password, mail):
- try:
- socket_smtp = socket.socket(socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_TCP)
- socket_smtp = ssl.wrap_socket(socket_smtp)
- socket_smtp.connect((smtp_server, 465))
- socket_smtp.recv(4096)
- command_HELO(socket_smtp, 'localhost')
- command_AUTHLOGIN(socket_smtp, user, password)
- arr_names = []
- with open(mail + '/config.txt') as file:
- strings = file.readlines()
- for string in strings:
- match = re.search('To: (.*)', string)
- if match:
- to_names = string[match.start(1): match.end(1)]
- for to_name in to_names.split(', '):
- arr_names.append(to_name)
- break
- for to_name in arr_names:
- command_MAILFROM(socket_smtp, user)
- message = send_mail(mail, user, to_name)
- command_RCPTTO(socket_smtp, to_name)
- command_DATA(socket_smtp, message)
- print('Все сообщения отправлены')
- command_QUIT(socket_smtp)
- except Exception:
- print('Неверные входные данные')
- if __name__ == '__main__':
- print('mail.ru, gumarov.radik.96@mail.ru, Palatanomer1')
- print('yandex.ru, ouler111@yandex.ru, Palatanomer1')
- print('rambler.ru, gumarov.radik@rambler.ru, Palatanomer1')
- print('----------------------------------------------------')
- with open('start_point/smtp_server.txt') as file:
- smtp_server = file.read()
- with open('start_point/user.txt') as file:
- user = file.read()
- with open('start_point/password.txt') as file:
- password = file.read()
- start(smtp_server, user, password, 'mail')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement