Solved

How to attach file in an email

Posted on 2001-08-04
15
18,267 Views
Last Modified: 2011-08-18
I run through help for mail but couldn't find any option to attach the file in the email.

How can I do it?

Thanks.


0
Comment
Question by:IceMountain
  • 4
  • 3
  • 3
  • +3
15 Comments
 
LVL 17

Expert Comment

by:dorward
Comment Utility
Do you mean the mail program started with "mail".

I don't think there is a way, I suggest using mutt for the mail client instead, if you don't have it already you can find it on http://freshmeat.net/

Issue "mutt --help" for a list of commands, but you could do something like this:

mutt -x -a filename.to.attach -s "Subject that you want" email@address.to.send.to

The -x will start in mailx emulation mode, you can leave that off and get the body of the email from a file instead:

mutt -a filename.to.attach -s "Subject that you want" email@address.to.send.to < file.name.for.body.of.email

Or leave of the included body altogether to start your default editor to type a body.

mutt -a filename.to.attach -s "Subject that you want" email@address.to.send.to

Or just run "mutt" to use it as any email client.
0
 
LVL 51

Expert Comment

by:ahoffmann
Comment Utility
an other suggestion: pine
0
 

Author Comment

by:IceMountain
Comment Utility
I want to be able to do so in the command line as I need to schedule a cronjob to send out the email.

Can mutt and pine serve this purpose?

Thanks.
0
 
LVL 51

Expert Comment

by:ahoffmann
Comment Utility
metasend -t receipient -s metasend -f file -b -m text/plain -e base64

You may also use elm, mutt, pine
but they are trickier to use 'cause the assume interactie mode.
0
 

Author Comment

by:IceMountain
Comment Utility
is metasend already available in linux or do i have to download it?

thanks.
0
 
LVL 17

Expert Comment

by:dorward
Comment Utility
Mutt does not assume interactive mode if you use the commands I gave above.
0
 
LVL 51

Expert Comment

by:ahoffmann
Comment Utility
metasend should be part of your distribution, watch CDs

dorward, probably I missed to read the mailbody from STDIN
when using mutt
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 17

Expert Comment

by:dorward
Comment Utility
ahoffmann: Quite possibly, it fires up interactive mode if it doesn't have a body, of course if you don't want one you can 'echo " " | mutt -etc'
0
 
LVL 51

Expert Comment

by:ahoffmann
Comment Utility
metasend should be part of your distribution, watch CDs

dorward, probably I missed to read the mailbody from STDIN
when using mutt
0
 
LVL 5

Accepted Solution

by:
moonbeam012200 earned 50 total points
Comment Utility
I understand your frustration. Many times I have had a need
from within a cron job or a script to send mail with text
and an attachment. If you are sending a file to a microsoft
person, you can just do:

uuencode file name | mailx -s file dest

This works because microsoft mailers to not follow the rfc
standards for uuencoded attachments. A unix client will
not see an attachment, but a uuencoded data block. (Which
is OK, for the uudecode savy unix people!)

The desired formatting would be a properly formatted
encoded attachments is a plain/text header and a base64
translated attachment. To achieve this, I wrote my own
mailer using perl. This mailer is meant to be used from
either a cron job or at the command line. The syntax is:

-->mime_mail
Usage: mime_mail -h | -d document.txt -s "mail subject"
          -u "user1 [,user2 ...]" -f "file1 [,file2 ...]"

Full help ...

-->mime_mail -h
NAME
    mime_mail

DISCRIPTION
    This program is a command line mailer that sends a text file
    description and one or more files in mime attachments. On success,
    mime_mail will work silently.

SYNOPSIS
    mime_mail -h | -d document.txt -s "mail subject"
              -u "user1 [,user2 ...]" -f "file1 [,file2 ...]"

