I’m having trouble with the smtplib

Asked

Viewed 70 times

0

The code only works when the subject and message(body) of the email are written in a few letters, I have no idea what it might be.

import smtplib

import config


def send_email(subject, msg):
    try:
        server = smtplib.SMTP('smtp-mail.outlook.com')
        server.ehlo()
        server.starttls()
        server.login(config.EMAIL_ADDRESS, config.EMAIL_PASS)
        message = 'Subject: {}\n\n{}'.format(subject, msg)
        server.sendmail(config.EMAIL_ADDRESS, config.EMAIL_TEST, message)
        server.quit()
        print("Envio de email foi concluido")
    except:
        print("Falha do envio de email")


subject = "Esse é um email teste, não precisa abrir"
msg = "Esse é um email teste obrigado"


send_email(subject, msg)

This is the error that appears:

Traceback (most recent call last):
  File "C:/Users/Douglas/PycharmProjects/pythonProject/main.py", line 21, in <module>
    send_email(subject, msg)
  File "C:/Users/Douglas/PycharmProjects/pythonProject/main.py", line 12, in send_email
    server.sendmail(config.EMAIL_ADDRESS, config.EMAIL_TEST, message)
  File "C:\Users\Douglas\AppData\Local\Programs\Python\Python38-32\lib\smtplib.py", line 859, in sendmail
    msg = _fix_eols(msg).encode('ascii')
UnicodeEncodeError: 'ascii' codec can't encode character '\xe3' in position 41: ordinal not in range(128)
Process finished with exit code 1
  • If too many letters returns some kind of error?

  • if cause, with the "except:" white m will never give to know: remove the try\except - let the mistakes happen while Oce develops, then you can know what is happening and tidy up. As it is, you lose all the information.

  • I made the mistake, it must be something related to subject and message, but I still haven’t found what can be.

1 answer

0

Sending email can be a bit tricky - with the "sendmail" method you are responsible for composing 100% of the body of your email, including the correct headers - you are putting the header "Subject" - but an email "good" too has time headers, content encoding, has to include the recipients and the source as well (inside the headers to email, not only in the protocol, outside of them, as parameter for a function).

So, it’s kind of cool to have worked with a kind of "hello world" for a short message - but a full email message has to be formatted according to RFC-822 specifications : https://tools.ietf.org/html/rfc822 (you will see that she is very boring to read). It doesn’t have many mandatory header fields - but out of place accents, incorrect white-color, among other things may leave the message invalid.

Another thing, as I said: never use one try\except "blank" as you did. You know an error happens, but you lose all the error information - remove "tr except" - do your development, and test - when you know what common errors your program can handle (like what happens when the recipient is invalid), you make a specific except for that mistake. Unknown errors should be allowed to pass through a log mechanism that allows you to read the complete message (in the case of a terminal program, the program stops and the error is printed on the terminal. If it is a routine on a system made with some web framework, the framework logs the error message and generates an HTTP 500 error message, for example)

And last but not least: using direct SMTP to send an email is a "different era" thing from the internet - from 30, until 25 years ago- email was much simpler. Because of the adaptations that have been made in the protocols and authentication for security account and avoid spam and spoofing this became very complicated - it is better to buy an email package as a service of some provider like "Mailchimp" and use an API - then it is simple again. (Alternatively, your internet hosting provider may have an email sending service as well, with an API other than SMTP).

  • 1

    Thanks, I started studying this to send email to my students, with all that Voce said I will have a direction to base myself, Thank you.

  • It has an integration that calls "IFTTT" - "If this than That" - it was almost all free until last year - and Ach which still has a free entry-level - look for this service on the web that I think can suit something like notifying students.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.