from email.mime.multipart import MIMEMultipart
from email.mime.image import MIMEImage
from email.mime.text import MIMEText
import smtplib
import os
class Email:
def __init__(self, login, password) -> int:
"""
:param login: Mail login
:pram pass: Mail password
"""
self.host = 'smtp.gmail.com: 587'
self.login = login
self.password = password
self.from_mail = login
self.server = smtplib.SMTP(self.host)
self.server.starttls()
self.server.login(login, password)
def send_message(self, mail, text, attach=None):
"""
:param mail: who to send message in mail
:param text: text Message
:param attach: attach message
"""
if attach == None:
self._send_message(mail, text)
else:
self._send_attach_message(mail, text, attach)
def _send_attach_message(self, mail, text, attach):
"""
:param mail: who to send message in mail
:param text: text Message
:param attach: attach message
"""
msg = MIMEMultipart()
msg['To'] = mail
msg['From'] = self.from_mail
msg['Subject'] = "VKbonus"
msg.attach(MIMEText(text, 'plain'))
for f in attach:
msg.attach(MIMEImage(open(f, 'rb').read()))
os.remove(f)
self.server.sendmail(self.from_mail, mail, msg.as_string())
def _send_message(self, mail, text):
"""
:param mail: who to send message in mail
:param text: text Message
"""
msg = MIMEMultipart()
msg['To'] = mail
msg['From'] = self.from_mail
msg['Subject'] = "VKbonus"
msg.attach(MIMEText(text, 'plain'))
s = self.server.sendmail(self.from_mail, mail, msg.as_string())
print(s)