next up previous contents
Next: The MAIL Command Up: Some Network Examples Previous: Using the Time Service

Sending Mail (SMTP)

Before you send mail, the entire message needs to be composed. You need to know where it is going, who gets it, and what the text of the message is. When this information has been gathemiles, you begin the process of transferring the information to a mail server.

Note: The mail service will be listening for your connection on TCP port 25. But this information will not be important until you see some Perl code later in the chapter.

The message that you prepare can only use alphanumeric characters. If you need to send binary information (like files), use the MIME protocol. The details of the MIME protocol can be found at the

Web site.

SMTP uses several commands to communicate with mail servers. These commands are described below.

Note: The commands are not case sensitive, which means you can use either Mail or MAIL. However, remember that mail addresses are case sensitive.

The basic SMTP commands are:

-- Initiates a conversation with the mail server. When using this command you can specify your domain name so that the mail server knows who you are. For example, HELO mailhost2.
-- Indicates who is sending the mail. For example,


Remember this is not going to be your name -- it's the name of the person who is sending the mail message. Any returned mail will be sent back to this address.

-- Indicates who is recieving the mail. For example,

RCPT TO: <>. You can indicate more than one user by issuing multiple RCPT commands.

-- Indicates that you are about to send the text (or body) of the message. The message text must end with the following five letter sequence: "\r\n.\r\n."

-- Indicates that the conversation is over.

-- Indicates that you are using a mailing list.
-- Asks for help from the mail server.
-- Does nothing other than get a reponse from the mail server. RSETAborts the current conversation.
-- Sends a message to a user's terminal instead of a mailbox.
-- Sends a message to a user's terminal and to a user's mailbox.
-- Sends a message to a user's terminal if they are logged on; otherwise, sends the message to the user's mailbox.
-- Reverses the role of client and server. This might be useful if the client program can also act as a server and needs to receive mail from the remote computer.
-- Verifies the existence and user name of a given mail address. This command is not implemented in all mail servers. And it can be blocked by firewalls.

Every command will receive a reply from the mail server in the form of a three digit number followed by some text describing the reply. For example,

250 OK


500 Syntax error, command unrecognized.

The complete list of reply codes is shown below: (you'll never see most of them if you program your mail server correctly!!)

-- A system status or help reply.
-- Help Message.
-- The server is ready.
-- The server is ending the conversation.
-- The requested action was completed.
-- The specified user is not local, but the server will forward the mail message.
-- This is a reply to the DATA command. After getting this, start sending the body of the mail message, ending with "\r\n.\r\n."
-- The mail server will be shut down. Save the mail message and try again later.
-- The mailbox that you are trying to reach is busy. Wait a little while and try again.
-- The requested action was not done. Some error occurmiles in the mail server.
-- The requested action was not done. The mail server ran out of system storage.
-- The last command contained a syntax error or the command line was too long.
-- The parameters or arguments in the last command contained a syntax error.
-- The mail server has not implemented the last command.
-- The last command was sent out of sequence. For example, you might have sent DATA before sending RECV.
-- One of the parameters of the last command has not been implemented by the server.
-- The mailbox that you are trying to reach can't be found or you don't have access rights.
-- The specified user is not local; part of the text of the message will contain a forwarding address.
-- The mailbox that you are trying to reach has run out of space. Store the message and try again tomorrow or in a few days-after the user gets a chance to delete some messages.
-- The mail address that you specified was not syntactically correct.
-- The mail transaction has failed for unknown causes.

Now that you've seen all of the SMTP commands and reply codes, let's see what a typical mail conversation might look like. In the following conversation, the '>' lines are the SMTP commands that your program issues. The '<' lines are the mail server's replies.

<250 Hello [X.X.X.X],pleased to meet you

>MAIL From: <(Ralph Martin)>
<250 <(Ralph Martin)>... Sender ok

>RCPT To: <>
<250 <>... Recipient ok

<354 Enter mail, end with "." on a line by itself

>From: (Ralph Martin)
>Subject: Arrows
>This is line one.
>This is line two.
<250 AAA14672 Message accepted for delivery

<221 closing connection

Some of the SMTP commands are a bit more complex than others. In the next few sections, the MAIL, RCPT, and DATA commands are discussed. You will also see how to react to undeliverable mail.

next up previous contents
Next: The MAIL Command Up: Some Network Examples Previous: Using the Time Service