i've looked online but every time i try to connect to the localhost it says connection refused. Do i need to sign into a valid email address to send email?
Here is my EXACT code.
>>> import smtplib
>>> sender = 'from#fromdomain.com'
#this is my exact sender name bc i don't know if i need to use a valid email address or if i can just make up one since i dont need a password and username
>>> receiver = ['to#todomain.com']
#again, i dont know what to use for the receiver email address
>>> message = 'this is a test'
>>> s = smtplib.SMTP('localhost')
Traceback (most recent call last):
File "<pyshell#4>", line 1, in <module>
s = smtplib.SMTP('localhost')
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/smtplib.py", line 242, in __init__
(code, msg) = self.connect(host, port)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/smtplib.py", line 302, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/smtplib.py", line 277, in _get_socket
return socket.create_connection((port, host), timeout)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py", line 571, in create_connection
raise err
error: [Errno 61] Connection refused
the connection refused error is my problem. i've looked online but i can't figure out how to connect it.
If you want to use gmail to send your message there is some code at: http://www.nixtutor.com/linux/send-mail-through-gmail-with-python/ that you can use. It should be pretty self explaining..
Do you have a smtp server running in your computer? Localhost refers to your own computer.
Related
I have been trying to run the following code however I am getting the following error when the code runs. I am currently following a video on a course on Udemy, however, the video seems to be outdated as the option to turn on "Less secure apps" on Gmail can't be turned on anymore as Google disabled the option. To fix this, I have enabled 2-factor authentication and generated a new password for this python script. However, it is still not working and I don't know why. Please can somebody help?
import smtplib
from credentials import *
connection = smtplib.SMTP("smtp.gmail.com", port = 587)
connection = smtplib.SMTP("smptp.gmail.com", port = 587)
connection.starttls()
connection.login(user=my_email, password=password)
connection.sendmail(from_addr=my_email, to_addrs=to_address, msg = "Testing")
connection.close()
Error as follows:
Traceback (most recent call last):
File "C:\Users\user\Documents\Python\bootcamp 2022\day 32\Birthday+Wisher+(Day+32)+start\Birthday Wisher (Day 32) start\main.py", line 9, in <module>
connection = smtplib.SMTP("smptp.gmail.com" ,port =587)
File "C:\Python39\lib\smtplib.py", line 255, in __init__
(code, msg) = self.connect(host, port)
File "C:\Python39\lib\smtplib.py", line 341, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "C:\Python39\lib\smtplib.py", line 312, in _get_socket
return socket.create_connection((host, port), timeout,
File "C:\Python39\lib\socket.py", line 822, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
File "C:\Python39\lib\socket.py", line 953, in getaddrinfo
for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno 11001] getaddrinfo failed
getaddrinfo looks up the IP address for a domain name. The reason why this is failing is because you have a simple typo in the address for smtp.gmail.com. Just fix it and it should be fine. Like this:
import smtplib
from credentials import *
connection = smtplib.SMTP("smtp.gmail.com", port=587)
connection.starttls()
connection.login(user=my_email, password=password)
connection.sendmail(from_addr=my_email, to_addrs=to_address, msg = "Testing")
connection.close()
Also, there is no need to redefine connection. Doing it only once will work.
I tried using different ports and find solutions in the internet but I still can't fix it. I tried changing the localhost to 127.0.0.1 and also tried changing the port numbers to all types of values but still won't work. A great help would be greatly appreciated.
import tkinter
import smtplib
import socket
from email.parser import Parser
smtp = smtplib.SMTP
user = ""
password = ""
def connect():
print(msg_entry.get())
smtp = smtplib.SMTP("localhost", 587)
#smtp.login(user,password)
smtp.sendmail(from_entry.get(), to_entry.get(), msg_entry.get())
smtp.quit()
app = tkinter.Tk()
app.title("test")
to_label = tkinter.Label(app,text="To:")
to_entry = tkinter.Entry(app)
from_label = tkinter.Label(app,text="From:")
from_entry = tkinter.Entry(app)
send_button = tkinter.Button(app,text="send",command=connect)
msg_label = tkinter.Label(app,text="Email:")
msg_entry = tkinter.Entry(app,width=50)
#pack(add) the widget to the app.
to_label.pack()
to_entry.pack()
from_label.pack()
from_entry.pack()
msg_label.pack()
msg_entry.pack()
send_button.pack()
#draw the window, have this at the end
app.mainloop()
Whenever I would click the send, I would get the error:
Traceback (most recent call last):
File "C:\Users\ADMIN\AppData\Local\Programs\Python\Python38\lib\tkinter\__init__.py", line 1883, in __call__
return self.func(*args)
File "C:/Users/ADMIN/PycharmProjects/CourseOutcome3/EmailTransmitter-DAMPAC.py", line 15, in connect
smtp = smtplib.SMTP("localhost", 587)
File "C:\Users\ADMIN\AppData\Local\Programs\Python\Python38\lib\smtplib.py", line 253, in __init__
(code, msg) = self.connect(host, port)
File "C:\Users\ADMIN\AppData\Local\Programs\Python\Python38\lib\smtplib.py", line 339, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "C:\Users\ADMIN\AppData\Local\Programs\Python\Python38\lib\smtplib.py", line 308, in _get_socket
return socket.create_connection((host, port), timeout,
File "C:\Users\ADMIN\AppData\Local\Programs\Python\Python38\lib\socket.py", line 808, in create_connection
raise err
File "C:\Users\ADMIN\AppData\Local\Programs\Python\Python38\lib\socket.py", line 796, in create_connection
sock.connect(sa)
ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it
You need to have a server on localhost to make it work, i recommend PaperCut, also you may want to check this post of mine about how to create a basic smtp mail server.
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
I'm working on a guessing game where there are tons of characters and it gives you hints for you to guess the character. There is also going to be a thing where you can submit a character. I want to do it so that it will send me an e-mail if somebody submits a character, but it's not working. Here's my code:
import smtplib
FROM = 'Guess#Character.com'
TO = ["mikpe120#gmail.com"] # must be a list
SUBJECT = "Somebody submitted a character!"
TEXT = "Name: " + name + "NameType: " + nameType
# Prepare actual message
message = """\
From: %s
To: %s
Subject: %s
%s
""" % (FROM, ", ".join(TO), SUBJECT, TEXT)
# Send the mail
server = smtplib.SMTP('myserver')
server.sendmail(FROM, TO, message)
server.quit()
And here is the error that I got:
Traceback (most recent call last):
File "C:/Users/Mikolaj Perzyna/Desktop/addCharacter.py", line 106, in <module>
server = smtplib.SMTP('myserver')
File "C:\Users\Mikolaj Perzyna\AppData\Local\Programs\Python\Python36\lib\smtplib.py", line 251, in __init__
(code, msg) = self.connect(host, port)
File "C:\Users\Mikolaj Perzyna\AppData\Local\Programs\Python\Python36\lib\smtplib.py", line 335, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "C:\Users\Mikolaj Perzyna\AppData\Local\Programs\Python\Python36\lib\smtplib.py", line 306, in _get_socket
self.source_address)
File "C:\Users\Mikolaj Perzyna\AppData\Local\Programs\Python\Python36\lib\socket.py", line 704, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
File "C:\Users\Mikolaj Perzyna\AppData\Local\Programs\Python\Python36\lib\socket.py", line 743, in getaddrinfo
for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno 11001] getaddrinfo failed
I'm sorry if I'm formatting this post wrong, which I probably am, but this is my first question on the site.
getaddrinfo failed means that the socket connection smptlib.SMTP uses failed to resolve the hostname of the server. Try a different SMTP server or a different IP/hostname.
This line is almost certainly wrong.
server = smtplib.SMTP('myserver')
Is your SMTP server really called myserver? I'd expect to see something like mail.mydomain.com there.
I've been trying to connect to my Gmail account using python. imap is enabled.
import imaplib
imap_server = imaplib.IMAP4_SSL("imap.gmail.com",993)
# also tried imap_server = imaplib.IMAP4_SSL("imap.gmail.com"), doesnt work.
Traceback is :
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
imap_server = imaplib.IMAP4_SSL("imap.gmail.com",993)
File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/imaplib.py", line 1202, in __init__
IMAP4.__init__(self, host, port)
File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/imaplib.py", line 172, in __init__
self.open(host, port)
File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/imaplib.py", line 1217, in open
IMAP4.open(self, host, port)
File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/imaplib.py", line 248, in open
self.sock = self._create_socket()
File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/imaplib.py", line 1205, in _create_socket
sock = IMAP4._create_socket(self)
File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/imaplib.py", line 238, in _create_socket
return socket.create_connection((self.host, self.port))
File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/socket.py", line 435, in create_connection
raise err
File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/socket.py", line 426, in create_connection
sock.connect(sa)
OSError: [Errno 65] No route to host
What OSError: [Errno 65] No route to host means is what it say: you can't get to that machine from your machine.
You can test that from outside of Python by opening up a terminal/DOS prompt and typing this:
ping imap.gmail.com
It's possible that this is actually a name lookup error, and you're somehow getting a bad address for imap.gmail.com. So, just to be sure, check by IP address too:
ping 74.125.129.108
ping 74.125.129.109
If ping works, you can check whether your router is for some reason just blocking TCP access to the host, e.g., with:
telnet imap.gmail.com
If it's working, this should either hang for a long time, or give you a connection-refused error; if it gives you a no-route-to-host error, it's the same problem you're seeing.
It's also possible that your router is specifically blocking port 993. You can test this too:
telnet imap.gmail.com 993
If it doesn't come back with something like "Connected to gmail-imap.l.google.com", same problem here too.
At any rate, once you've verified that this is a system or network configuration problem, not a programming problem, go ask for help with your system on the appropriate site.