Using Google Chrome?

Download my
free Chrome Extension, Power Notes Searcher, to make searching for and evaluating SAP notes, much easier.

Recent Posts

Thursday, March 28, 2013

HowTo: Script eMail With Attachment Direct via SMTP Server

Print Friendly and PDF
Have you ever wondered how you can directly send an email with a file attachment, direct from a shell script via an SMTP server?
That is, without needing to rely on any mail or sendmail setup on the UNIX/Linux server.
Well, it's simple.

Create a script that encodes the file you wish to attach, into BASE64, then type the following to send the file:

telnet 25<<EOF
mail from:
rcpt to:
Subject: Test attachment

Mime-Version: 1.0
Content-Type: multipart/mixed; boundary=attachmentbound

The email body

Content-Type: application/octet-stream; name="attachmentfile"
Content-Disposition: attachment\; filename="thefilename.txt"
Content-Transfer-Encoding: base64

VGVzdCBBdHRhY2htZW50     <<-- Insert your BASE64 content here!


You need to have the content already encoded in BASE64.
For this you could use a Perl script using the MIME::Base64 module as shown below:

use MIME::Base64;
#printf ("%s", encode_base64(eval "\"$ARGV[0]\""));

## Check to see if each and every file exists ##
foreach $file (@ARGV)
if (not -e $file)
print "File \"$file\" does not exist\n";
exit( 1 );

# Convert file to BASE64
open( OUTFILE, ">$file.out") or die "Could not open outputfile: $!\n";;
open( FILE, $file ) or die "Could not open inputfile: $!\n";
local($/) = undef;
print OUTFILE encode_base64(<FILE>);
close( FILE );
close( OUTFILE );

No comments: