1

OS: MacCatalinva V10.15.3

Python: 3.7.7

PiP: 20.0.2

Hey, I'm new to coding so I'm not sure what this really means.

I'm trying to send emails via Python through Gmail, I've set my account to accept "Less secure app access" and followed the steps in this guide, but all I get is the following:

`[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1076) Traceback (most recent call last): File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/smtplib.py", line 354, in send self.sock.sendall(s) OSError: [Errno 9] Bad file descriptor

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "/Users/mymac/Desktop/Test2.py", line 34, in server.quit() File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/smtplib.py", line 984, in quit res = self.docmd("quit") File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/smtplib.py", line 420, in docmd self.putcmd(cmd, args) File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/smtplib.py", line 367, in putcmd self.send(str) File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/smtplib.py", line 357, in send raise SMTPServerDisconnected('Server not connected') smtplib.SMTPServerDisconnected: Server not connected`

And this is my Code:

import smtplib
import ssl


sender_email = "myemailadress@gmail.com"
receiver_email = "myadress@hotmail.com"
message = """\
Subject: Hi there

This message is sent from Python."""

# Send email here

smtp_server = "smtp.gmail.com"
port = 587  # For starttls
sender_email = "myemailadress@gmail.com"
password = input("Type your password and press enter: ")

# Create a secure SSL context
context = ssl.create_default_context()

# Try to log in to server and send email
try:
    server = smtplib.SMTP(smtp_server, port)
    server.ehlo()  # Can be omitted
    server.starttls(context=context)  # Secure the connection
    server.ehlo()  # Can be omitted
    server.login(sender_email, password)
    # TODO: Send email here
except Exception as e:
    # Print any error messages to stdout
    print(e)
finally:
    server.quit()
Ribz
  • 13
  • 2

1 Answers1

0

1)After this line:

server.login(sender_email, password)

make sure to send the message you have it. For that:

server.sendmail(sender_email,receiver_email,message)    

that's it, I hope.

mystica
  • 32
  • 6
  • 1
    Hey Mystica, thank you so much, it works like a charm! May I ask you what the changes actually does? Also, are that these messages still encrypted? – Ribz Mar 21 '20 at 13:01
  • 1
    I also noticed that I can't use foreign language symbols like "åäöü", it tells me: "ascii' codec can't encode character '\xe4' in position 15: ordinal not in range(128)" Do you know of a fix? – Ribz Mar 21 '20 at 13:17
  • @Ribz i'm sorry that i missed secure connection and it will work bud. and see this to support the other language in mail https://stackoverflow.com/a/39246290/11048119 – mystica Mar 22 '20 at 17:17