cl = xmpp.Client('myserver.com')
if not cl.connect(server=('mysefver.com',5223)):
raise IOError('cannot connect to server')
cl.RegisterHandler('message',messageHandler)
cl.auth('myemail#myserver.com', 'mypassword', 'statusbot')
cl.sendInitPresence()
msgtext = formatToDo(cal, 'text')
message = xmpp.Message('anotheremail#myserver.com', msgtext)
message.setAttr('type', 'chat')
cl.send(message)
I get the following error message when I try to run it:
xmpp.protocol.InvalidFrom: (u'invalid-from', '')
Why is this happening :(
From the XMPP protocol specification:
If the value of the 'from'
address does not match the hostname represented by the Receiving
Server when opening the TCP connection (or any validated domain
thereof, such as a validated subdomain of the Receiving Server's
hostname or another validated domain hosted by the Receiving Server),
then the Authoritative Server MUST generate an stream
error condition and terminate both the XML stream and the underlying
TCP connection.
which basically means, that if the sender is not recognized by the xmpp-server, it'll reply with this message. XMPP supplies a registration mechanism: xmpp.features.register
Related
I have written a code in jython 2.5, which uses the smtplib sendmail to send mail over a smtp server. Below is a simple snippet of the code
mail = MIMEMultipart('mixed')
mail['Subject'] = mail_subject
mail['FROM']=UstrSender
mail['To']=UstrReceivers
mail['Cc']=UstrCC
mail_p2=MIMEText(mail_html, 'html', 'utf-8')
mail.attach(mail_p2)
#Connection to SMTP
#Enter SMTP Server Details, In case your server do require authentication modify authentication parameter below and uncomment
s = smtplib.SMTP(smtpserver)
#s.sendmail(UstrSender, [UstrReceivers, UstrCC], mail.as_string())
sendmail_return = s.sendmail(UstrSender, [UstrReceivers, UstrCC], mail.as_string())
Now, the smtp server mentioned is a cluster of 3 individual servers and there is a lag at times on one of these servers because of a long queue of requests. To identify such issues and the culprit server, need to have a generic script to identify the queue id of the message being sent. Kindly, help on the same, if using docmd we can create such a command to get the queue id of the submitted email.
I tried getting a response from sendmail itself, but since the mail is eventually sent, there is no return from the command.
Thanks,
Dev
I have some code that hosts a local server and when a user connects it will send them some html code, which works fine.
But I want it so if they connect to http://localhost:90/abc it will show something different. How can I get the exact url they connected to?
Here is my code:
import socket
sock = socket.socket()
sock.bind(('', 90))
sock.listen(5)
print("Listening...")
while True:
client, address = sock.accept()
print("Connection recieved: ", address)
print(The exact url they connected to.)
print()
client.send(b'HTTP/1.0 200 OK\r\n')
client.send(b"Content-Type: text/html\r\n\r\n")
client.send(b'<html><body><h1>Hello, User!</body></html>')
client.close()
sock.close()
I tried print(client.getpeername()[1]), but that gets the client ip, and if there is a similar way to get the ip they connected to it probably wont get the 'abc' part of the url.
Thanks in advance.
Socket's don't have a notion of URL, that's specific to the HTTP protocol which runs on top of a socket. For this reason, only part of the HTTP URL is even used in the creation of a socket.
|--1---|----2----|-3-|--4-|
http:// localhost :90 /abc
Specifies which protocol inside of TCP the URL uses
Specifies the remote host, either by IP address or hostname
Specifies the remote port and is optional
Specifies the path of the URL
Only parts 2 and 3 are actually known to a TCP socket though! This is because TCP is a very basic form of communication, HTTP adds a bunch of functionality on top of it like requests and responses and paths and so on.
Basically if you're implementing an HTTP server, knowing the /abc part is your job. Take a look at this example. The client actually sends the /abc part to the server, otherwise it has no way of knowing which path the request is for.
When the client connects to your server, it will send:
GET /abc HTTP/1.1
Host: localhost
more headers...
<blank line>
Your server needs to parse the GET line and extract /abc from that.
I'm writing a script to grab the link from an email that my web application sends out to then start off a set of automated tests. At the minute my code should only grab the body of the email, I haven't started to scan for the link within that yet because I get no return.
import imaplib
mail = imaplib.IMAP4_SSL('imap.gmail.com')
mail.login('user', 'passwd')
mail.list()
mail.select("inbox")
result, data = mail.search(None, "ALL")
ids = data[0]
id_list = ids.split()
latest_email_id = id_list[-1]
result, data = mail.fetch(latest_email_id, "(RFC822)")
raw_email = data[0][1]
print raw_email
I don't see anything wrong in my code for getting the newest email (top of the inbox) and my IMAP is set up for the account. Can anyone enlighten me as to why I'm getting the error:
socket.error: [Errno 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond
Thanks in advance.
Edit: Company firewall is blocking port 143 and 993 (default IMAP(S) port), I've posted my workaround as the answer. There is nothing wrong with my code!
To get around the company port restrictions I used the netsh command to port forward an open port to port 993 (default for imaplib's SSL) using the following command in terminal
netsh interface portproxy add v4tov4 listenport=UNBLOCKTHISPORT listenaddress=YOURIP connectport=THISPORTISOPEN connectaddress=YOURIP
I am attempting to connect to the Gmail SMTP mail server and perform tasks as outlined by the skeleton code given to me. Only the use of sockets is allowed (so not the smtplib). I need to: send HELO command, MAIL FROM, RCPT TO, and DATA.
There are many cases of similar problems posted, but they haven't received the proper answer. For example:
Implementing Transport Layer Security in Python - Simple Mail Client
The program is required to connect to smtp.gmail.com over port 587. I've taken two different approaches:
Using STARTTLS:
mailserver = 'smtp.gmail.com'
clientSocket = socket(AF_INET, SOCK_STREAM)
clientSocket.connect((mailserver, 587))
recv = clientSocket.recv(1024)
print recv
if recv[:3] != '220':
print '220 reply not received from server.'
#Send HELO command and print server response
heloCommand = 'HELO Alice\r\n'
clientSocket.send(heloCommand)
recv1 = clientSocket.recv(1024)
print recv1
if recv1[:3] != '250':
print '250 reply not received from server.'
#Send MAIL FROM command and print server response.
command = "STARTTLS\r\n"
clientSocket.send(command)
recvdiscard = clientSocket.recv(1024)
print recvdiscard
clientSocket.send("MAIL From: email\r\n")
recv2 = clientSocket.recv(1024)
print recv2
if recv2[:3] != '250':
print '250 reply not received from server.'
Using SSL:
clientSocketSSL = ssl.wrap_socket(clientSocket)
Then clientSocketSSL replaces all instances of clientSocket. The STARTTLS lines are also removed and import ssl is added to the top.
When using the first method, the MAIL FROM: command isn't returning anything. I'm getting the following output:
250 mx.google.com at your service
220 2.0.0 Ready to start TLS
250 reply not received from server.
When using SSL, I'm getting the same as the linked post:
ssl.SSLError: [Errno 1] _ssl.c:504: error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol
Am I missing something here? I guess my best option is to use TLS but I have no idea how to go about that... is there something wrong with my MAIL FROM command?
When using SSL, you need to connect to port 465 instead of port 587. If you use STARTTLS, you still need to use ssl.wrap_socket, you just do it later - specifically, after receiving the 220 response to the STARTTLS command. After doing STARTTLS, you're supposed to do HELO again, since the server is supposed to forget anything that happened before the STARTTLS.
In either case, the servers at smtp.google.com ports 465 and 587 still won't return a 250 response to the MAIL command, since they require that you are authenticated before you send mail. You'll get a 530 response instead. You'll need to use the AUTH command with your gmail.com credentials to authenticate before you can use MAIL successfully on those servers.
If you don't want to authenticate, and depending on the details of what you need to do, you could try using port 25 of the server found in gmail.com's MX record. At the moment, the server is gmail-smtp-in.l.google.com and supports STARTTLS.
After STARTTLS, call
clientSocket = ssl.wrap_socket(clientSocket)
I have some code trying to receive an email sent from a server on a client. The email is definitively sent from the server to the client, and a SMTP server on the client should be able to receive this email. Here is my test implementation:
# define the SMTP server (with the real IP adress of the client of course)
server = smtpd.PureProxy(('XXX.XXX.XXX.XXX', 25), None)
inputs = [server]
outputs = []
message_queues = {}
readable, writable, exceptional = select.select(inputs, outputs, inputs)
# Only one socket in the list returned (there is exactly one)
socket = readable[0]
# Accept the connection or get it or whatever
connection, client_address = socket.accept()
# get the data
data = connection.recv(1024)
print data
After a considerably long time some data is received, which in no way resembles the content of the email. It is always
EHLO YYY.YYY.YYY.YYY
with the YYY the address of the server. I am no expert in SMTP and sockets, but what am I doing wrong to correctly receive the emai and its contents?
Thanks
Alex
The EHLO is part of the SMTP protocol exchange and it represents the client sending its greeting to your server which doesn't respond properly (because it doesn't respond at all). When the client gets tired of waiting for "a considerably long time" the session times out and your server shows what it received.
You seem to be confused as to which process is the server. The smtpd module creates servers or Mail Transport Agents, not clients. As noted in the smtpd documentation for SMTPServer:
Create a new SMTPServer object, which binds to local address
localaddr. It will treat remoteaddr as an upstream SMTP relayer. It
inherits from asyncore.dispatcher, and so will insert itself into
asyncore‘s event loop on instantiation.
You also seem to have the sense of localaddr and remoteaddr confused. The localaddr is not (as your comment claims) the address of the client, but where that server should accept connections from. You might want to try in place of your code:
server = smtpd.DebuggingServer(('localhost', 2525), None)
asyncore.loop()
Which can be tested with client code (in a separate process) of:
client smtplib.SMTP('localhost', 2525)
client.sendmail('from', 'to', 'body')
Finally, having a PureProxy with a remoteaddr of None, it if works at all, would proxy mail into nowhere which is probably not what you want in a proxy.
That is the proper start of the ESMTP protocol dialog. Your program needs to understand and handle at least the basic SMTP verbs; see RFC5321.