OPTIONS
    -h
        Displays a man page.

    -d document.txt
        Sends "document.txt" in the mail message body. The file must
        be a normal text file. If a "-" is provided as an argument,
        the input will be taken from stdin. If no "-d" argument is
        supplied, the attachment will be sent with the message
        "Please see the attached.".

    -s "mail subject"
        Sets the mail subject. The mail subject must be quoted if the
        subject contains any spaces.

    -u user1,user2 ...
        A comma separated list of email addresses or aliases. Quotes
        are required if the list contains any spaces.

    -f file1,file2 ...
        A comma separaed list of files to attach. Quotes are required
        if the list contains any spaces. The files can be of any type.
        The recognised application content types are ".doc", ".html",
        ,"htm",".xls", ".csv", ".pdf", ".rtf", and ".mdb".

William Julien
-->mime_mail -h
NAME
    mime_mail

DISCRIPTION
    This program is a command line mailer that sends a text file
    description and one or more files in mime attachments. On success,
    mime_mail will work silently.

SYNOPSIS
    mime_mail -h | -d document.txt -s "mail subject"
              -u "user1 [,user2 ...]" -f "file1 [,file2 ...]"

OPTIONS
    -h
        Displays a man page.

    -d document.txt
        Sends "document.txt" in the mail message body. The file must
        be a normal text file. If a "-" is provided as an argument,
        the input will be taken from stdin. If no "-d" argument is
        supplied, the attachment will be sent with the message
        "Please see the attached.".

    -s "mail subject"
        Sets the mail subject. The mail subject must be quoted if the
        subject contains any spaces.

    -u user1,user2 ...
        A comma separated list of email addresses or aliases. Quotes
        are required if the list contains any spaces.

    -f file1,file2 ...
        A comma separaed list of files to attach. Quotes are required
        if the list contains any spaces. The files can be of any type.
        The recognised application content types are ".doc", ".html",
        ,"htm",".xls", ".csv", ".pdf", ".rtf", and ".mdb".

William Julien

With this utility you can send a properly formatted attachment with a text header.

Here is the perl script:

#!/usr/bin/perl -w
#
# simple program to send a text message with an attachment
#
# William Julien
###

use strict;             # turn on the strict pragma
use Getopt::Std;        # use standard getopt to parse options

$/ = undef;             # turn on slurp mode

#
# scope local variables
#
my ($date,              # the current date
    $sendmail,          # the sendmail command
    $document,          # the document text
    %options,           # program options
    $filename,          # a file to send
    $file,              # the content of a file
    @filelist,          # list files to send
    $tolist,            # list of comma separated email addresses
    $today,             # today's date
    $usage,             # program usage
    $man,               # program man page
    $subject,           # email subject
    $extent,            # file extension
    %mime_type,         # list of supported application context mime types
    $mime_type,         # current mime application content type
    );

#
# variable initializations
#
$man = <<MAN;
NAME
    mime_mail

DISCRIPTION
    This program is a command line mailer that sends a text file
    description and one or more files in mime attachments. On success,
    mime_mail will work silently.

SYNOPSIS
    mime_mail -h | -d document.txt -s "mail subject"
              -u "user1 [,user2 ...]" -f "file1 [,file2 ...]"

OPTIONS
    -h
        Displays a man page.

    -d document.txt
        Sends "document.txt" in the mail message body. The file must
        be a normal text file. If a "-" is provided as an argument,
        the input will be taken from stdin. If no "-d" argument is
        supplied, the attachment will be sent with the message
        "Please see the attached.".

    -s "mail subject"
        Sets the mail subject. The mail subject must be quoted if the
        subject contains any spaces.

    -u user1,user2 ...
        A comma separated list of email addresses or aliases. Quotes
        are required if the list contains any spaces.

    -f file1,file2 ...
        A comma separaed list of files to attach. Quotes are required
        if the list contains any spaces. The files can be of any type.
        The recognised application content types are ".doc", ".html",
        ,"htm",".xls", ".csv", ".pdf", ".rtf", and ".mdb".

William Julien
425-865-5511
MAN
$usage = <<USAGE;

Usage:
mime_mail -h | -d document.txt -s "mail subject"
          -u "user1 [,user2 ...]" -f "file1 [,file2 ...]"
