M
M
MichaelMih2021-06-29 22:29:14
Python
MichaelMih, 2021-06-29 22:29:14

Why is smtp not sending messages to email?

Good afternoon!
For some reason, sending to email ( mail.ru ) does not work.
The code:

spoiler
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)


_send_message function

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question