Solved

Email a GZIP attachment in a bash script

Posted on 2014-01-13
3
677 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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Using 'screen' for session sharing, The Simple Edition Step 1: user starts session with command: screen Step 2: other user (logged in with same user account) connects with command: screen -x Done. Both users are connected to the same CLI sessio…
Background Still having to process all these year-end "csv" files received from all these sources (including Government entities), sometimes we have the need to examine the contents due to data error, etc... As a "Unix" shop, our only readily …
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

773 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