Solved

Email a GZIP attachment in a bash script

Posted on 2014-01-13
3
640 Views
Last Modified: 2014-01-18
Hello everyone

I am trying (and failing) to write a shell script which can email a GZIP archive as an attachment from my web host's shared server. I've tried following multiple sets of instructions found online with little success. Sometimes the email comes through in gobbledygook (looks like the GZIP file is inline rather than attached), sometimes the GZIP file is present but can't be opened in 7-Zip, sometimes the scripts I've based off of just fail.

So I'd like to start afresh. The web host has (unfortunately) no mutt available, so that easy solution is off the table.

Has anyone got a working script sample I can adapt?

Thanks!
David
0
Comment
Question by:davidiwharper
  • 2
3 Comments
 
LVL 8

Expert Comment

by:Pepe2323
ID: 39778032
Hi

Have you tried to use mailx?

mailx -a /path/to/file email@address

regards.
0
 

Accepted Solution

by:
davidiwharper earned 0 total points
ID: 39778611
Thanks for that suggestion. I had tried MailX first up but it failed to work as expected. From memory the email server at the receiving end couldn't process the headers correctly, either that or the gzip file was corrupt (it's been a long day).

Anyway I have written a little Perl script which can be called from the shell script. Finally managed to find sample code (combination of http://www.perlmonks.org/?node_id=19430 and http://www.perlmonks.org/?node_id=139965) which actually worked, more or less.

#!/usr/bin/perl

use MIME::Lite;
use File::MimeInfo;

# record the command line parameters
my $to_name = $ARGV[0] || "admin";
my $to_domain = $ARGV[1] || "mydomain.com";
my $content = $ARGV[2] || "Default Test";
my $file = $ARGV[3] || "/etc/lsb_release";

# now do some calculations
my @fileArray = split(/\//, $file);
my $filename = $fileArray[-1];
my $mimetype = mimetype($file);
my $to = $to_name."\@".$to_domain;

if ($to_name eq "--help" || $to_name eq "-h") {
	print "\n";
	print "Basic file emailer\n";
	print "\n";
	print "Usage: $0 [user] [domain] [desc] [file]\n";
	print "\n";
	print "to:     account to send the file to\n";
	print "domain: domain name where the account resides\n";
	print "desc:   description of the file\n";
	print "file:   file to email\n";
	print "\n";
	print "Example: $0 rupert debian.org Sample /etc/debian_version\n";
	print "\n";
	exit 0;
}

$from = "no-reply\@someplace.net";
$Subject = $content." Backup";

# Part using which the attachment is sent to an email #
$msg = MIME::Lite->new(
        From     => $from,
        To       => $to,
        Subject  => $Subject,
        Type     => 'multipart/Mixed',
);
$msg->attach(
      Type     => 'TEXT', 
      Data     => "Please find the $content backup attached."
);
$msg->attach(
      Type     => $mimetype,
      Path     => $file,
      Filename => $filename,
      Disposition => 'attachment'
);

$msg->send; # send via default
print "Mail with $filename sent!\n";

Open in new window

0
 

Author Closing Comment

by:davidiwharper
ID: 39790483
Well you know the old saying, if you want it done right do it yourself. Tried a bunch of sendmail/mail/mailx variants, plus a python script, and finally hit the jackpot with Perl.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Over the years I've spent many an hour playing on hardened, DMZ'd servers, with only a sub-set of the usual GNU toy's to keep me company; frequently I've needed to save and send log or data extracts from these server back to my PC, or to others, and…
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

706 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

14 Experts available now in Live!

Get 1:1 Help Now