USAGE
$sendmail = "/usr/lib/sendmail";
if ( ! -f $sendmail ) {
    die "Sorry -- cannot locate sendmail at $sendmail.\nYou must edit $0\n";
}
$today = scalar(localtime());
%mime_type = ("doc"   => "application/msword",
              "txt"   => "text/plain",
              "htm"   => "text/html",
              "html"  => "text/html",
              "xls"   => "application/vnd.ms-excel",
              "csv"   => "application/octet-stream",
              "pdf"   => "application/acrobat",
              "rtf"   => "application/rtf",
              "mdb"   => "application/vnd.ms-access",
             );

#
# process program options
#
getopts('d:s:u:f:h', \%options) or die "$usage\n";
if ( defined $options{"h"} ) {
    print "$man\n";
    exit;
}
if ( defined $options{"f"} ) {
    @filelist = split /\,/,$options{"f"};
    foreach $file (@filelist) {
        if (! -f $file) {
            die "$0: cannot open $file\n";
        }
    }
} else {
    die "$usage\n";
}
if ( defined $options{"d"} ) {
    $document = $options{"d"};
    if ( $document eq "-" ) {
        $document = <STDIN>;
    } else {
        if ( -f $document ) {
            open F, "$document";
            $document = <F>;
            close F;
        } else {
            die "$0 error: cannot open $document\n";
        }
    }
} else {
    $document = "Please see the attached.\n";
}
if ( defined $options{"s"} ) {
    $subject = $options{"s"};
} else {
    die "$usage\nError: No Subject\n";
}
if ( defined $options{"u"} ) {
    $tolist = $options{"u"};
} else {
    die "$usage\n$0 error: no subject specified\n";
}

open MAIL, "|$sendmail -t";
print MAIL <<HEADER;
To: $tolist
Subject: $subject
Date: $today
Mime-Version: 1.0
Content-Type: multipart/mixed;
        boundary="ThisRandomString"

This mail was formatted with mime_mail (wmj)

--ThisRandomString
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

$document
HEADER
foreach $filename (@filelist) {
    $extent = $filename =~ /\.(\w+)$/ ? "\L$1" : undef;
    $mime_type = "application/octet-stream";
    if ( defined $extent ) {
        if ( defined $mime_type{"$extent"} ) {
            $mime_type = $mime_type{"$extent"};
        } else {
            $mime_type = "application/octet-stream";
        }
    }
    open F,"$filename";
    $file = <F>;
    close F;
    $file = encode_base64($file);
    print MAIL <<FILE;
--ThisRandomString
Content-Type: $mime_type; name="$filename"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="$filename"

$file
FILE
}
print MAIL "--ThisRandomString--\n";
#
# send the mail
#
close MAIL;

#
# fin
###

sub encode_base64
{
    use integer;
    my $res = "";
    my $eol = $_[1];
    $eol = "\n" unless defined $eol;
    pos($_[0]) = 0;                          # ensure start at the beginning
    while ($_[0] =~ /(.{1,45})/gs) {
        $res .= substr(pack('u', $1), 1);
        chop($res);
    }
    $res =~ tr|` -_|AA-Za-z0-9+/|;            # `# help emacs
    # fix padding at the end
    my $padding = (3 - length($_[0]) % 3) % 3;
    $res =~ s/.{$padding}$/'=' x $padding/e if $padding;
    # break encoded string into lines of no more than 76 characters each
    if (length $eol) {
        $res =~ s/(.{1,76})/$1$eol/g;
    }
    return $res;
}
#!/usr/bin/perl -w
#
# simple program to send a text message with an attachment
#
# William Julien
###

use strict;             # turn on the strict pragma
use Getopt::Std;        # use standard getopt to parse options

$/ = undef;             # turn on slurp mode

#
# scope local variables
#
my ($date,              # the current date
    $sendmail,          # the sendmail command
    $document,          # the document text
    %options,           # program options
    $filename,          # a file to send
    $file,              # the content of a file
    @filelist,          # list files to send
    $tolist,            # list of comma separated email addresses
    $today,             # today's date
    $usage,             # program usage
    $man,               # program man page
    $subject,           # email subject
    $extent,            # file extension
    %mime_type,         # list of supported application context mime types
    $mime_type,         # current mime application content type
    );

