Linux
Apache
MySQL
PHP

CSS
XHTML1.1
XML/RSS

Creative Commons

2007-07-25 14:33:39

Email from the Command Prompt

This is something I've known how to do for quite some time, but I thought it would be beneficial to others if I documented it (and myself for when I forget it). Most people think that email is some magical thing that happens when and only when you visit Hotmail.com. It's kind of like people saying, "The internet is gone," because they think that the entire internet is confined to the little blue E on their desktop that happens to be missing.
Anyway, I'll start from the beginning. If you already know how email works, you can skip to the actual commands near the bottom.
There are computers, servers to be exact, connected to the internet that handle email. Your ISP has one (hopefully more than one), your school has one, and if you have a website hosted with some company they have one too. Email wouldn't exist without these servers, it wouldn't be possible. But what about Hotmail and Yahoo? They have these servers too. They just also have a nice website that lets you use their servers.
Now these servers need some way of accepting input from humans so that they can send that input, in the form of an email, to another human (or robot). This "way" is a service called the Simple Mail Transport Protocol (SMTP). SMTP is a program that is installed on a server and listens on the network for data being sent to it. When it gets data, it knows how to package it into a format that other email servers can read and unltimately, email programs can produce in a human readable form. That's really all there is to it. You don't need any special program to send an email (or "an internet" as Senator Tubes would say). If you know how to format your data so that the SMTP server can understand it, you can send emails without using Hotmail, Outlook, or any other email program.
I said before that the SMTP server listens on the network for data. More specifically, it listens on the server's port 25. To make this easy, think of the server as your local post office. It has multiple boxes so that it can separate different things for different people. In the computer world, port 25 is a box that's used for emails, just like mailbox 25 at the post office would be used for Joe Schmoe. So the server knows that when any data comes to port 25 (it's "email box") it's an email and not any other type of data, such as a web page or your local time coming from the atomic clock.
Hopefully that was easy enough to understand. But how do we send our email server data to it's port 25? The "telnet" command is how. Let's look at an easy example that sends the words "It worked!" in an email to "you@yourcompany.com".
> telnet emailserver.mycompany.com 25 helo mail from: me@mycompany.com rcpt to: you@yourcompany.com data It worked! .
So what just happened? A lot, actually. First, from a command prompt, we used "telnet" to connect to our email server on port 25. Now we need to know what the SMTP server expects to see in order to send our email. First, it needs to start our transmission, which is cleverly done by saying hello, or helo as it wants it to be. Once we've said hi we tell it what we want to do. We want to send mail from me to the recipient, you. Next we tell it everything coming next is the body of the email, or data. Once we've typed the contents of our email, we enter a dot on a new line and hit ENTER to signify that this is the end.
Pretty simple, huh? The above example doesn't happen too often, because most email servers require you to login before sending email. They do this because they don't want spammers to be able to use their email server to send bad emails. They want to make sure you're actually allowed to send mail using their server. So how do we give the server our username and password so we can send mail?
> telnet emailserver.mycompany.com 25 helo auth login dXNlcm5hbWU= cGFzc3dvcmQ= mail from: me@mycompany.com rcpt to: you@yourcompany.com data It worked! .
Note all we had to was tell it in the beginning that we were going to supply our login information and then supply it. But what's all that garbled junk? Well, email servers transmit this data in plain text over the network, which means that any bad guy listening in on your transmission could steal your username and password. So the email servers are smart and require your username and password to be encoded, base64 specifically. So the above sends the username "username" and the password "password". If you can't do base64 conversions in your head, download the program I wrote to do it for you.
So we can now send emails whether the server requires authentication or not. But if you do, you'll notice that the emails are rather plain. There is no subject and the "From" field shows the email address (me@mycompany.com) instead of the prettier human name (Kevin J. Slonka). How do we set these other options? I'm glad you asked.
> telnet emailserver.mycompany.com 25 helo auth login dXNlcm5hbWU= cGFzc3dvcmQ= mail from: me@mycompany.com rcpt to: you@yourcompany.com data Subject: This is my email From: "Kevin J. Slonka" To: It worked, and it's pretty! .
We simply added some stuff to the "data" section. There are certain keywords, such as Subject:, From:, and To: that, when they are found at the beginning of the "data" section, set certain properties of the email. Instead of the body of your email showing all of that stuff, it goes where it is supposed to go. The Subject: line goes into the email's subject, the From: line makes it look like it's really coming from Kevin J. Slonka, and the To: line sets the recipient of the email. You're probably wondering why you had to enter the recipient's email address twice. If you don't add the extra "data" stuff, you only need to enter it on the "rcpt" line, but once you start entering the extra "data" you need to also enter it there. Stupid, yes, but necessary.
So there, you now know how to send email without using some silly email program.

Back

1 comments


2007-07-25 20:20:29


Joe says...
Thanks for giving out my mailbox number!!

Post a comment!

Name:
Comment: