Reputation: 1407
I'm trying to send an email to an SMTP server but I keep getting errors from the server. It says my commands are unrecognized with status codes like 500 and 501.
Here is my script:
import socket
server_info = ("smtp.gmx.com", 25)
socket = socket.socket()
socket.connect(server_info)
username = raw_input("Enter your username: ")
password = raw_input("Enter your password: ")
recipient = raw_input("Recipient: ")
data = raw_input("Your message: ")
auth = username + "" + password
auth = auth.encode("base64").replace("\n", "")
socket.send("HELO\r\n")
print "EHLO Response: " + socket.recv(1024)
socket.send("AUTH PLAIN "+auth+"\r\n")
print "AUTH Response: " + socket.recv(1024)
socket.send("MAIL FROM:<"+username+">\r\n")
print "MAIL FROM Response: " + socket.recv(1024)
socket.send("RCPT TO:"+recipient+"\r\n")
print "RCPT TO Response: " + socket.recv(1024)
socket.send("RCPT TO:"+recipient+"\r\n")
print "RCPT TO Response: " + socket.recv(1024)
socket.send("DATA\r\n")
print "DATA Response: " + socket.recv(1024)
socket.send(data + "\r\n.\r\n")
print "RAW DATA Response: " + socket.recv(1024)
socket.send("QUIT\r\n")
print "QUIT Response: " + socket.recv(1024)
print "Done."
socket.close()
What is the problem with these command? I wrote them exactly like they should be. Here is the errors I get from the server:
http://i.gyazo.com/3f5eb3a34cbb0f00510281bccc8d0546.png
P.S I don't want to use smtplib. I would like to send my email manually, for learning purposes
Upvotes: 2
Views: 2521
Reputation: 3609
You're sending "HELO" to start your SMTP session while you should send "EHLO".
The SMTP authentication mechanism is only available if you're using extended SMTP. Starting your session with HELO tells the servers you're not using extended SMTP, hence it'll reject all authentication commands.
Upvotes: 0
Reputation: 500
You have a great module to send email messages - smtplib
Usage example:
import smtplib
SMTP_SERVER = 'smtp.gmail.com'
SMTP_PORT = 587
sender = '[email protected]'
password = "password"
recipient = '[email protected]'
subject = 'Gmail SMTP Test'
body = 'blah blah blah'
body = "" + body + ""
headers = ["From: " + sender,
"Subject: " + subject,
"To: " + recipient,
"MIME-Version: 1.0",
"Content-Type: text/html"]
headers = "\r\n".join(headers)
session = smtplib.SMTP(SMTP_SERVER, SMTP_PORT)
session.ehlo()
session.starttls()
session.ehlo()
session.login(sender, password)
session.sendmail(sender, recipient, headers + "\r\n\r\n" + body)
session.quit()
UPD: If you want to use SSL:
...
session = smtplib.SMTP_SSL_PORT(SMTP_SERVER, SMTP_PORT)
session.ehlo()
session.login(sender, password)
session.sendmail(sender, recipient, headers + "\r\n\r\n" + body)
session.quit()
Upvotes: 2