#
# variable initializations
#
$man = <<MAN;
NAME
    mime_mail

DISCRIPTION
    This program is a command line mailer that sends a text file
    description and one or more files in mime attachments. On success,
    mime_mail will work silently.

SYNOPSIS
    mime_mail -h | -d document.txt -s "mail subject"
              -u "user1 [,user2 ...]" -f "file1 [,file2 ...]"

OPTIONS
    -h
        Displays a man page.

    -d document.txt
        Sends "document.txt" in the mail message body. The file must
        be a normal text file. If a "-" is provided as an argument,
        the input will be taken from stdin. If no "-d" argument is
        supplied, the attachment will be sent with the message
        "Please see the attached.".

    -s "mail subject"
        Sets the mail subject. The mail subject must be quoted if the
        subject contains any spaces.

    -u user1,user2 ...
        A comma separated list of email addresses or aliases. Quotes
        are required if the list contains any spaces.

    -f file1,file2 ...
        A comma separaed list of files to attach. Quotes are required
        if the list contains any spaces. The files can be of any type.
        The recognised application content types are ".doc", ".html",
        ,"htm",".xls", ".csv", ".pdf", ".rtf", and ".mdb".

William Julien
425-865-5511
MAN
$usage = <<USAGE;

Usage:
mime_mail -h | -d document.txt -s "mail subject"
          -u "user1 [,user2 ...]" -f "file1 [,file2 ...]"
USAGE
$sendmail = "/usr/lib/sendmail";
if ( ! -f $sendmail ) {
    die "Sorry -- cannot locate sendmail at $sendmail.\nYou must edit $0\n";
}
$today = scalar(localtime());
%mime_type = ("doc"   => "application/msword",
              "txt"   => "text/plain",
              "htm"   => "text/html",
              "html"  => "text/html",
              "xls"   => "application/vnd.ms-excel",
              "csv"   => "application/octet-stream",
              "pdf"   => "application/acrobat",
              "rtf"   => "application/rtf",
              "mdb"   => "application/vnd.ms-access",
             );

#
# process program options
#
getopts('d:s:u:f:h', \%options) or die "$usage\n";
if ( defined $options{"h"} ) {
    print "$man\n";
    exit;
}
if ( defined $options{"f"} ) {
    @filelist = split /\,/,$options{"f"};
    foreach $file (@filelist) {
        if (! -f $file) {
            die "$0: cannot open $file\n";
        }
    }
} else {
    die "$usage\n";
}
if ( defined $options{"d"} ) {
    $document = $options{"d"};
    if ( $document eq "-" ) {
        $document = <STDIN>;
    } else {
        if ( -f $document ) {
            open F, "$document";
            $document = <F>;
            close F;
        } else {
            die "$0 error: cannot open $document\n";
        }
    }
} else {
    $document = "Please see the attached.\n";
}
if ( defined $options{"s"} ) {
    $subject = $options{"s"};
} else {
    die "$usage\nError: No Subject\n";
}
if ( defined $options{"u"} ) {
    $tolist = $options{"u"};
} else {
    die "$usage\n$0 error: no subject specified\n";
}

open MAIL, "|$sendmail -t";
print MAIL <<HEADER;
To: $tolist
Subject: $subject
Date: $today
Mime-Version: 1.0
Content-Type: multipart/mixed;
        boundary="ThisRandomString"

This mail was formatted with mime_mail (wmj)

--ThisRandomString
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

$document
HEADER
foreach $filename (@filelist) {
    $extent = $filename =~ /\.(\w+)$/ ? "\L$1" : undef;
    $mime_type = "application/octet-stream";
    if ( defined $extent ) {
        if ( defined $mime_type{"$extent"} ) {
            $mime_type = $mime_type{"$extent"};
        } else {
            $mime_type = "application/octet-stream";
        }
    }
    open F,"$filename";
    $file = <F>;
    close F;
    $file = encode_base64($file);
    print MAIL <<FILE;
--ThisRandomString
Content-Type: $mime_type; name="$filename"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="$filename"

$file
FILE
}
print MAIL "--ThisRandomString--\n";
#
# send the mail
#
close MAIL;

