Adding Subject and From to sendmail

Hi, based on server restrictions and a recently asked question, I am sending text file attachments using

system("cat nc/jh10083.txt | unix2dos | uuencode nc/jh10083.txt | /usr/sbin/sendmail -f me\ me\");

How do I add Subject, From, and possibly text in the body of the email?
(the -f did not show the From when received)

A previous question described

echo -e "Subject: A subject\n\nMessage text" | sendmail -f my@address recipient@address

to add the Subject

But I couldn’t put it all together.

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

You *really* would have been better off using MIME::Lite as it is easier to set all these things, plus it makes it much more portable when moving between systems.

Was there any good reason you decided against using MIME::Lite?

BrentNJAuthor Commented:
Well, issue 5 got worse -

5) MIME::Lite with open (SENDMAIL, "| /usr/sbin/sendmail -t -oi");
$msg->print(\*SENDMAIL); close(SENDMAIL);
works great, but also adds the text file contents into the body of the email itself.

Now the text only shows up in the body, no longer with the attachment.

The last cutoff for registration is coming up. I expect a rush of last minute sign-ups, so I need to get something going.

If you have any other ideas please let me know.


If you are using sendmail with MIME::Lite, you don't need to explicity print the message to a sendmail stream.

Just doing


will work fine.

If you are not seeing an attachment, please post the test code you are using.  MIME::Lite is a *very* popular module, so I'd be surprised if you were getting any problems due to the module itself.
OWASP: Avoiding Hacker Tricks

Learn to build secure applications from the mindset of the hacker and avoid being exploited.

You could use your original system call... but use pipes. Open a pipe to the system call for input and open a pipe to sendmail for output. Into sendmail first write
Subject: muhahaha

Then read line at a time from input pipe and write to output pipe.
BrentNJAuthor Commented:
I will try above suggestions.

BrentNJAuthor Commented:

Here is the code. The text file comes through in the body instead of as an attachment. I noticed that the sample for a binary file attachment had a line Encoding => 'base64',
Do I need a similar line for a test attachment?


use MIME::Lite;

$SUBJECT="Any attchement?";

# Create a new multipart message:
$msg = new MIME::Lite
   From    => "$MAILFROM",
   To      => "$MAILTO",
   Cc      => "$MAILCOPY",
   Subject => "$SUBJECT",
   Type    => 'multipart/mixed';

# Add parts (each "attach" has same arguments as "new"):
attach $msg
   Type     => 'text/plain',
   Path     => "$TXTFILE";

# Output the message to sendmail

open (SENDMAIL, "| /usr/sbin/sendmail -t -oi");
Your code above won't even compile.  Try the following version (with coding improvements!!):

use strict;
use MIME::Lite;

my $txtfile="nc/jh10083.txt";    # Should use an absolute path, otherwise you'll get bitten.
my $subject="Any attchement?";
my $mailto='';

my $msg = new MIME::Lite (
   From     => $mailfrom,
   To         => $mailto,
   Subject  => $subject

# Add attachment
$msg->attach (
   Type       => 'TEXT',
   Path        => $txtfile,
   Filename => 'Text file'


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
BrentNJAuthor Commented:
Will do. I'll try it now.
BrentNJAuthor Commented:
Message arrived with no attachment and no text in body of message.
BrentNJAuthor Commented:
I did use an absolute path for my $txtfile.
I was assuming the path you gave in your sample code was the actual path, hence the reason I made the comment about using absolute paths.

Before you construct the mail message, add:

die "$txtfile not found" unless -f $txtfile;

And see what the result is.
BrentNJAuthor Commented:
Thanks everyone for your help. It works but not consistently. I will probably change hosting services next year.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.