| | |
Mail command
Please support our Shell Scripting advertiser: Programming Forums - DaniWeb Sister Site
![]() |
sendmail does to include subject. You simply need to have a "Subject: ldsfgfdg" line in the text.
Java Programmer and Sun Systems Administrator
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
•
•
Join Date: May 2004
Posts: 178
Reputation:
Solved Threads: 10
Use
Shell Scripting Syntax (Toggle Plain Text)
mailx -r '<from name>' -s '<subject here>' john@somewhere.com < message_text_file
Maybe this is just self-promotion, and then again, maybe it will help you. Here is a perl script I have written for sending mail with attachments as a command line utility for unix. It uses the GetOpt:
TD, Net:
omain, and MIME::Base64 Modules, all of which should be in the standard Perl distribution. It sends a multipart mime message using sendmail (with a subject). It sets the From, To, CC, and BCC Addressees as well. You will almost definately need to change the top line to point to your perl command.
TD, Net:
omain, and MIME::Base64 Modules, all of which should be in the standard Perl distribution. It sends a multipart mime message using sendmail (with a subject). It sets the From, To, CC, and BCC Addressees as well. You will almost definately need to change the top line to point to your perl command. Shell Scripting Syntax (Toggle Plain Text)
#!/usr/bin/perl -w ######################################################################### # # # Name: maila # # # # Author: Martin L. Simons # # # # Usage: maila [-a <attach,...>] [-b <bcc,...>] [-c <cc,...>] # # [-f <from>] [-h] [-t <text file>] [-T <Text string>] # # [-r] [-s <subj>] [-p <path>] <Addressee> [<Addressee>] # # Options: # # -a comma seperated list of files to send as attachments. # # -b comma seperated list of blind carbon copy recipients. # # -c comma seperated list of carbon copy recipients. # # -f full address for the "From:" line (Default: User@Host.Domain). # # -h print this message. # # -p Full path to sendmail command (Default: /usr/lib/sendmail). # # -r set the official replier. # # -s subject (Default: Mail from "From Addressee"). # # -t name of file containing text for mail (without this or the -T # # option the message will be typed until a "." by itself). # # -T a text string to include as the body of the message (see -t). # # Arguments: # # <Addressee> The "To:" Addressees. # # # # Pupose: To eable the mailing of attachments from a unix machine. # # # # Exit Codes: # # # # 0 -- Everything was successful # # 1 -- Invalid Options # # 2 -- Both option -t and -T were provided # # 3 -- No "To:" addressees provided # # 4 -- sendmail not found or not executable # # 5 -- sendmail error. # # # ######################################################################### ######################################################################### # # # BEGIN subroutine # # Arguments: None # # Purpose: Set the script name and directory variables immediately # # # ######################################################################### sub BEGIN { use File::Basename; $main::me = basename($0); $main::libdir = dirname($0); } # # use strict and lib and declare global variables. # use strict; use lib "$main::libdir"; use vars qw($opt_a $opt_b $opt_c $opt_f $opt_h $opt_p $opt_r $opt_s $opt_t $opt_T); # # include needed modules # use Getopt::Std; use MIME::Base64; use Net::Domain qw(hostname hostfqdn hostdomain); require Sys::Hostname; # # set the path # $ENV{PATH} = $main::libdir . ":" . $ENV{PATH} . ":/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin"; ######################################################################### # # # usage subroutine # # Arguments: Exit Code value # # Purpose: Print the usage message and exit with given exit code. # # # ######################################################################### sub usage ($) { my $rc = shift; my $sp = " " x length($main::me); print STDERR <<EOF; Usage: $main::me [-a <attach,attach,...>] [-b <bcc,bcc,...>] [-c <cc,cc,...>] $sp [-f <from>] [-h] [-t <text file>] [-T <Text string>] $sp [-r] [-s <subject>] [-p <sendmail path>] <Addressee> [<Addressee>] Options: -a comma seperated list of files to send as attachments. -b comma seperated list of blind carbon copy recipients. -c comma seperated list of carbon copy recipients. -f full address to appear on "From:" line (Default: User\@Host.Domain). -h print this message. -p Full path to sendmail command (Default: /usr/lib/sendmail). -r set the official replier (must be previleged sendmail user or root). -s subject (Default: Mail from "From Addressee"). -t name of file containing body text for mail (without this or the -T option the body of the message will be typed until a "." by itself). -T a text string to include as the body of the message (see -t). Arguments: <Addressee> The "To:" Addressees. EOF exit($rc); } # # Process Options # &usage(1) unless (getopts('a:b:c:f:hp:rs:t:T:')); # # check for -t -T # if ((defined($opt_t)) && (defined($opt_T))) { print STDERR "Options <t> and <T> are mutually exclusive.\n"; exit(2); } # # -h # &usage(0) if (defined($opt_h)); # # check for "To:" Addressees # &usage(3) unless (scalar(@ARGV) > 0); # # Check sendmail # my $sendmail = ((defined($opt_p)) ? $opt_p : "/usr/lib/sendmail"); unless (-x $sendmail) { print STDERR "Cannot find $sendmail."; print STDERR " Try defining it with -p.\n" unless (defined($opt_p)); print STDERR " Check the entered path again.\n" if (defined($opt_p)); exit(4); } # # create a search path from CDPATH,cdpath,PATH # my @dirs = (); push(@dirs, split(/:/, $ENV{CDPATH})) if (defined($ENV{CDPATH})); push(@dirs, split(/:/, $ENV{cdpath})) if (defined($ENV{cdpath})); push(@dirs, split(/:/, $ENV{PATH})); # # Search for the provided file. # my @files = (); if (defined($opt_a)) { foreach my $file (split(/,/, $opt_a)) { if (-f $file) { push(@files, $file); } else { my $temp = $file; foreach my $dir (@dirs) { if ((defined($dir)) && (-f $dir . "/" . $file)) { push(@files, $dir . "/" . $file); $temp = $dir . "/" . $file; last; } } print STDERR "WARNING: Cannot fine $file, skipping.\n" if ($file eq $temp); } } } # # Set the carbon copy lists. # my @bcc = split(/,/, $opt_b) if (defined($opt_b)); my @cc = split(/,/, $opt_c) if (defined($opt_c)); # # Determine the from addressee # my $from = ((defined($opt_f)) ? $opt_f : undef); unless (defined($from)) { my $host = hostfqdn(); unless (defined($host)) { $host = hostname(); my $domain = `domainname`; if (defined($domain)) { chomp($domain); $host .= (($domain !~ m/^\s*$/) ? "." . $domain : ""); } } my $user = (getpwuid($<))[0]; unless (defined($user)) { if (defined($ENV{USER})) { $user = $ENV{USER}; } else { $user = `who am i`; chomp($user); $user = (split(/\s+/, $user))[0]; } } $from = $user . '@' . $host; } # # Determine sendmail options and the subject. # my $opts = ((defined($opt_r)) ? qq[-t -r "$from"] : "-t "); my $subj = ((defined($opt_s)) ? $opt_s : qq[Mail from $from]); # # Print a warning if the mail will be empty of text. # if (defined($opt_t)) { if ((! -f $opt_t) || (-z $opt_t)) { print STDERR "WARNING: $opt_t is empty or does not exist."; print STDERR " Mail will contain no text.\n"; $opt_t = "User Entered Empty File"; } } # # Execute sendmail # unless (open(MAIL, "| $sendmail $opts")) { print STDERR "Could not execute $sendmail: $!\n"; exit(5); } # # Print the addresses and subject # print MAIL "From: $from\n"; print MAIL "To: " . join(", ", @ARGV) . "\n"; print MAIL "Cc: " . join(", ", @cc) . "\n"; print MAIL "Bcc: " . join(", ", @bcc) . "\n"; print MAIL "Subject: $subj\n"; # # Make it a multpart message if needed # if (scalar(@files) > 0) { print MAIL "Mime-Version: 1.0\n"; print MAIL "Content-Type: multipart/mixed; BOUNDARY=A_Mail_SCR_0\n\n"; print MAIL "--A_Mail_SCR_0\n"; print MAIL "Content-Type: text/plain; charset=us-ascii\n\n"; } else { print MAIL "Content-Type: text/plain; charset=us-ascii\n\n"; } # # retreive mail text either "string", text file, or STDIN # if (defined($opt_T)) { print MAIL "$opt_T\n\n\n\n\n\n\n"; } elsif (defined($opt_t)) { unless ($opt_t eq "User Entered Empty File") { if (open(FILE, "<$opt_t")) { my $orig = $/; $/ = undef; print MAIL <FILE>; close(FILE); $/ = $orig; print MAIL "\n\n\n\n\n\n"; } else { print STDERR "WARNING: Could not open $opt_t."; print STDERR " Mail will contain no text\n"; } } } else { print STDOUT "\nNow enter your text body ending with a line containing\n"; print STDOUT qq[either a ".", "EOF", or "EOT" on a line by itself.\n\n]; while(<STDIN>) { chomp; if (m/^(\.|EOF|EOT)$/) { last; } print MAIL $_, "\n"; } print MAIL "\n\n\n\n\n\n"; } # # Add each attachment. Read file in 57 byte blocks to ensure 76 char # base64 encode lines. # foreach my $file (@files) { unless (open(FILE, $file)) { print STDERR "Could not open $file: $!\n"; next; } my $temp = `file $file`; chomp $temp; $temp =~ s/^\s*[^:]*:\s+//; print MAIL "\n--A_Mail_SCR_0\n"; print MAIL "Content-Type: APPLICATION/octet-stream; name=", basename($file); print MAIL "\nContent-Transfer-Encoding: BASE64\n"; print MAIL "Content-Description: ", basename($file), " (", $temp, ")\n\n"; my $read = "\0" x 57; while (read(FILE, $read, 60*57)) { print MAIL encode_base64($read); } close(FILE); } # # Inform user and exit. # print STDOUT "\nMail Sent.\n"; exit(0);
Java Programmer and Sun Systems Administrator
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
•
•
Join Date: Sep 2006
Posts: 30
Reputation:
Solved Threads: 1
Set the variables as needed first, then:
Shell Scripting Syntax (Toggle Plain Text)
( cat <<! From: $FROM Subject: $SUBJ To: $TO ! [ "$CC" ] && echo "Cc: $CC" echo cat $BODY ) | sendmail
![]() |
Similar Threads
- Bash Mail script help (Shell Scripting)
- usage of the MAIL command in shell scripts (Shell Scripting)
- Mail command in shell scripts (Shell Scripting)
- Script use command mail (Shell Scripting)
- Problems in Dynaform (Form Mail) URGENT HELP (PHP)
Other Threads in the Shell Scripting Forum
- Previous Thread: Replace text in a file
- Next Thread: string parser
| Thread Tools | Search this Thread |






