Solved

Email a GZIP attachment in a bash script

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

Don't miss ATEN at NAB Show April 24-27!

Visit ATEN at NAB Show to learn how our "Seamlessly Entertaining" solutions deliver fast, precise video streaming without delays for the broadcasting and media environment. ATEN will showcase its 16x16 Modular Matrix Switch (VM1600) and KVM Over IP Solution (KE6900 series).

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Run Secure WMI query from CentOS 5 127
linux SFTP 8 76
Linux mount of Windows Shared Now Fails 8 110
AWK: Pythagoras script 6 34
Note: for this to work properly you need to use a Cross-Over network cable. 1. Connect both servers S1 and S2 on the second network slots respectively. Note that you can use the 1st slots but usually these would be occupied by the Service Provide…
Google Drive is extremely cheap offsite storage, and it's even possible to get extra storage for free for two years.  You can use the free account 15GB, and if you have an Android device..when you install Google Drive for the first time it will give…
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.:
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

680 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