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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

mail attachment file from unix 3

Status
Not open for further replies.

hok1man

Technical User
Feb 16, 2008
102
0
0
Hi guys,

just wondering how to put attachment and send the email from unix..from solaris 10.

I usually use the mail command is "mailx"

search in man page.. no luck...

Thanks guys,
 
man uuencode

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Besides uuencode you probably also need unix2dos e.g.
Code:
unix2dos /etc/hosts | uuencode hostsfile.txt | mailx -s 'hosts file attached' hok1man@unknown.com
 
OK, so there's no comments and it's in perl but the print_usage routine says what it does. This version had been thoroughly tested on RedHat Linux and AIX 5.1
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`;
chomp $uname;
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"; };
  {
  foreach ( split /,/, $opts{'i'} )
    { -r $_ or print_usage "Unable to read $_\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, "|/usr/sbin/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
    {
    foreach my $ifile ( split /,/, $opts{'i'} )
      {
      open IFH, $ifile or print_usage "Unable to open ifile $ifile\n";
      print OFH <IFH>;
      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;
    print OFH "Content-type: binary; name=\"$shortname\"\n";
    print OFH "Content-Disposition: attachment; filename=\"$shortname\"\n";
    print OFH "Content-Transfer-Encoding: x-uuencode\n\n";
    #print OFH "Content-Transfer-Encoding: 7bit\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;

On the internet no one knows you're a dog

Columb Healy
 
Hi PDreyer,

your converter to DOS is not working

Code:
$ unix2dos /etc/hosts | uuencode testScript.ksh |mailx -s "file" hok1man@test.com could not open /dev/kbd to get keyboard type US keyboard assumed
could not get keyboard type US keyboard assumed
 
You can safely ignore those "could not get keyboard" warnings.

Annihilannic.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top