LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices

Reply
 
Search this Thread
Old 06-02-2005, 08:06 PM   #1
RonnyNussbaum
LQ Newbie
 
Registered: Jun 2005
Posts: 17

Rep: Reputation: 0
Question Sending an attachment from a command line


Hello,
Forgive me if this is the wrong forum.

I'm running Fedora Core 3, and have Postfix running on it.
Postfix is configured to forward any message to root@mydomain.com, to my e-mail (ronny@mydomain.com).

I used the "mail" command-line application, and sent myself a text file:

cat text_file |/bin/mail -s "this is a test" root

This sent the file "text_file" to me, and I got it pasted into the message body. However, I'm trying to make it appear as an attachment.

My other requirement is that I want to use only the command line only, no GUI.

Can I use Postfix to send it (instead of "mail")?
Or maybe I can use "mail" somehow, but still have it appear as an attachment.

I appreciate any help.

Thanks

-Ronny
 
Old 06-02-2005, 08:20 PM   #2
kencaz
Senior Member
 
Registered: Mar 2005
Location: Las Vegas, NV
Distribution: Mandriva Slackware FreeBSD
Posts: 1,468

Rep: Reputation: 47
I don't think "mail" will handle true mime type attachments. You may want to look into "Mutt"

KC
 
Old 06-02-2005, 08:27 PM   #3
RonnyNussbaum
LQ Newbie
 
Registered: Jun 2005
Posts: 17

Original Poster
Rep: Reputation: 0
Thanks for your reply.
I was able to find a cool Perl script that is very simple, and does everything I wanted, so I wanted to post it here.

It's taken from here: https://www.redhat.com/archives/fedo.../msg02178.html

And also pasted below.

Thanks

-Ronny

#!/usr/bin/perl -w
#
# simple program to send a text message with an attachment
#
# William Julien
###

use strict; # turn on the strict pragma
use Getopt::Std; # use standard getopt to parse options

$/ = undef; # turn on slurp mode

#
# scope local variables
#
my ($date, # the current date
$sendmail, # the sendmail command
$document, # the document text
%options, # program options
$filename, # a file to send
$file, # the content of a file
@filelist, # list files to send
$tolist, # list of comma separated email addresses
$today, # today's date
$usage, # program usage
$man, # program man page
$subject, # email subject
$extent, # file extension
%mime_type, # list of supported application context mime types
$mime_type, # current mime application content type
);

#
# variable initializations
#
$man = <<MAN;
NAME
mime_mail

DISCRIPTION
This program is a command line mailer that sends a text file
description and one or more files in mime attachments. On success,
mime_mail will work silently.

SYNOPSIS
mime_mail -h | -d document.txt -s "mail subject"
-u "user1 [,user2 ...]" -f "file1 [,file2 ...]"

OPTIONS
-h
Displays a man page.

-d document.txt
Sends "document.txt" in the mail message body. The file must
be a normal text file. If a "-" is provided as an argument,
the input will be taken from stdin. If no "-d" argument is
supplied, the attachment will be sent with the message
"Please see the attached.".

-s "mail subject"
Sets the mail subject. The mail subject must be quoted if the
subject contains any spaces.

-u user1,user2 ...
A comma separated list of email addresses or aliases. Quotes
are required if the list contains any spaces.

-f file1,file2 ...
A comma separaed list of files to attach. Quotes are required
if the list contains any spaces. The files can be of any type.
The recognised application content types are ".doc", ".html",
,"htm",".xls", ".csv", ".pdf", ".rtf", and ".mdb".

William Julien
425-865-5511
MAN
$usage = <<USAGE;

Usage:
mime_mail -h | -d document.txt -s "mail subject"
-u "user1 [,user2 ...]" -f "file1 [,file2 ...]"
USAGE
$sendmail = "/usr/sbin/sendmail";
if ( ! -f $sendmail ) {
die "Sorry -- cannot locate sendmail at $sendmail.\nYou must edit $0\n";
}
$today = scalar(localtime());
%mime_type = ("doc" => "application/msword",
"txt" => "text/plain",
"htm" => "text/html",
"html" => "text/html",
"xls" => "application/vnd.ms-excel",
"csv" => "application/octet-stream",
"pdf" => "application/acrobat",
"rtf" => "application/rtf",
"mdb" => "application/vnd.ms-access",
);

