SMTP Headers
These are easy. The ones we're going to concern ourselves with are To:, From:, Subject:, Cc: and Bcc:. (Note that they're not supposed to be case-sensitive, but some systems will complain, so type them like I have here.)
Here's an example:
To: "John Doe" <johndoe@codecall.net> From: "Tal Fulano" <tfulano@codecall.net> Cc: jordan@codecall.net Bcc: whoknows@codecall.net Subject: I can use sendmail now!
You can put these headers in any order you like, and use as many or as few of them in any combination. The only required one is To: for obvious reasons. When putting your emails into the fields, you can choose to use a display name plus the email address, or just the email address itself. When using both, be sure to put the name in double quotes and the email address in angle brackets. If you are only using the email address, you don't need the brackets. When sending to multiple addresses, separate emails by a single semicolon and a space:
"Mike K." <mikek@yourdomain.com>; someone@college.edu
The last email in a list should not have a semicolon after it. Another thing - the From: field can only have one address, again for obvious reasons.
I'm sure by now you've noticed that you can put anything in the From: field. Yes, unfortunately. This is how spammers work. I myself have abused this to prank my mom with an elaborately designed email that appeared to be a cease-and-desist order from the US Copyright Office. She fell for it, too.
MIME Headers
The MIME headers tell the mail server information about what you're sending. For now, we will be declaring our content as text/plain, but later on I'll show you more fancy stuff. Other types we'll explore later are multipart/mixed and text/html - those are pretty much all you'll encounter in the real world. For now, these are our MIME headers:
MIME-Version: 1.0 Content-Type: text/plain
Same as the SMTP headers, we can put them in any order we want, omit or include whichever ones we want. The only required header is Content-Type, but MIME-Version is almost always included as well.
Let's send an email to our friend, Bob.
From: "Me" <me@codecall.net> To: "Bob" <bob@yahoo.com> Subject: First Email MIME-Version: 1.0 Content-Type: text/plain Hello, World!
Did you notice that there's a completely blank line after the Content-Type header? This is to tell the mail server that we're done sending headers, and the next line begins the actual message. The line must be completely blank, and this is always required after the last header. It's not output in the actual email, though, so Bob will get a one-line message from me.
Sending the Email
There are two ways of sending emails with sendmail: You either type it by hand at the command prompt, or you just save it in a regular old text file and pipe it to sendmail. The problem with the command prompt is that you can't use backspace, so if you make a mistake you have to start over.
Piping:
diarguet@rtp-lds-035:~$ cat mymessage | sendmail -i -t
Typing:
diarguet@rtp-lds-035:~$ sendmail -t From: "Me" <me@codecall.net> To: "Bob" <bob@yahoo.com> Subject: First Email MIME-Version: 1.0 Content-Type: text/plain Hello, World! . diarguet@rtp-lds-035:~$ _
Notice something funky here? There's a lone period at the end of our message when we typed it at the command prompt. (It needs to be on its own line, with no spaces or anything.) This is because sendmail needs a way to find out when you're done typing your message. When you pipe your file in from the command line, the EOF character is automatically transmitted at the end of your file, so sendmail doesn't need the period.
Allow me to explain the options:
-t tells sendmail that the message contains all the information it needs to send an email, i.e. the To: and From: fields, etc. Otherwise it's going to expect you to pass them as arguments on the command line.
-i tells sendmail that we're piping the message in from a file, so it should ignore the single-period requirement and expect an EOF instead. If you don't pass this argument when piping a file, then sendmail will expect a lone period at the end of your message file.
In my next tutorial, I'll show you how to spice up your emails by using HTML, and if I don't ramble on for too long, I might get to how to add attachments.
References
Message Header Reference - IANA (note that this contains a list of headers for various protocols, including HTTP, SMTP, MIME, netnews, and more.)
sendmail man page
Edited by dargueta, 02 November 2012 - 01:54 PM.
Added link to part 2