smpt lib multiple module error with a macbookpro - python

My code I have copied and pasted from real python.com
import smtplib, ssl
port = 587 # For starttls
smtp_server = "smtp.gmail.com"
sender_email = "sender#gmail.com"
receiver_email = "reciever#gmail.com"
password = input("Type your password and press enter:")
message = """\
Subject: Hi there
This message is sent from 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)
this somehow simple code to send an email produces a large list of errors which i cant track down.It seems to be an error with the module but im unaware how to resolve or trouble shoot these errors. I cant find any information on it as well; ive been looking for the past hour?
Traceback (most recent call last):
File "/Users/kaiwhitehouse/Documents/RetailEmailapp/lhbhbhlbhjbhjlbjljklhkjh.py", line 16, in <module>
server.starttls(context=context)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/smtplib.py", line 774, in starttls
self.sock = context.wrap_socket(self.sock,
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/ssl.py", line 500, in wrap_socket
return self.sslsocket_class._create(
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/ssl.py", line 1040, in _create
self.do_handshake()
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/ssl.py", line 1309, in do_handshake
self._sslobj.do_handshake()
ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1123)
Im running on mac os with a 2020 macbook pro but does anyone know the solution to my problem?

Related

Repl.it SMTPLIB OSError: [Errno 99] Cannot assign requested address

I am trying to run simple emailsender.py script on repl.it to send an email.
It works without any problem when I try to run it on desktop PC, but on repl.it I am having an error message OSError: [Errno 99] Cannot assign requested address as detailed below.
The emailsender.py program looks like this:
import smtplib
def send_email(username: str, key: str):
reciever = username+"#theirmail.cz"
sender = "my_email#email.cz"
topic = "Autothorization bot"
# header
msg = "From: %s\r\nSubject: %s\r\nTo: %s\r\n\r\n" % (sender, topic, reciever)
# add message content
content = "Your key is: " + key
msg += content
server = smtplib.SMTP('smtp.seznam.cz')
server.login('my_email#email.cz', "my_email_password")
server.sendmail(sender, reciever, msg)
server.quit()
When I try to run the script via python on repl.it, I get this error message after some time:
>>> import emailsender
>>> emailsender.send_email("username", "test_message")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/runner/botpy/emailsender.py", line 24, in send_email
server = smtplib.SMTP('smtp.seznam.cz')
File "/usr/lib/python3.8/smtplib.py", line 253, in __init__
(code, msg) = self.connect(host, port)
File "/usr/lib/python3.8/smtplib.py", line 337, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "/usr/lib/python3.8/smtplib.py", line 308, in _get_socket
return socket.create_connection((host, port), timeout,
File "/usr/lib/python3.8/socket.py", line 808, in create_connection
raise err
File "/usr/lib/python3.8/socket.py", line 796, in create_connection
sock.connect(sa)
OSError: [Errno 99] Cannot assign requested address
This .py script is a piece of larger discord.py bot that I am trying to host on repl.it, but this should not have any effect, because this script alone is running well individually on desktop PC.
I've tried my best to figure out the cause of this error, but with no success so far.
For reasons unknown to me, a small configuration of commands fixed it.
New part of the script for sending the email looks like this:
server = smtplib.SMTP('smtp.seznam.cz', 587)
server.starttls()
server.ehlo()
server.login('my_email#email.cz', "my_email_password")
server.sendmail(sender, reciever, msg)
server.quit()
I had the same error and
port=587 fixed the problem

Trying to send email through python and getting error with socket

This is my code
import smtplib
from datetime import datetime
print("Sending Mail")
with smtplib.SMTP('smpt.gmail.com', 587) as smtp:
smtp.ehlo()
smtp.starttls()
smtp.ehlo()
smtp.login('gmail', 'password')
print("Logged In")
subject = "Current Time: " + datetime.now.strftime("%d/%m/%Y %H:%M:%S")
smtp.sendmail('myemail#gmail.com', 'myemail#gmail.com', "hello")
print("Sent Email")
I'm trying to send an email through gmail using smtplib, this is the error I get:
Sending Mail
Traceback (most recent call last):
File "c:/Users/12488/Downloads/Key Logger/keylog.py", line 43, in <module>
with smtplib.SMTP('smpt.gmail.com', 587) as smtp:
File "C:\Users\12488\AppData\Local\Programs\Python\Python38\lib\smtplib.py", line 253, in __init__
(code, msg) = self.connect(host, port)
File "C:\Users\12488\AppData\Local\Programs\Python\Python38\lib\smtplib.py", line 339, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "C:\Users\12488\AppData\Local\Programs\Python\Python38\lib\smtplib.py", line 308, in _get_socket
return socket.create_connection((host, port), timeout,
File "C:\Users\12488\AppData\Local\Programs\Python\Python38\lib\socket.py", line 787, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
File "C:\Users\12488\AppData\Local\Programs\Python\Python38\lib\socket.py", line 918, in getaddrinfo
for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno 11001] getaddrinfo failed
I made sure less secure devices is checked on gmail but I haven't tried every solution on the internet because I'm new to python and this is my first time using smtp and I don't get a lot of them, I think this has something to do with socket because that's another popular python module from what I've read but I don't know much about it and I'm not sure, if anyone can simplify what is wrong and explain it to me like I'm a 5 year old, it would be greatly appreciated.
You're using an incorrect domain which is not being resolved by DNS servers.
smpt.gmail.com
should be:
smtp.gmail.com

Python Error SSL: ssl.SSLError: [SSL: SSLV3_ALERT_HANDSHAKE_FAILURE] sslv3 alert handshake failure (_ssl.c:726)

i'm trying to get the SNI from a (IP, destport 443) with python 2.7.15 and i'm using the last version of OpenSSL and the ssl module.
Here there is my code:
import OpenSSL as OSsl #This two modules are imported for the only purpose of getting the SNI using function defined by us down here getSNI
import ssl
ip = "52.85.25.17"
dport = "443"
#With this function we get the Server Name Identification for the trasmissions with Secure Socket Layer identified by the port 443. We only care about the destinationIP and the destinationPort
def getSNI(ip, dport):
if dport != "443":
commonName = "Not SSL"
print commonName
else:
server_certificate = ssl.get_server_certificate((ip, dport))
x509 = OSsl.crypto.load_certificate(OSsl.crypto.FILETYPE_PEM, server_certificate) #x509 is referred to the standard used for PKI (Public Key Infrastructure) used in this case for ciphering our informations about certificate
#FILETYPE_PEM serializes data to a Base64-Encoded
#getting the informations about Certificate
certInfo = x509.get_subject()
commonName = certInfo.commonName
print (commonName)
return commonName
getSNI(ip,dport)
This works, but for the address specified (in the snippet of code i posted here) I get this error:
Traceback (most recent call last):
File "getSNI.py", line 31, in <module>
getSNI(ip,dport)
File "getSNI.py", line 17, in getSNI
server_certificate = ssl.get_server_certificate((ip, dport))
File "/usr/lib/python2.7/ssl.py", line 1023, in get_server_certificate
with closing(context.wrap_socket(sock)) as sslsock:
File "/usr/lib/python2.7/ssl.py", line 369, in wrap_socket
_context=self)
File "/usr/lib/python2.7/ssl.py", line 617, in __init__
self.do_handshake()
File "/usr/lib/python2.7/ssl.py", line 846, in do_handshake
self._sslobj.do_handshake()
ssl.SSLError: [SSL: SSLV3_ALERT_HANDSHAKE_FAILURE] sslv3 alert handshake failure (_ssl.c:726)
I have all modules and packages upgraded, i read a lot of issues about this topic and i don't know how to solve this problem
Edit1: Executing whois I found that this IpAddress is connected to Amazon, so are there any particular issues about Amazon and SNI?
The point of SNI is that there could exists several domains which are resolved to concrete IP address. So, the IP you provided (52.85.25.17) is the one of such addresses. Server couldn't decide which domain's cert you are requesting, so it terminates the connection with error.
Appendix 1. Catching SSLError exceptions
You can catch ssl.SSLError this way:
try:
server_certificate = ssl.get_server_certificate((ip, dport))
...
except ssl.SSLError as e:
common_name = "Handshake Failed"

How i could to make SSl Connection server using username, password by python?

Hello I would ask if i could do connection to extract some data from Tool Server.
so i making this code but i need to modify it to open this tool with username and password and extract data from My tool server.
import socket
import ssl
HOST, PORT = '10.74.159.82', 31039
def handle(conn):
conn.write(b'GET / HTTP/1.1\n')
print(conn.recv() . decode())
def main():
sock = socket.socket(socket.AF_INET)
context = ssl.create_default_context(ssl.Purpose.SERVER_AUTH)
conn = context.wrap_socket(sock, server_hostname=HOST)
try:
conn.connect((HOST, PORT))
handle(conn)
finally:
conn.close()
if __name__ == '__main__':
main()
RESULT
!! C:\Users\Admin\.PyCharmCE2018.1\config\venv\Scripts\python.exe!! C:/Users/Admin/.PyCharmCE2018.1/config/codestyles/Under_Building.py
Traceback (most recent call last):
File "C:/Users/Admin/.PyCharmCE2018.1/config/codestyles/Under_Building.py", line 22, in <module>
main()
File "C:/Users/Admin/.PyCharmCE2018.1/config/codestyles/Under_Building.py", line 16, in main
conn.connect((HOST, PORT))
File "C:\Users\Admin\AppData\Local\Programs\Python\Python37-32\lib\ssl.py", line 1141, in connect
self._real_connect(addr, False)
File "C:\Users\Admin\AppData\Local\Programs\Python\Python37-32\lib\ssl.py", line 1132, in _real_connect
self.do_handshake()
File "C:\Users\Admin\AppData\Local\Programs\Python\Python37-32\lib\ssl.py", line 1108, in do_handshake
self._sslobj.do_handshake()
ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate in certificate chain (_ssl.c:1045)
Process finished with exit code 1
I'm Beginner so i need to learn and supporting
It's hard to see exactly, but it seems the server you're connecting to (on https://10.74.159.82:31039) is using a self-signed certificate (or its CA is, perhaps).
It's perhaps better to create or buy a proper (non-self-signed) certificate, but assuming this is a test server this is fine.
So with what you have, your best bet is to allow self-signed certs in your SSL Context (and also, not to check the hostname - that will probably fail too as you're using an IP address):
context = ssl.create_default_context()
context.check_hostname=False
context.verify_mode=ssl.CERT_NONE
# etc...

Unable to send email using gmail server using python

I am trying to send email using python. I tried many solution on Stackoverflow but none worked.
my code is -
import smtplib
fromaddr = 'user_me#gmail.com'
toaddrs = 'user_you#gmail.com'
msg = 'msg text here'
username = 'user_me#gmail.com'
password = 'pwd'
server = smtplib.SMTP('smtp.gmail.com',587)
server.ehlo()
server.starttls()
server.ehlo()
server.login(username,password)
server.sendmail(fromaddr, toaddrs, msg)
server.quit()
Code remain stuck at line server = smtplib.SMTP('smtp.gmail.com',587)
Tried port 465 and get this error -
Traceback (most recent call last):
File "gmail-python.py", line 130, in <module>
mainFunction()
File "gmail-python.py", line 101, in mainFunction
mail = smtplib.SMTP('smtp.gmail.com', 465)
File "/usr/lib/python2.7/smtplib.py", line 256, in __init__
(code, msg) = self.connect(host, port)
File "/usr/lib/python2.7/smtplib.py", line 317, in connect
(code, msg) = self.getreply()
File "/usr/lib/python2.7/smtplib.py", line 368, in getreply
raise SMTPServerDisconnected("Connection unexpectedly closed")
smtplib.SMTPServerDisconnected: Connection unexpectedly closed
tried this - smtplib.SMTP_SSL('smtp.gmail.com',465) and got this error
raise SMTPException("STARTTLS extension not supported by server.")
smtplib.SMTPException: STARTTLS extension not supported by server.
removing server.ehlo() had no effect.
removing server.starttls() caused below error -
raise SMTPAuthenticationError(code, resp)
smtplib.SMTPAuthenticationError: (534, '5.7.14 <https://accounts.google.com/signin/continue?sarp=1&scc=1&plt=AKgnsbsV\n5.7.14 3GHIcJvaksu56NrYm76aC0MqGI_sFI2cNaKSoFX6CV7TlyW593zn4dnJN7mG4TGI7edfcj\n5.7.14 l-MOV1Sb0Joyewnax8dxiL5BQ28Gg3UsWDfibI5TZ-tEaqqpL4WEzX5hsqYg6ZIQlczjyq\n5.7.14 UbElUx1C2E0QBt-X3uCuueb06uPBqehnwNV-hOlYFCgZpxRR9fdi-rO65oD5c1asYyGaTh\n5.7.14 1VRKgMYJp-o_uMhMjGEyJWKLLYX3A> Please log in via your web browser and\n5.7.14 then try again.\n5.7.14 Learn more at\n5.7.14 https://support.google.com/mail/answer/78754 r85sm13251579pfr.48 - gsmtp')
I have enabled less secure app access in my account. Python version 2.7.6 on ubuntu.
Try after turning of Gmail's Security Feature Here.
This may be preventing 3rd party application from accessing Gmail for security purpose
https://www.google.com/settings/security/lesssecureapps

Categories