#
# process program options
#
getopts('d:s:u:f:h', \%options) or die "$usage\n";
if ( defined $options{"h"} ) {
print "$man\n";
exit;
}
if ( defined $options{"f"} ) {
@filelist = split /\,/,$options{"f"};
foreach $file (@filelist) {
if (! -f $file) {
die "$0: cannot open $file\n";
}
}
} else {
die "$usage\n";
}
if ( defined $options{"d"} ) {
$document = $options{"d"};
if ( $document eq "-" ) {
$document = <STDIN>;
} else {
if ( -f $document ) {
open F, "$document";
$document = <F>;
close F;
} else {
die "$0 error: cannot open $document\n";
}
}
} else {
$document = "Please see the attached.\n";
}
if ( defined $options{"s"} ) {
$subject = $options{"s"};
} else {
die "$usage\nError: No Subject\n";
}
if ( defined $options{"u"} ) {
$tolist = $options{"u"};
} else {
die "$usage\n$0 error: no subject specified\n";
}
open MAIL, "|$sendmail -t";
print MAIL <<HEADER;
To: $tolist
Subject: $subject
Date: $today
Mime-Version: 1.0
Content-Type: multipart/mixed;
boundary="ThisRandomString"

This mail was formatted with mime_mail (wmj)

--ThisRandomString
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

$document
HEADER
foreach $filename (@filelist) {
$extent = $filename =~ /\.(\w+)$/ ? "\L$1" : undef;
$mime_type = "application/octet-stream";
if ( defined $extent ) {
if ( defined $mime_type{"$extent"} ) {
$mime_type = $mime_type{"$extent"};
} else {
$mime_type = "application/octet-stream";
}
}
open F,"$filename";
$file = <F>;
close F;
$file = encode_base64($file);
print MAIL <<FILE;
--ThisRandomString
Content-Type: $mime_type; name="$filename"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="$filename"

$file
FILE
}
print MAIL "--ThisRandomString--\n";
#
# send the mail
#
close MAIL;

#
# fin
###

sub encode_base64
{
use integer;
my $res = "";
my $eol = $_[1];
$eol = "\n" unless defined $eol;
pos($_[0]) = 0; # ensure start at the beginning
while ($_[0] =~ /(.{1,45})/gs) {
$res .= substr(pack('u', $1), 1);
chop($res);
}
$res =~ tr|` -_|AA-Za-z0-9+/|; # `# help emacs
# fix padding at the end
my $padding = (3 - length($_[0]) % 3) % 3;
$res =~ s/.{$padding}$/'=' x $padding/e if $padding;
# break encoded string into lines of no more than 76 characters each
if (length $eol) {
$res =~ s/(.{1,76})/$1$eol/g;
}
return $res;
}
 
Old 06-03-2005, 04:00 AM   #4
JaBa
LQ Newbie
 
Registered: Oct 2004
Posts: 13

Rep: Reputation: 0
Install mutt (http://www.mutt.org)

And try this:

cat sometext.txt | mutt your-name -a /home/your-file-to-send -s "<Yoursubject>"

The 'your-name' from above has to be set in the '/etc/aliases' file: (i am not sure of this for Fedora):

your-name: youremail@domain.com

That's all !

Greetz.
JaBa
 
Old 06-03-2005, 05:47 AM   #5
tronayne
Senior Member
 
Registered: Oct 2003
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,037

Rep: Reputation: 756Reputation: 756Reputation: 756Reputation: 756Reputation: 756Reputation: 756Reputation: 756
If you have POSIX-compliant mailx, this little script will do the job (the file name will be in the subject line):

#!/bin/ksh
cat ${1} | uuencode "${1}" | mailx -s ${1} you@where.what

Try it. If the file you give as an argument arrives as an attachment, you have a POSIX-compliant mailx program. If it arrives as text body, you don't so you can

echo "Here it is" | mailx -s "subject" -a filename you@where.what

You need the echo so you don't have to enter a control-D to end the message and "filename" is the name of the file you want attached.

Hope this helps.
 
Old 06-03-2005, 06:56 AM   #6
RonnyNussbaum
LQ Newbie
 
Registered: Jun 2005
Posts: 17

Original Poster
Rep: Reputation: 0
Thank you both, however, the script that I provided works perfectly for me without installing a thing

-Ronny
 
Old 06-05-2005, 11:16 PM   #7
goeko
LQ Newbie
 
Registered: Mar 2005
Location: A cold place
Distribution: Debian Mostly
Posts: 4

Rep: Reputation: 0
Lightbulb Another way to get this done.

Just to point out another way to get this done, if for some reason someone chooses not to use your fine script Ronny

I ran across this problem emailing html web reports, and didn't want to create my own script because I didn't know the requirements for mime headers good enough. So I found a tool called

mime-construct

which is in the debian distribution. And I have been using that, which is working great for me.

==>brian.
 
Old 06-06-2005, 05:32 AM   #8
nobody
LQ Newbie
 
Registered: May 2002
Distribution: SUSE 10.1, slackware 10.1, LFS 6.2
Posts: 10

Rep: Reputation: 0
As far as 1 know:
mail -a filename
should do the trick
 
Old 06-06-2005, 10:57 AM   #9
RonnyNussbaum
LQ Newbie
 
Registered: Jun 2005
Posts: 17

Original Poster
Rep: Reputation: 0
Thanks

Thanks for the advice goeko.

"nobody": Mail -a filename sends the file inside the message, as if you did a cut-paste of the contents of the file.

-RoNNY
 
Old 06-06-2005, 03:00 PM   #10
nobody
LQ Newbie
 
Registered: May 2002
Distribution: SUSE 10.1, slackware 10.1, LFS 6.2
Posts: 10

Rep: Reputation: 0
Thanks RoNNY, you are right. I'm using SUSE 8.x and this uses nail. But /usr/bin/mail is linked to nail and therefore everytime I type in 'mail' it starts nail.
So the correct thing would have been

nail -a filename
 
Old 06-01-2007, 03:57 PM   #11
Chiragrs
Member
 
Registered: May 2007
Distribution: Kubuntu
Posts: 43

Rep: Reputation: 15
Great Program

This Program is excellent.. I use it to send attachments all the time.. It works for all mail providers EXCEPT hotmail.. Do you know why this is? Is it a bug in the program or some setting in hotmail?? Please help as my primary email is in hotmail..
 
Old 06-02-2007, 04:09 AM   #12
nobody
LQ Newbie
 
Registered: May 2002
Distribution: SUSE 10.1, slackware 10.1, LFS 6.2
Posts: 10

Rep: Reputation: 0
Sory i dont know what the problem is.
I never used hotmail.
I normaly use gmx and there it works fine but it also worked with other mail providers as you also stated.
 
Old 06-04-2007, 11:47 AM   #13
Chiragrs
Member
 
Registered: May 2007
Distribution: Kubuntu
Posts: 43

Rep: Reputation: 15
Here's another excellent perl script that allows you to do more. Check it out.. And I tested it.. it works for hotmail, gmail and yahoo!

http://caspian.dotconf.net/menu/Software/SendEmail/
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
command-line email sending ixion Linux - Software 9 08-01-2008 06:46 AM
sending mail from command line won't work disorderly Linux - General 2 06-09-2005 11:29 AM
command-line for sending email with attachment? enemorales Linux - Software 3 10-06-2004 08:04 AM
sending msgs thru command line citrus Linux - Software 3 04-13-2004 02:39 AM
Sending Email's from command line jlaruffa Linux - Newbie 0 06-04-2003 03:31 PM


All times are GMT -5. The time now is 02:29 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration