-2
import smtplib, ssl

port = 587  # For starttls
smtp_server = "smtp.gmail.com"
sender_email = "jppythons@gmail.com"
receiver_email = "jppythons@gmail.com"
password = input("pass")
message = """\
Subject: Email Test1

First eamil ever using Python."""

context = ssl.create_default_context()
with smtplib.SMTP(smtp_server, port) as server:
    server.ehlo()  # Can be omitted
    server.starttls(context=context)
    server.ehlo()  # Can be omitted
    server.login(sender_email, password)
    server.sendmail(sender_email, receiver_email, message)

The only thing it returns when I run my code is my password... nothing else happens, no errors, and no email

Georgy
  • 12,464
  • 7
  • 65
  • 73
Peter Pan
  • 35
  • 8
  • 1
    Does this answer your question? [How to send an email with Python?](https://stackoverflow.com/questions/6270782/how-to-send-an-email-with-python) – Georgy Dec 14 '20 at 16:35

1 Answers1

0

i would use try except so you can see if there are any errors. for example,

https://docs.python.org/3/tutorial/errors.html#handling-exceptions

try:
    server = smtplib.SMTP(smtp_server,port)
    server.ehlo()
    server.starttls(context=context)
    server.ehlo()
    server.login(sender_email, password)
    server.sendmail(sender_email, receiver_email, message)
except Exception as e:
    # Print any error messages to stdout
    print(e)
finally:
    server.quit()
vadderung
  • 13
  • 1
  • 8
  • Traceback (most recent call last): File "/home/pi/Desktop/Python Testes/Emails.py", line 21 except Exception as e: ^ SyntaxError: invalid syntax – Peter Pan Dec 14 '20 at 16:37
  • can you please share your updated code - or a few lines around line 21? – vadderung Dec 14 '20 at 16:41
  • context = ssl.create_default_context() with smtplib.SMTP(smtp_server, port) as server: server.ehlo() # Can be omitted server.starttls(context=context) server.ehlo() # Can be omitted server.login(sender_email, password) server.sendmail(sender_email, receiver_email, message) except Exception as e: print(e) server.quit() – Peter Pan Dec 14 '20 at 16:50
  • i don't see "try" in your code. it needs to be formatted like above. in short, replace with smtplib.SMTP(smtp_server, port) as server with the above try: server = smtplib.SMTP(smtp_server,port) – vadderung Dec 14 '20 at 16:53
  • context = ssl.create_default_context() try: server = smtplib.SMTP(smtp_server,port) server.ehlo() server.starttls(context=context) server.ehlo() server.login(sender_email, password) server.sendmail(sender_email, receiver_email, message) except Exception as e: # Print any error messages to stdout print(e) finally: server.quit() It returns: my password again, nothing else – Peter Pan Dec 14 '20 at 18:27
  • what version of python are you using? i just tested with python 3.8.x and received email – vadderung Dec 14 '20 at 19:05
  • I´m running Thonny in a raspberry 2 with Python 3.7.3 – Peter Pan Dec 15 '20 at 10:24
  • Already using python 3.8 :), never less the code runs with no errors but noting happens ;( – Peter Pan Dec 15 '20 at 16:01