0

We have written a small Python program, where we want to send a mail to our mail address. We have some code, but when it's about to finish it stops and prints the error message. To be exact it stops at server.sendmail. When we print config.EMAIL_ADDRESS and config.PASSWORD, we receive the right credentials, so thats not the issue (config is where our credentials are stored).

We're using python3 on an ubuntu server.

import config
import smtplib

def send_mail(subject, msg):
    try:
        server = smtplib.SMTP('smtp.gmail.com:587')
        server.ehlo()
        server.starttls()
        print (f'EMAIL = {config.EMAIL_ADDRESS} & PASSWORD = {config.PASSWORD}')
        server.login(config.EMAIL_ADDRESS, config.PASSWORD)
        message = 'Subject: {}\n\n{}'.format(subject, msg)
        server.sendmail(config.EMAIL_ADDRESS, config.EMAIL_ADDRESS, message)
        server.quit()
        print ("Successfully sent email")
    except SMTPException:
        print ("Error: unable to send email")

subject = "ALERT!!!"
msg = "LPN SENDER IKKE DATA!!! GØR NOGET VED DET!!!"

send_mail(subject, msg)

We have opened port 25 and 587, that should not be a problem. We don't know what else to do. Maybe someone here can help us out. Also we need to establish a secure connection. Is this secure? If not, what can we do to secure the transport of our mail?

UPDATE

ERROR CODE

  File "Alarm.py", line 21, in send_mail
    server.sendmail(config.EMAIL_ADDRESS, config.EMAIL_ADDRESS, message)
  File "/usr/lib/python3.6/smtplib.py", line 855, in sendmail
    msg = _fix_eols(msg).encode('ascii')
UnicodeEncodeError: 'ascii' codec can't encode character '\xd8' in position 46: ordinal not in range(128)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "Alarm.py", line 48, in <module>
    send_mail(subject, msg)
  File "Alarm.py", line 25, in send_mail
    except SMTPException:
NameError: name 'SMTPException' is not defined
Buster3650
  • 141
  • 1
  • 10
  • Oh, I had problems with gmail and python myself! Let me find the code that eventually worked. – h4z3 Sep 05 '19 at 13:17
  • thanks! That would be awesome!! – Buster3650 Sep 05 '19 at 13:17
  • 2
    Remove the ``try...except`` or re-raise the exception, so that you (and we if you post the full error stack in the question) get an idea **what exactly** fails. – Mike Scotty Sep 05 '19 at 13:21
  • I had to turn on "less secure app access" in the past, but that was ssl. https://support.google.com/accounts/answer/6010255?hl=en – tgikal Sep 05 '19 at 13:25
  • If I recall correctly, gmail needs to use TLS. Also, I don't know if it's the problem, but SMTP normally gets host and port as 2 arguments. – h4z3 Sep 05 '19 at 13:26
  • Just updated question with error! Thanks, @MikeScotty for remember me on it – Buster3650 Sep 05 '19 at 13:32
  • 1
    Based on the error message and by counting the characters, the 46th character in your message is ``Ø``. Maybe this can help: https://stackoverflow.com/questions/8329741/ – Mike Scotty Sep 05 '19 at 13:35
  • Thanks, @MikeScotty. That worked! if you post your answer as the solution, then i'll make sure tick it as the solution – Buster3650 Sep 05 '19 at 14:40
  • Glad it helped. You should be able to close your own question as a duplicate of the linked question. I will also create a close vote, flagging your question as a dupe. – Mike Scotty Sep 05 '19 at 14:45

1 Answers1

1

The last time I worked with sending emails with Python the problem was that I hadn't enabled "Less secure Apps" for the gmail account. This is not the typical error caused by not having this enabled, but maybe you can still solve it by enabling it. You can do that here:

https://www.google.com/settings/security/lesssecureapps

Also it should be noted that Google automatically disables this setting again, if it hasn't been used for a while.

Zciurus-Alt-Del
  • 369
  • 1
  • 11