I am trying to manually send email by opening TCP connection with SMTP server endpoint of Mailgun using python socket. “QUIT” command does not seem to be working, how to fix it? Although, email does get send successfully with the python script below.
I checked if SMTP server accepts “QUIT” command using netcat in terminal, it seems it does based on exchange below. C:
denotes my instruction and S:
denotes response from server.
Messages exchanged using terminal:
C: nc smtp.mailgun.org 587
S: 220 Mailgun Influx ready
C: QUIT
S: 221 See you later. Yours truly, Mailgun
Messages exchanged with python script below:
S: 220 Mailgun Influx ready
C: STARTTLS
S: 220 Go ahead
C: EHLO example.com
S: 250-f71ae9659782
250-AUTH PLAIN LOGIN
250-SIZE 52428800
250-8BITMIME
250-SMTPUTF8
250 PIPELINING
C: AUTH LOGIN
S: 334 VXNlcm5hbWU6
C: <encoded username>
S: 334 UGFzc3dvcmQ6
C: <encoded password>
S: 235 Authentication successful
C: MAIL FROM: <[email protected]>
S: 250 Sender address accepted
C: RCPT TO: <[email protected]>
S: 250 Recipient address accepted
C: DATA
S: 354 Continue
C: From: [email protected]
C: To: [email protected]
C: Subject: Test Email from python client
C:
C: I love computer networks!
C:
.
S: 250 Great success
C: QUIT
S: 502 Command not implemented
Python script:
import os
from dotenv import load_dotenv
from socket import *
import ssl
import base64
import sys
load_dotenv()
def sendMessage(command, code, clientSocket):
command = command + "rn"
print("C:", command)
clientSocket.send(command.encode())
if code != "skip":
recv = clientSocket.recv(1024).decode()
print("S:", recv)
if recv[:3] != code:
print(f'{code} reply not received from server.')
clientSocket.close()
sys.exit()
msg = "I love computer networks!"
endmsg = "rn.rn"
mailfrom = os.environ.get("MAIL_FROM")
mailto = os.environ.get("MAIL_TO")
mailserver = os.environ.get("MAILSERVER")
smtpUsername = base64.b64encode(os.environ.get("SMTP_USERNAME").encode()).decode()
smtpPassword = base64.b64encode(os.environ.get("SMTP_PASSWORD").encode()).decode()
# Create socket called clientSocket and establish a TCP connection with mailserver
clientSocket = socket(AF_INET, SOCK_STREAM)
clientSocket.connect((mailserver, 587))
recv = clientSocket.recv(1024).decode()
print("S:", recv)
if recv[:3] != '220':
print('220 reply not received from server.')
clientSocket.close()
#Send TLS command and print server response.
sendMessage("STARTTLS", "220", clientSocket)
#Upgrade to SSL
ssl_context = ssl.create_default_context(cafile="/etc/ssl/cert.pem")
ssl_context.check_hostname = True
ssl_context.verify_mode = ssl.CERT_REQUIRED
clientSocket = ssl_context.wrap_socket(clientSocket, server_hostname=mailserver)
#exchanges messages with SMTP server
sendMessage("EHLO example.com", "250", clientSocket) #SMTP handshake Hello command
sendMessage("AUTH LOGIN", "334", clientSocket) #asking auth login
sendMessage(smtpUsername, "334", clientSocket)
sendMessage(smtpPassword, "235", clientSocket)
sendMessage(f"MAIL FROM: <{mailfrom}>", "250", clientSocket)
sendMessage(f"RCPT TO: <{mailto}>", "250", clientSocket)
sendMessage("DATA", "354", clientSocket)
sendMessage(f"From: {mailfrom}", "skip", clientSocket)
sendMessage(f"To: {mailto}", "skip", clientSocket)
sendMessage("Subject: Test Email from python client", "skip", clientSocket)
sendMessage("", "skip", clientSocket)
sendMessage(msg, "skip", clientSocket)
sendMessage(endmsg, "250", clientSocket)
sendMessage("QUIT", "221", clientSocket)