www.bimwook.com
Sending Mail (SMTP)
( 2012-12-29 19:41:53 )
 

  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
  
  http://ds.internic.net/ds/dspg0intdoc.html
  
  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:
  
  
  HELO
  -- 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. cf.ac.uk.


  MAIL
  -- Indicates who is sending the mail. For example,
  MAIL FROM: <dave@cs.cf.ac.uk>.
  
  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.
  

  RCPT
  -- Indicates who is recieving the mail. For example,
  RCPT TO: <user@email.com>. You can indicate more than one user by issuing multiple RCPT commands.
  
  
  DATA
  -- 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."
  

  QUIT
  -- Indicates that the conversation is over.
  

  EXPN
  -- Indicates that you are using a mailing list.


  HELP
  -- Asks for help from the mail server.


  NOOP
  -- Does nothing other than get a reponse from the mail server. RSETAborts the current conversation.


  SEND
  -- Sends a message to a user's terminal instead of a mailbox.


  SAML
  -- Sends a message to a user's terminal and to a user's mailbox.


  SOML
  -- Sends a message to a user's terminal if they are logged on; otherwise, sends the message to the user's mailbox.


  TURN
  -- 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.


  VRFY
  -- 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
  
  or
  
  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!!)
  
  
  211 -- A system status or help reply.
  214 -- Help Message.
  220 -- The server is ready.
  221 -- The server is ending the conversation.
  250 -- The requested action was completed.
  251 -- The specified user is not local, but the server will forward the mail message.
  354 -- 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."
  421 -- The mail server will be shut down. Save the mail message and try again later.
  450 -- The mailbox that you are trying to reach is busy. Wait a little while and try again.
  451 -- The requested action was not done. Some error occurmiles in the mail server.
  452 -- The requested action was not done. The mail server ran out of system storage.
  500 -- The last command contained a syntax error or the command line was too long.
  501 -- The parameters or arguments in the last command contained a syntax error.
  502 -- The mail server has not implemented the last command.
  503 -- The last command was sent out of sequence. For example, you might have sent DATA before sending RECV.
  504 -- One of the parameters of the last command has not been implemented by the server.
  550 -- The mailbox that you are trying to reach can't be found or you don't have access rights.
  551 -- The specified user is not local; part of the text of the message will contain a forwarding address.
  552 -- 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.
  553 -- The mail address that you specified was not syntactically correct.
  554 -- 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.
  
  
  >HELO
  <250 sentinel.cs.cf.ac.uk Hello dave@cs.cf.ac.uk [X.X.X.X],pleased to meet you
  
  
  >MAIL From: <(Ralph Martin)>
  <250 <(Ralph Martin)>... Sender ok
  
  
  >RCPT To: <dave@cs.cf.ac.uk>
  <250 <dave@cs.cf.ac.uk>... Recipient ok
  
  
  >DATA
  <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
  
  
  >QUIT
  <221 sentinel.cs.cf.ac.uk 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.