+ Reply to Thread
Results 1 to 4 of 4

Thread: Sending Emails With Sendmail - Part 1

  1. #1
    Join Date
    Oct 2007
    Location
    /dev/null
    Posts
    4,513
    Blog Entries
    8
    Rep Power
    59

    Sending Emails With Sendmail - Part 1

    Sending emails with sendmail is quite easy, so tutorial will mostly show you how to correctly format an email rather than how to use sendmail itself. There are three parts to an email: the SMTP headers, MIME headers, and the actual message body.

    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:
    Code:
    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:

    Code:
    "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:

    Code:
    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.

    Code:
    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:
    Code:
    diarguet@rtp-lds-035:~$ cat mymessage | sendmail -i -t
    Typing:
    Code:
    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
    Last edited by dargueta; 09-01-2009 at 05:28 AM.
    sudo rm -rf /

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    Join Date
    Jul 2006
    Posts
    16,494
    Blog Entries
    75
    Rep Power
    143

    Re: Sending Emails With Sendmail - Part 1

    interesting one. +rep
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  4. #3
    Join Date
    Oct 2007
    Location
    /dev/null
    Posts
    4,513
    Blog Entries
    8
    Rep Power
    59

    Re: Sending Emails With Sendmail - Part 1

    Thanks!
    sudo rm -rf /

  5. #4
    Jordan Guest

    Re: Sending Emails With Sendmail - Part 1

    Sendmail is a great tool. +rep!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Beginner MySQL Part 2: Connecting and sending querys.
    By bbqroast in forum PHP Tutorials
    Replies: 0
    Last Post: 07-24-2011, 03:22 PM
  2. Sending emails
    By Vswe in forum Visual Basic Tutorials
    Replies: 8
    Last Post: 04-13-2010, 06:06 AM
  3. Sending Emails With Sendmail - Part 3
    By dargueta in forum Linux Tutorials, Guides and Tips
    Replies: 6
    Last Post: 09-03-2009, 06:41 PM
  4. Sending Emails With Sendmail - Part 2
    By dargueta in forum Linux Tutorials, Guides and Tips
    Replies: 7
    Last Post: 09-02-2009, 02:46 PM
  5. Sending emails with PHP & jQuery
    By Jaan in forum PHP Tutorials
    Replies: 9
    Last Post: 08-13-2009, 08:24 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts