I am trying to write a python program which will send emails without using smtplib. I have tried to look other posts on this but could not find a solution.
`from socket import *
msg = "\r\n My email's content!"
endmsg = "\r\n.\r\n"
mailserver = ("smtp.aol.com", 25)
clientSocket = socket(AF_INET, SOCK_STREAM)
clientSocket.connect(mailserver)
recv = clientSocket.recv(1024)
recv = recv.decode()
print("Message after connection request:" + recv)
if recv[:3] != '220':
print('220 reply not received from server.')
heloCommand = 'HELO Alice\r\n'
clientSocket.send(heloCommand.encode())
recv1 = clientSocket.recv(1024)
recv1 = recv1.decode()
print("Message after HeLO command:" + recv1)
if recv1[:3] != '250':
print('250 reply not received from server.')
# #Info for username and password
# username = "myusername"
# password = "xxxxxxxx"
# authMsg = "AUTH LOGIN\r\n"
# clientSocket.send(authMsg.encode())
# recv_auth = clientSocket.recv(1024)
# print(recv_auth.decode())
# clientSocket.send(username.encode())
# clientSocket.send("\r\n".encode())
# recv_user = clientSocket.recv(1024)
# print("Response after sending username: "+recv_user.decode())
# clientSocket.send(password.encode())
# clientSocket.send("\r\n".encode())
# recv_pass = clientSocket.recv(1024)
# print("Response after sending password: "+recv_pass.decode())
# clientSocket.send(('starttls\r\n').encode())
# recv_tls = clientSocket.recv(1024)
# print(recv_tls.decode())
mailFrom = "MAIL FROM:<[email protected]>\r\n"
clientSocket.send(mailFrom.encode())
recv2 = clientSocket.recv(1024)
recv2 = recv2.decode()
print("After MAIL FROM command: "+recv2)
rcptTo = "RCPT TO:<[email protected]>\r\n"
clientSocket.send(rcptTo.encode())
recv3 = clientSocket.recv(1024)
recv3 = recv3.decode()
print("After RCPT TO command: "+recv3)
data = "DATA\r\n"
clientSocket.send(data.encode())
recv4 = clientSocket.recv(1024)
recv4 = recv4.decode()
print("After DATA command: "+recv4)
clientSocket.send(msg.encode())
clientSocket.send(endmsg.encode())
recv_msg = clientSocket.recv(1024)
print("Response after sending message body:"+recv_msg.decode())
quit = "QUIT\r\n"
clientSocket.send(quit.encode())
recv5 = clientSocket.recv(1024)
print(recv5.decode())
clientSocket.close()`
Is it possible to send an email without logging in to server? If not how do I login correctly? Every time I tried to log in I have received "authentication failed" error. I also have errors saying "Need MAIL command" and "Need RCPT command" even though I am sending MAIL FROM and RCPT TO commands.
Simply copy & paste the libraries below into your application to start sending! The SocketLabs Email Delivery Python library allows you to easily send any type of email message supported by our Injection API, from a simple message to a single recipient, to a complex bulk message sent to multiple recipients.
Python provides smtplib module, which defines an SMTP client session object that can be used to send mail to any Internet machine with an SMTP or ESMTP listener daemon.
I was able to send e-mail using smtp.gmx.com
from socket import *
import base64
import time
msg = "\r\n I love computer networks!"
endmsg = "\r\n.\r\n"
mailserver = ("smtp.gmx.com", 25) #Fill in start #Fill in end
clientSocket = socket(AF_INET, SOCK_STREAM)
clientSocket.connect(mailserver)
recv = clientSocket.recv(1024)
recv = recv.decode()
print("Message after connection request:" + recv)
if recv[:3] != '220':
print('220 reply not received from server.')
heloCommand = 'EHLO Alice\r\n'
clientSocket.send(heloCommand.encode())
recv1 = clientSocket.recv(1024)
recv1 = recv1.decode()
print("Message after EHLO command:" + recv1)
if recv1[:3] != '250':
print('250 reply not received from server.')
#Info for username and password
username = "xxxxxx"
password = "xxxxxx"
base64_str = ("\x00"+username+"\x00"+password).encode()
base64_str = base64.b64encode(base64_str)
authMsg = "AUTH PLAIN ".encode()+base64_str+"\r\n".encode()
clientSocket.send(authMsg)
recv_auth = clientSocket.recv(1024)
print(recv_auth.decode())
mailFrom = "MAIL FROM:<xxxxxxxx>\r\n"
clientSocket.send(mailFrom.encode())
recv2 = clientSocket.recv(1024)
recv2 = recv2.decode()
print("After MAIL FROM command: "+recv2)
rcptTo = "RCPT TO:<xxxxxxxxxx>\r\n"
clientSocket.send(rcptTo.encode())
recv3 = clientSocket.recv(1024)
recv3 = recv3.decode()
print("After RCPT TO command: "+recv3)
data = "DATA\r\n"
clientSocket.send(data.encode())
recv4 = clientSocket.recv(1024)
recv4 = recv4.decode()
print("After DATA command: "+recv4)
subject = "Subject: testing my client\r\n\r\n"
clientSocket.send(subject.encode())
date = time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.gmtime())
date = date + "\r\n\r\n"
clientSocket.send(date.encode())
clientSocket.send(msg.encode())
clientSocket.send(endmsg.encode())
recv_msg = clientSocket.recv(1024)
print("Response after sending message body:"+recv_msg.decode())
quit = "QUIT\r\n"
clientSocket.send(quit.encode())
recv5 = clientSocket.recv(1024)
print(recv5.decode())
clientSocket.close()
The only problem is that gmail rejects emails, but some other email servers do receive. Now I am trying to send email from smtp.gmail.com which requires SSL or TLS. Any suggestions/help on that?
Is it possible to send an email without logging in to server?
It depends on the server. smtp.aol.com is the server for customers of AOL mail and this server needs you to log in. The MX for AOL (dig mx aol.com
) does not need you to login since it is used for communication between MTA but it has other restrictions in order to fight spam.
If not how do I login correctly?
First you should better use EHLO not HELO and then you would also see which authentication methods are supported by the server.
250-AUTH PLAIN LOGIN XAOL-UAS-MB XOAUTH2
For an introduction into PLAIN and LOGIN authentication (and others) see http://www.samlogic.net/articles/smtp-commands-reference-auth.htm. In short: for PLAIN you have to provide base64(username+"\0"+password)
.
I also have errors saying "Need MAIL command" and "Need RCPT command" even though I am sending MAIL FROM and RCPT TO commands.
You provide no debug output but I guess that the server is rejecting your MAIL FROM and RCPT TO commands because you are not authorized.
You might need to enable IMAP in order to use smtp.gmail.com. I just did that and it works perfectly. You have to use either port 587 or 465. To enable IMAP click on the gear icon in your gmail account, then click Forwarding and POP/IMAP. Finally click enable IMAP and save changes. This should work.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With