Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sending attachments with SENDMAIL or MAIL

Status
Not open for further replies.

jaaconsult

Technical User
Jan 14, 2001
15
US
I am using the following command to send a file from within an application from AIX unix to external mail recipients that looks like the following:

# /usr/lib/sendmail -t -oi -fjim@abc.com < /dbms/email/file.txt &

This works fine except that the file appears in the body of the recipient's email and I would like it to be an attachment. The files are invoices and statements and won't page properly if they are in the body of the e-mail. I am a beginner at best as a unix programmer; so please explain in terms that I can hopefully understand. Thanks.
 
You want to use uuencode. There was a good posting on this some time ago (try the search)

I personally use a perl script (perl should come as standard on an AIX system)
Code:
#!/usr/bin/perl -w
use strict;
use Getopt::Std;
 
sub print_usage
  {
  print @_;
  print STDERR "Usage $0
        -r <recipient>[,<recipient>.... ]
        -b <binary file to send>[,<binary file to send>....]
        -f <text file to send>[,<text file to send>.....]
        -s <subject>
        -t <Message Text>
        -i <Message Text as file>";
  die "\n";
  }
 
my %opts;
getopt 'rbfsti', \%opts;
defined $opts{'r'} or print_usage "No recipent(s) defined\n";
my $recips = join " ", (split /,/, $opts{'r'});
 
my $boundary = "Message-Boundary-$$";
my $uname = `whoami`;
my $host = `uname -n`;
chomp $uname;
chomp $host;
my $fullname;
open IFH,  "/etc/passwd" or print_usage "Can't open /etc/passwd\n";
foreach (<IFH>)
  {
  /^${uname}:[^:]+:[^:]+:[^:]+:([^:]+)/ and $fullname = $1, last;
  }
close IFH;
 
defined $opts{'i'} and do 
  { -r $opts{'i'} or print_usage "Unable to open $opts{'i'}\n"; };
defined $opts{'f'} and do
  {
  foreach my $tfile ( split /,/, $opts{'f'} )
    { -r $tfile or print_usage "Unable to open $tfile\n"; }
  };
defined $opts{'b'} and do
  {
  foreach my $tfile ( split /,/, $opts{'b'} )
    { -r $tfile or print_usage "Unable to open $tfile\n"; }
  };
open OFH, "|sendmail $recips" or print_usage "Unable to open pipe to sendmail\n";
print OFH "From: $fullname\n";
print OFH "To: $recips\n";
print OFH "Subject: ";
defined $opts{'s'} and print OFH "$opts{'s'}\n" or print OFH "\n";
print OFH "Content-Type:Multipart/Mixed; boundary=$boundary\n\n";
defined $opts{'t'}|| defined $opts{'i'} and do
  {
  print OFH "--$boundary\n";
  print OFH "Content-type: text/plain; charset=US-ASCII\n";
  print OFH "Content-transfer-encoding: 7BIT\n";
  print OFH "Content-description: Read Me First\n\n\n";
  defined $opts{'t'} and print OFH "$opts{'t'}\n";
  defined $opts{'i'} and do
    {
    open IFH, $opts{'i'} or print_usage "Unable to open $opts{'i'}\n";
    foreach (<IFH>) { print OFH; }
    close IFH;
    };
  };
defined $opts{'b'} and do
  {
  foreach my $file ( split /,/, $opts{'b'} )
    {
    my $shortname;
    $file =~ /([^\/]+)$/ and $shortname = $1;
    print OFH "--$boundary\n";
    print OFH "Content-type: Application/Octet-stream; name=$shortname; type=binary\n";
    print OFH "Content-disposition: attachment; filename=$shortname\n";
    print OFH "Content-transfer-encoding: X-UUencode\n\n";
    print OFH `uuencode $file $file`;
    }
  };
defined $opts{'f'} and do 
  {
  foreach my $file ( split /,/, $opts{'f'} )
    {
    my $shortname;
    $file =~ /([^\/]+)$/ and $shortname = $1;
    print OFH "--$boundary\n";
    print OFH "Content-type: Application/Octet-stream; name=$shortname; type=text\n";
    print OFH "Content-disposition: attachment; filename=$shortname\n\n";
    open IFH, $file or print_usage "Unable to open $file\n";
    foreach (<IFH>)
      {
      chomp;
      print OFH "$_\r\n";
      }
    close IFH;
    }
  };
close OFH;
To use this
[ol]
[li]vi /usr/local/bin/sendfile.pl[/li]
[li]cut and paste the code from this posting[/li]
[li]save and exit[/li]
[li]chmod 755 /usr/local/bin/sendfile.pl[/li]
[li]run sendfile.pl to show usage[/li]
[/ol]

Columb Healy
 
I've just run a quick search on this forum using the keyword uuencode - it picked up multiple threads that answer your question. Using attachment as a keywork would have worked just as well.

It's always worth doing a search before posting.

Columb Healy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top