0

im just trying to make a simple code that sends emails to my clients with a PDF file attached, it was working on June when I created the version 1.0, now I'm recreating using a GUI and the sender is now broken

import smtplib
from email.message import EmailMessage

def Enviar_emails(email,senha, email_cli, pdf_cli):
    server_type = 'smtp.office365.com'
    port = 587
    try:
        print("iniciando envio de emails")

        msg = EmailMessage()
        msg['From'] = email
        msg.set_content("Aqui estão as novas atualizações em relação a mudança do aluguel de acordo com as normas IGP-M.")
        msg["To"] = email_cli
        msg["Subject"] = "Atualização do valor de custo mensal da propriedade."
        fileToSend = pdf_cli
        emailToSend = email_cli
        print(emailToSend)
        contFile = open(f'.\\pdf\\{fileToSend}', 'rb')
        cont = contFile.read()
        msg.add_attachment(cont, maintype='application', subtype='pdf', filename=fileToSend)
        server = smtplib.SMTP(server_type, port)
        server.starttls()
        server.login(email, senha)
        server.send_message(msg)
        msg.clear_content()
    except Exception as e:
        print(f"Error: {e}")

Enviar_emails('[email protected]', 'app_password', '[email protected]','fileToSend.pdf')

When I tried this code I received this error:

(535, b'5.7.139 Authentication unsuccessful, basic authentication is disabled. [CP5P284CA0198.BRAP284.PROD.OUTLOOK.COM 2024-12-12T23:57:36.327Z 08DD19E380D79708]')

I believe that the error is on or the Microsoft outlook account, or on the smtplib config

the account was created today just to test the smtplib and make this code work

3
  • This question is similar to: Sending Email from Office 365 no longer supports Basic Authentication and SMTP. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem. Commented Dec 13, 2024 at 0:21
  • 1
    The error means what it says: you cannot use basic auth. You must use OAuth. Commented Dec 13, 2024 at 2:06
  • Hello, it happened to me as well but I am trying to do it through n8n and there is not an option for OAuth.... How can I solve this ? Commented Feb 21, 2025 at 20:07

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.