Email a GZIP attachment in a bash script

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
davidiwharperAsked:
Who is Participating?
 
davidiwharperAuthor Commented:
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
 
Pepe2323Commented:
Hi

Have you tried to use mailx?

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

regards.
0
 
davidiwharperAuthor Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.