Solved

Adding Subject and From to sendmail

Posted on 2003-10-30
12
2,485 Views
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\@myhost.com me\@myhost.com");

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.

Thanks!!
0
Comment
Question by:BrentNJ
  • 7
  • 4
12 Comments
 
LVL 48

Expert Comment

by:Tintin
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?

0
 

Author Comment

by:BrentNJ
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.

Thanks!!



0
 
LVL 48

Expert Comment

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

$msg->send;

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.
0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
LVL 1

Assisted Solution

by:robysath
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
From: from@me.com
Subject: muhahaha

Then read line at a time from input pipe and write to output pipe.
0
 

Author Comment

by:BrentNJ
ID: 9679541
I will try above suggestions.

Thanks
0
 

Author Comment

by:BrentNJ
ID: 9702771
Tinton,

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?

Thanks


#!/usr/bin/perl
use MIME::Lite;


$TXTFILE="nc/jh10083.txt";
$SUBJECT="Any attchement?";
$MAILTO="me\@myhost.net";

# 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");
$msg->send;
close(SENDMAIL);
0
 
LVL 48

Accepted Solution

by:
Tintin earned 360 total points
ID: 9711685
Your code above won't even compile.  Try the following version (with coding improvements!!):

#!/usr/bin/perl
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='me@myhost.net';

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


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

$msg->send;
 
0
 

Author Comment

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

Author Comment

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

Author Comment

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

Expert Comment

by:Tintin
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.
0
 

Author Comment

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

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
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…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

775 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