#
# fin
###

sub encode_base64
{
    use integer;
    my $res = "";
    my $eol = $_[1];
    $eol = "\n" unless defined $eol;
    pos($_[0]) = 0;                          # ensure start at the beginning
    while ($_[0] =~ /(.{1,45})/gs) {
        $res .= substr(pack('u', $1), 1);
        chop($res);
    }
    $res =~ tr|` -_|AA-Za-z0-9+/|;            # `# help emacs
    # fix padding at the end
    my $padding = (3 - length($_[0]) % 3) % 3;
    $res =~ s/.{$padding}$/'=' x $padding/e if $padding;
    # break encoded string into lines of no more than 76 characters each
    if (length $eol) {
        $res =~ s/(.{1,76})/$1$eol/g;
    }
    return $res;
}
0
 
LVL 5

Expert Comment

by:moonbeam012200
Comment Utility
sorry, on review of my post I see that netscape duped the
paste. Sorry about that folks. I figure that you can figure
it out. If not, please let me know.

DOH!

William
btw... I'm Baaaak!
0
 
LVL 5

Expert Comment

by:Netminder
Comment Utility
IceMountain,

Some of these questions have been open for some time. Please resolve them appropriately as soon as possible. Continued disregard of your open questions will result in the force/acceptance of a comment as an answer; other actions affecting your account may also be taken. I will revisit these questions in approximately seven (7) days.

http://www.experts-exchange.com/jsp/qShow.jsp?ta=apache&qid=20244080 12/05/01  
http://www.experts-exchange.com/jsp/qShow.jsp?ta=dbgen&qid=20176058 08/29/01  
http://www.experts-exchange.com/jsp/qShow.jsp?ta=linux&qid=20104970 04/12/01  
http://www.experts-exchange.com/jsp/qShow.jsp?ta=linux&qid=20163211 08/04/01  
http://www.experts-exchange.com/jsp/qShow.jsp?ta=lotusnotes&qid=11950339 11/22/00  
http://www.experts-exchange.com/jsp/qShow.jsp?ta=lotusnotes&qid=20014301 12/28/00  
http://www.experts-exchange.com/jsp/qShow.jsp?ta=lotusnotes&qid=20029438 01/10/01  
http://www.experts-exchange.com/jsp/qShow.jsp?ta=lotusnotes&qid=20074928 02/08/01  
http://www.experts-exchange.com/jsp/qShow.jsp?ta=lotusnotes&qid=20079924 02/19/01  
http://www.experts-exchange.com/jsp/qShow.jsp?ta=lotusnotes&qid=20122138 05/22/01  
http://www.experts-exchange.com/jsp/qShow.jsp?ta=lotusnotes&qid=20124947 05/26/01  
http://www.experts-exchange.com/jsp/qShow.jsp?ta=lotusnotes&qid=20136581 06/16/01  
http://www.experts-exchange.com/jsp/qShow.jsp?ta=lotusnotes&qid=20142793 06/28/01  
http://www.experts-exchange.com/jsp/qShow.jsp?ta=lotusnotes&qid=20144554 07/02/01  
http://www.experts-exchange.com/jsp/qShow.jsp?ta=lotusnotes&qid=20168831 08/15/01  
http://www.experts-exchange.com/jsp/qShow.jsp?ta=lotusnotes&qid=20208718 10/24/01  
http://www.experts-exchange.com/jsp/qShow.jsp?ta=lotusnotes&qid=20226597 11/04/01  
http://www.experts-exchange.com/jsp/qShow.jsp?ta=php&qid=20096626 03/25/01  
http://www.experts-exchange.com/jsp/qShow.jsp?ta=php&qid=20108210 04/21/01  
http://www.experts-exchange.com/jsp/qShow.jsp?ta=php&qid=20184032 09/17/01  
http://www.experts-exchange.com/jsp/qShow.jsp?ta=php&qid=20206133 10/23/01  
http://www.experts-exchange.com/jsp/qShow.jsp?ta=php&qid=20227517 11/05/01  
http://www.experts-exchange.com/jsp/qShow.jsp?ta=perl&qid=20078009 02/14/01  
http://www.experts-exchange.com/jsp/qShow.jsp?ta=perl&qid=20084634 02/27/01  
http://www.experts-exchange.com/jsp/qShow.jsp?ta=perl&qid=20093489 03/18/01  

