How do I use Sendmail?

Sendmail is a mail transfer agent (MTA) that is a well known project of the open source, free software, and Unix communities. It is distributed both as free software and proprietary software. It is used to route email over the Internet to a specific recipient, and can be done by running the sendmail command in a Shell environment. It can also be used within a programming script to send email.

This article covers a few simple examples of Sendmail.

Using sendmail from the command line

The following are two examples on how to send email from the command line using sendmail.

Simple example

View the SSH article for detailed instructions on how to log in to your web server via SSH. Once logged in, you can run the following command to send email:

[server]$ /usr/sbin/sendmail youremail@example.com
Subject: Test Send Mail
Hello World
control d (this key combination of control key and d will finish the email.)

The above example sends an email as follows:

  • The FROM email address is your user@server. For example, username@servername.dreamhost.com.
  • The Subject is ‘Test Send Mail’.
  • The body of the message says ‘Hello World’.
  • The TO address only shows as undisclosed-recipients.

Adding To and From addresses

This example is the same as the one above, but adds a specific To and From address:

[server]$ /usr/sbin/sendmail youremail@example.com
To: differentemail@example.com
From: anyone@example.com
Subject: Test Send Mail
Hello World
control d (this key combination will finish the email.)

The above example sends an email as follows:

  • The From address is what you entered: anyone@example.com.
  • The Subject is what you entered: Test Send Mail.
  • The body of the message is what you entered: Hello World.
  • The To address is what you entered: youremail@example.com.

The email you enter into the To field does not receive an email. It only shows as the To address when the actual recipient receives the email. The recipient is still the first email you enter on the first line when running the command, and can also be the same if you wish.

Sendmail in a cgi script

You can also use sendmail in a script as well. This can be done in several languages. The following example shows a perl cgi script.

Create a file name perltest.pl with the following code and make sure the permissions on the file are set to 755 or it will not run:

#!/usr/bin/perl
use strict;
my($mailprog) = '/usr/sbin/sendmail';
my($from_address) ='AnyoneYouWant@example.com';
my($to_address) ='email@example.com';
       open (MAIL, "|$mailprog -t $to_address") || die "Can't open $mailprog!\n";
       print MAIL "To: $to_address\n";
       print MAIL "From: $from_address\n";
       print MAIL "Subject: Hello, self. Nice to email myself.";
       print MAIL "This is still subject line.\n";
       print MAIL "This is the body of my message.\n";
       print MAIL "Pretty boring, yes?\n";
       print MAIL "Goodbye.\n";
       close (MAIL);

This example sends an email to email@example.com.

See also

Did this article answer your questions?

Article last updated .