Skip to content

Sendmail Linux Examples

  • by

Sendmail on the command line:

$ sendmail emailaddress write body of message CTRL-D

The CTRL-D is a end of message code for standard-in.

Example :

From: your-email@example.com

To: email@example.com

enter body of message

 

This message is missing a useful TO:line as well as a subject. To create these you need to create a file or use a script.

   $ vim email.txt

date: todays-date    // not required

to: user-email@example.com

subject: subject

from: your-email@example.com

Body of message goes here

 

Then call sendmail with that file as an input:

    $ sendmail -t user-email@example.com < email.txt

 

Or you can use the -toption to to tell sendmail to read the header of the message to figure out who to send it to.

$ sendmail -t < mail.txt

 

This will process the To: and CC: lines for you and send the mail to the correct addresses.

Or call from a script:

#!/usr/bin/perl

use Time::localtime;

open (OUT,”|/usr/sbin/sendmail -t”);

print OUT “From: your-email@domain.comn”; ## don’t forget to escape the @

print(OUT “Date: “.ctime().”n”);

print(OUT “To: $emailn”);

print(OUT “Subject: $subjectn”);

print(OUT “n”);

print(OUT “$bodyn”);

close(OUT);