Please note that an Easy question is normally considered to be worth 50 points.

Thanks,

Netminder
Community Support Moderator
Experts Exchange
0
 
LVL 5

Expert Comment

by:Netminder
Comment Utility
Admin notified of user neglect. Force/accepted by

Netminder
Community Support Moderator
Experts Exchange
0
 
LVL 3

Expert Comment

by:dbauermann
Comment Utility
I add a new option for EXCELENT William's script.

Congratulations William.


#!/usr/bin/perl -w
#
# simple program to send a text message with an attachment
#
# William Julien
###
#
# Create a new option to choose message type (text | html)
# 08/27/04  Daniel Bauermann
###

use strict;             # turn on the strict pragma
use Getopt::Std;        # use standard getopt to parse options

$/ = undef;             # turn on slurp mode

#
# scope local variables
#
my ($date,              # the current date
    $sendmail,          # the sendmail command
    $document,          # the document text
    %options,           # program options
    $filename,          # a file to send
    $file,              # the content of a file
    @filelist,          # list files to send
    $tolist,            # list of comma separated email addresses
    $today,             # today's date
    $usage,             # program usage
    $man,               # program man page
    $subject,           # email subject
    $extent,            # file extension
    %mime_type,         # list of supported application context mime types
    $mime_type,         # current mime application content type
    $typemessage,       # define type message (new option)
    $headertypemessage, # content type message (new option)
    );

#
# variable initializations
#
$man = <<MAN;
NAME
    mime_mail

DISCRIPTION
    This program is a command line mailer that sends a text file
    description and one or more files in mime attachments. On success,
    mime_mail will work silently.

SYNOPSIS
    mime_mail -h | -d document.txt -s "mail subject"
              -u "user1 [,user2 ...]" -f "file1 [,file2 ...]" -t [text|html]

OPTIONS
    -h
        Displays a man page.

    -d document.txt
        Sends "document.txt" in the mail message body. The file must
        be a normal text file. If a "-" is provided as an argument,
        the input will be taken from stdin. If no "-d" argument is
        supplied, the attachment will be sent with the message
        "Please see the attached.".

    -s "mail subject"
        Sets the mail subject. The mail subject must be quoted if the
        subject contains any spaces.

    -u user1,user2 ...
        A comma separated list of email addresses or aliases. Quotes
        are required if the list contains any spaces.

    -f file1,file2 ...
        A comma separaed list of files to attach. Quotes are required
        if the list contains any spaces. The files can be of any type.
        The recognised application content types are ".doc", ".html",
        ,"htm",".xls", ".csv", ".pdf", ".rtf", and ".mdb".

    -t [text|html]
        Message type

William Julien
425-865-5511
MAN
$usage = <<USAGE;

Usage:
mime_mail -h | -d document.txt -s "mail subject"
          -u "user1 [,user2 ...]" -f "file1 [,file2 ...]" -t [text|html]
USAGE
$sendmail = "/usr/lib/sendmail";
if ( ! -f $sendmail ) {
    die "Sorry -- cannot locate sendmail at $sendmail.\nYou must edit $0\n";
}
$today = scalar(localtime());
%mime_type = ("doc"   => "application/msword",
              "txt"   => "text/plain",
              "htm"   => "text/html",
              "html"  => "text/html",
              "xls"   => "application/vnd.ms-excel",
              "csv"   => "application/octet-stream",
              "pdf"   => "application/acrobat",
              "rtf"   => "application/rtf",
              "mdb"   => "application/vnd.ms-access",
             );

