- - - ----------------------------------------------------- - - -
Subject: Using sockets for sending mail
Author: kHz, written for clanX scripterz
Date: March 6, 2001
- - - ----------------------------------------------------- - - -

In this article, I am going to explain or just try to give you some idea about using sockets to send mail.
You can see according to the scroll bar that this article is not that long, I tried to write "short and focused"
with no extra shit and only things you really should know, so you won't get confused by extras.
First, before reading, to understand what is going on this article you must be an expert with writing scripts,
and it is recommended that you will have a bit experience with sockets, or just the basics of it.
So let's start...

First, we have to open a connection to a SMTP (SIMPLE MAIL TRASFER PROTOCOL) server.
To open the connection we use /sockopen , a quick syntx for the sockopen command: /sockopen name address port
The name of our socket will be mail, and the address would be smtp.matav.net.il (an exmaple for a smtp server address)
SMTP servers listen to incoming connections on port 25, so we will use port 25 in the sockopen command,
All in all, the command would be:
/sockopen mail smtp.matav.net.il 25
mIRC will open a socket to this address under port 25 and will wait for the smtp server to response...

Once the connection was established, a greet from the server will be sent to the client, something like that:
220 andrew.nonstop.net.il ESMTP server ready Tue, 6 Mar 2001 23:05:37 +0200
So you know the socket is now opened.
Some servers require to be "HELO'ED", so we will just use
on *:sockopen:mail:{ sockwrite -n $sockname HELO }
(the missing L is not a mistake)
which will sent the HELO command to the server like he wants ;)

Now we want to send the mail, this stage requires some commands:
MAIL FROM: <your emailaddress>
RCPT TO: <the address we send to>

Writing a mail exmaple:
/sockwrite -n mail MAIL FROM: idansh@matav.net.il
will appear in the "from:" field in the email client (here you usually type your email address)
/sockwrite -n mail RCPT TO: blah@test.com
will tell the server where to send the mail (here you type who you want to send the mail to)
/sockwrite -n mail DATA
will tell the server that everything you write from now is the message itself
after typing the data command:
/sockwrite -n mail TEXT...
your mail lines... (you replace TEXT.. with your message)
your mail lines2 (same here)
your mail lines 3 (same here)
after finishing:

/sockwrite -n mail .
will tell the server to send the mail

Now, here's an exmaple for the whole thing:

/sockwrite -n mail MAIL FROM: idansh@matav.net.il
/sockwrite -n mail RCPT TO: blah@test.com
/sockwrite -n mail DATA
/sockwrite -n mail Hi
/sockwrite -n mail How are you?
/sockwrite -n mail This is a test... doh
/sockwrite -n mail well, must go
/sockwrite -n mail bye!
/sockwrite -n mail .

when you send the "." , it ends it all and sends the mail, a typical response after sending:
250 Message received: 20010306211818.BFZM20788.andrew@[]

After finished: you can use the /sockclose command to close the connection: /sockclose mail
But you can also tell the server to disconnect you with using the QUIT command
/sockwrite -n mail QUIT

Below I wrote an exmaple script for aliases and remotes combination to make things work together:

;Exmaple script by kHz
;Use /mailcon and watch the status window
;everything from now goes to the a new remote file:

alias mailcon {
set %mail.to $$?="enter address to send the mail to"
set %mail.from $?="enter your address"
set %mail.subject $?="enter the mail subject"
set %mail.smtp $?="enter the SMTP server address"
;sets the required details to send a mail in general
if (%mail.to) && (%mail.from) && (%mail.subject) && (%mail.smtp) {
  ;verfying that all the variables *are something*
  sockopen mail %mail.smtp 25
  ;opening the socket to the specified smtp server
  echo -s * Connecting to %mail.smtp on port 25...
on *:sockopen:mail:{
echo -s * Mail socket has been opened!
;echoes when the socket was opened
if ($sockerr > 0) { return }
;checks if the erros are bigger then 0, if so aborts, otherwise:
else {
  sockwrite -n mail MAIL FROM: %mail.from
  ;telling the server what is our email address
  sockwrite -n mail RCPT TO: %mail.to
  ;telling the server the address to send the mail
  sockwrite -n mail DATA
  ;telling the server everything from now on belongs to the mail body
  sockwrite -n mail SUBJECT: %mail.subject
  ;writes the subject we specified
  sockwrite -n mail Hi
  sockwrite -n mail Is everything okay there?
  sockwrite -n mail This is a test... duh
  sockwrite -n mail well, gotta go, BYE
  ;writes the mail message *itself*
  sockwrite -n mail .
  ;finishing by the dot and by that telling the server to send the email
  echo -s * Message sent, closing mail socket...
  .timer 1 1 sockwrite -n mail QUIT
  ;timering a quit command so the server will disconnect us from him
;reads the data, and echoes it to the status window
on *:sockread:mail:{ sockread %temp | echo -s * %temp }
;echoes that the socket was closed and unsets the detail variables
on *:sockclose:mail:{ echo -s * Mail socket closed. | unset %mail.* }

;end of Exmaple script

Other SMTP commands and information can be found on:

I hope I helped you out, even a bit to figure out how this works, and how to use it.
If you have any questions/commets/anything you wanna ask/tell me, mail me by clicking my nickname below
Best Regards,