Adding Subject and From to sendmail

Posted on 2003-10-30
Last Modified: 2008-02-01
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.

Question by:BrentNJ
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 4
LVL 48

Expert Comment

ID: 9653503
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?


Author Comment

ID: 9654550
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.


LVL 48

Expert Comment

ID: 9654681
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.
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Assisted Solution

robysath earned 100 total points
ID: 9654918
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.

Author Comment

ID: 9679541
I will try above suggestions.


Author Comment

ID: 9702771

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");
LVL 48

Accepted Solution

Tintin earned 360 total points
ID: 9711685
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'


Author Comment

ID: 9724797
Will do. I'll try it now.

Author Comment

ID: 9724845
Message arrived with no attachment and no text in body of message.

Author Comment

ID: 9724926
I did use an absolute path for my $txtfile.
LVL 48

Expert Comment

ID: 9728363
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.

Author Comment

ID: 9882169
Thanks everyone for your help. It works but not consistently. I will probably change hosting services next year.

Featured Post

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question