Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Sending Emails With Sendmail - Part 1

form sendmail

  • Please log in to reply
9 replies to this topic

#1 dargueta

dargueta

    I chown trolls.

  • Moderator
  • 4854 posts
  • Programming Language:C, Java, C++, PHP, Python, JavaScript, Perl, Assembly, Bash, Others
  • Learning:Objective-C

Posted 31 August 2009 - 03:24 PM

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

  • 3

sudo rm -rf / && echo $'Sanitize your inputs!'


#2 WingedPanther73

WingedPanther73

    A spammer's worst nightmare

  • Moderator
  • 17757 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

Posted 01 September 2009 - 04:23 AM

interesting one. +rep
  • 0

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/


#3 dargueta

dargueta

    I chown trolls.

  • Moderator
  • 4854 posts
  • Programming Language:C, Java, C++, PHP, Python, JavaScript, Perl, Assembly, Bash, Others
  • Learning:Objective-C

Posted 01 September 2009 - 04:29 AM

Thanks!
  • 0

sudo rm -rf / && echo $'Sanitize your inputs!'


#4 Guest_Jordan_*

Guest_Jordan_*
  • Guest

Posted 01 September 2009 - 04:56 AM

Sendmail is a great tool. +rep!
  • 0

#5 patiobarbecue

patiobarbecue

    CC Lurker

  • New Member
  • Pip
  • 4 posts
  • Programming Language:C

Posted 02 November 2012 - 10:56 AM

thanks!

however, sendmail simply waits forever! I tried both typing and piping as suggested. sendmail does not respond to my CTRL+D neither. What can I do? I am on Ubuntu 12.04.
  • 0

#6 dargueta

dargueta

    I chown trolls.

  • Moderator
  • 4854 posts
  • Programming Language:C, Java, C++, PHP, Python, JavaScript, Perl, Assembly, Bash, Others
  • Learning:Objective-C

Posted 03 November 2012 - 09:07 PM

Odd... try

cat myfile - | sendmail -it

Be sure to include that dash, and hit CTRL-D after you press Enter. Not right after, cat has to pipe myfile in first, then the CTRL-D.
  • 0

sudo rm -rf / && echo $'Sanitize your inputs!'


#7 patiobarbecue

patiobarbecue

    CC Lurker

  • New Member
  • Pip
  • 4 posts
  • Programming Language:C

Posted 05 November 2012 - 10:59 AM

tried it, and sendmail didn't wait forever this time. however, no emails sent out at all. Maybe this is an installation problem? I recall there were warnings during installation like "domain name not right". Will try to reinstall to see what is the issue. Thanks, Dargueta.
  • 0

#8 dargueta

dargueta

    I chown trolls.

  • Moderator
  • 4854 posts
  • Programming Language:C, Java, C++, PHP, Python, JavaScript, Perl, Assembly, Bash, Others
  • Learning:Objective-C

Posted 05 November 2012 - 03:06 PM

That was my first guess, but I decided to try that hack. It most likely can't contact the SMTP server or something. Are you sending these from a personal computer not logged into a network with an SMTP server?
  • 0

sudo rm -rf / && echo $'Sanitize your inputs!'


#9 sendmail

sendmail

    CC Lurker

  • Just Joined
  • Pip
  • 1 posts
  • Programming Language:C, Java, C++, PHP, JavaScript, Logo, Assembly, Bash

Posted 12 January 2013 - 03:23 AM

I had sendmail error Recipient names must be specified
To fix Recipient names must be specified, add to: header.
To: name surname <nick@example.com>
and use:
cat email.patch | sendmail -i -t
to send the email
  • 0

#10 dargueta

dargueta

    I chown trolls.

  • Moderator
  • 4854 posts
  • Programming Language:C, Java, C++, PHP, Python, JavaScript, Perl, Assembly, Bash, Others
  • Learning:Objective-C

Posted 12 January 2013 - 03:13 PM

Well, that's pretty much exactly what I do in the tutorial, so I think you might've made a typo.
  • 0

sudo rm -rf / && echo $'Sanitize your inputs!'






Also tagged with one or more of these keywords: form, sendmail

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download