#
# process program options
#
getopts('d:s:u:f:h:t', \%options) or die "$usage\n";
if ( defined $options{"h"} ) {
    print "$man\n";
    exit;
}
if ( defined $options{"f"} ) {
    @filelist = split /\,/,$options{"f"};
    foreach $file (@filelist) {
        if (! -f $file) {
            die "$0: cannot open $file\n";
        }
    }
} else {
    die "$usage\n";
}
if ( defined $options{"d"} ) {
    $document = $options{"d"};
    if ( $document eq "-" ) {
        $document = <STDIN>;
    } else {
        if ( -f $document ) {
            open F, "$document";
            $document = <F>;
            close F;
        } else {
            die "$0 error: cannot open $document\n";
        }
    }
} else {
    $document = "Please see the attached.\n";
}
if ( defined $options{"s"} ) {
    $subject = $options{"s"};
} else {
    die "$usage\nError: No Subject\n";
}
if ( defined $options{"u"} ) {
    $tolist = $options{"u"};
} else {
    die "$usage\n$0 error: no subject specified\n";
}
#
# New option
#
if ( defined $options{"t"} ) {
    $typemessage = $options{"t"};
} else {
    $typemessage = "text";
}
if ( $typemessage eq "text" ) {
    $headertypemessage = "Content-Type: text/plain; charset=us-ascii\nContent-Transfer-Encoding: 7bit";
} else {
    $headertypemessage = "Content-Type: text/html;\n   charset=\"iso-8859-1\"\nContent-Transfer-Encoding: quoted-printable";
}
#
# End new option
#

open MAIL, "|$sendmail -t";
print MAIL <<HEADER;
To: $tolist
Subject: $subject
Date: $today
Mime-Version: 1.0
Content-Type: multipart/mixed;
        boundary="ThisRandomString"

This mail was formatted with mime_mail (wmj)

--ThisRandomString
$headertypemessage

$document
HEADER
foreach $filename (@filelist) {
    $extent = $filename =~ /\.(\w+)$/ ? "\L$1" : undef;
    $mime_type = "application/octet-stream";
    if ( defined $extent ) {
        if ( defined $mime_type{"$extent"} ) {
            $mime_type = $mime_type{"$extent"};
        } else {
            $mime_type = "application/octet-stream";
        }
    }
    open F,"$filename";
    $file = <F>;
    close F;
    $file = encode_base64($file);
    print MAIL <<FILE;
--ThisRandomString
Content-Type: $mime_type; name="$filename"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="$filename"

$file
FILE
}
print MAIL "--ThisRandomString--\n";
#
# send the mail
#
close MAIL;

#
# fin
###

sub encode_base64
{
    use integer;
    my $res = "";
    my $eol = $_[1];
    $eol = "\n" unless defined $eol;
    pos($_[0]) = 0;                          # ensure start at the beginning
    while ($_[0] =~ /(.{1,45})/gs) {
        $res .= substr(pack('u', $1), 1);
        chop($res);
    }
    $res =~ tr|` -_|AA-Za-z0-9+/|;            # `# help emacs
    # fix padding at the end
    my $padding = (3 - length($_[0]) % 3) % 3;
    $res =~ s/.{$padding}$/'=' x $padding/e if $padding;
    # break encoded string into lines of no more than 76 characters each
    if (length $eol) {
        $res =~ s/(.{1,76})/$1$eol/g;
    }
    return $res;
}
0
 
LVL 5

Expert Comment

by:moonbeam012200
Comment Utility
Works for me.

William
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

I. Introduction There's an interesting discussion going on now in an Experts Exchange Group — Attachments with no extension (http://www.experts-exchange.com/discussions/210281/Attachments-with-no-extension.html). This reminded me of questions tha…
Join Greg Farro and Ethan Banks from Packet Pushers (http://packetpushers.net/podcast/podcasts/pq-show-93-smart-network-monitoring-paessler-sponsored/) and Greg Ross from Paessler (https://www.paessler.com/prtg) for a discussion about smart network …
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now