How to send simple text file as attachment using HP-UX shell script? - email

I need to send an email with a text file as attachment using shell script in HP-UX; I don't have mutt installed.
I am using following command but it sends the file content in body of email, I want it as an attachment.
mailx -s "Report" name#example.com < file.txt

I wrote this ksh function a few years ago
# usage: email_attachment to cc subject body attachment_filename
email_attachment() {
to="$1"
cc="$2"
subject="$3"
body="$4"
filename="${5:-''}"
boundary="_====_blah_====_$(date +%Y%m%d%H%M%S)_====_"
{
print -- "To: $to"
print -- "Cc: $cc"
print -- "Subject: $subject"
print -- "Content-Type: multipart/mixed; boundary=\"$boundary\""
print -- "Mime-Version: 1.0"
print -- ""
print -- "This is a multi-part message in MIME format."
print -- ""
print -- "--$boundary"
print -- "Content-Type: text/plain; charset=ISO-8859-1"
print -- ""
print -- "$body"
print -- ""
if [[ -n "$filename" && -f "$filename" && -r "$filename" ]]; then
print -- "--$boundary"
print -- "Content-Transfer-Encoding: base64"
print -- "Content-Type: application/octet-stream; name=$filename"
print -- "Content-Disposition: attachment; filename=$filename"
print -- ""
print -- "$(perl -MMIME::Base64 -e 'undef $/; open $fh, shift; print MIME::Base64::encode(<$fh>); close $fh; ' $filename)"
print -- ""
fi
print -- "--${boundary}--"
} | /usr/lib/sendmail -oi -t
}

uuencode is your friend.
Here is a tested example:
(uuencode .vimrc vimrc.txt; uuencode .zshrc zshrc.txt; echo Here are your attachments) | mailx -s 'Mail with attachments' email_address

I was having the same problem where the output of uuencode was being sent as part of the message body rather than as an attached file (at least when using Outlook 2010 to view the sent mail). I found the answer in this thread http://www.unix.com/hp-ux/41306-sending-attachments-through-mailx.html
Adding -m causes mailx to not add MIME header lines when sending email. The OP's command would be altered to look like:
mailx -m -s "Report" name#example.com < file.txt

I also encountered the same problem few months ago.
The command I needed was ux2dos
( cat message_content_file; ux2dos file.txt | uuencode file.txt file.txt ) | mailx -m -s "subject" -r mail#sender mail#recipient
I hope it can help !
Regards

Related

I have a some scritps is working but not work in crontab

I have a some scripts and, when ı was run manually the scripts were run. But
When working in crontab, the format is incorrect.
This is for a new Linux server
awk 'BEGIN{
FS="-"
print "<HTML>""<table border="1" border="3" cellpadding="4" cellspacing="4" bgcolor=lightblue><TH>Firma</TH><TH>Charged Party No</TH><TH>Pcom Status</TH>"
}
{
printf "<TR>"
for(i=1;i<=NF;i++)
printf "<TD>%s</TD>",$i
print "</TR>"
}
END{
print "</TABLE></BODY></HTML>"
}
' /app/ovocontrol/cp_not_found2.txt > file.html
sed -i "s/failure/<font color="red">failure<\/font>/g;s/success/<font color="green">success<\/font>/g" file.html
(
echo "To: **********"
echo "Subject: Son 10 Dakikaya ait Toplu SMS CUDB Hata Detayi"
echo "Content-Type: text/html"
echo
cat file.html
echo
) | /usr/sbin/sendmail -t
I have a some scripts and, when ı was run manually the scripts were run. But
When working in crontab, the format is incorrect.
You don't have a shebang, nor do you have a complete crontab, so I'm guessing at what you're actually doing. I suspect you are trying to call those multiple commands directly from your crontab, which is a terrible idea. Instead, put your multiple calls into a single script and invoke it from cron. eg, do something like:
$ cat > /path/to/script << 'EOF'
#!/bin/sh
: ${f:=/app/ovocontrol/cp_not_found2.txt}
{
echo "To: **********"
echo "Subject: Son 10 Dakikaya ait Toplu SMS CUDB Hata Detayi"
echo "Content-Type: text/html"
echo
printf '<HTML><table border="1" border="3" cellpadding="4" cellspacing="4"'
printf ' bgcolor=lightblue><TH>Firma</TH><TH>Charged Party No</TH><TH>Pcom Status</TH>\n'
awk -F - ' {
printf "<TR>"
for(i=1;i<=NF;i++) printf "<TD>%s</TD>",$i
print "</TR>"
}
' "$f"
printf '</TABLE></BODY></HTML>\n'
} \
| sed -e 's#failure#<font color="red">failure</font>#g' \
-e 's#success#<font color="green">success</font>#g'
| /usr/sbin/sendmail -t
EOF
$ chmod +x /path/to/script
$ printf 'i\n0 * * * * /path/to/script\n.\nw\nq\n' | EDITOR=ed crontab -e
Note that the last command above is not really a great idea, just an attempt to codify the directive to add /path/to/script to your crontab.
I solved problem other way , "awk '!seen[$0]++'" command incorrect my format. the code was actually a short portion of the code. I think crontab has a special settings.

How is it possible to use a special character (a star) in the "from" field using Perl's sendmail

I am using Perl's SENDMAIL to receive email from my website:
open(SENDMAIL, "|$sendmail") or die "Cannot open $sendmail: $!";
print SENDMAIL "From: $from\n";
print SENDMAIL "Subject: $subject\n";
print SENDMAIL "To: $to\n";
print SENDMAIL "Content-type: text/plain\n\n";
print SENDMAIL $message;
close(SENDMAIL);
And I would like to modify the $from variable to be something like this:
$from = "★ David Jones <david.jones#oozicle.com>"
What happens right now is that I just see the ampersand etc., and the star is not shown.
I know that it is possible to use special characters because I receive spam containing them.
Is it possible to do this using SENDMAIL?
use Encode qw( encode );
my $name = "\x{2605} David Jones";
my $addr = 'david.jones#oozicle.com';
my $from_header = encode('MIME-Header', $name) . ' <' . $addr . '>';

attachment displaying full path when emailed using perl script

My perl script emails attachment which displays full path (/abc/xyz/report.xls) in outlook email client but not in lotus notes email client.
environment: 2010 Outlook on windows 7 OS.
How do I make sure path is not displayed in the attachment?
Code Snippet:
&sendEmail(#subject, #message, $exception_report_xls);
sub sendEmail
{
my $subject = "#subject";
my $distlist = 'ayc#abc.com';
my $message ="\n\n\n#message \n";
my $send = system("\(echo \"$message\" ; uuencode \"$exception_report_xls\" \"$exception_report_xls\"\) | mailx -s \"$subject\" \"$distlist\"");
}
Appreciate any resonse!
Ramya.
You are writing the full path and filename of the XLS file in the source of the message. So, some mail clients will display the full path.
If you are wedded to using (antiquated) UUENCODE, try it this way instead:
sub Send_Email_UUENCODE {
local($to, $from, $subj, $message, $attach) = #_;
local($i,$filename,$cmd,$att,$mailprog);
$i=length($attach)-1;
while( (substr($attach,$i,1) cmp '/')!=0 && $i>0) {$i--;}
$filename=substr($attach,$i+1,length($attach)-$i-1);
if ($attach) {
$cmd="uuencode " . $attach . ' ' . $filename;
$att=`$cmd`;
}
$mailprog = "/usr/sbin/sendmail -f " . $from;
open (MAIL, "|$mailprog $to") || die "Can't open $mailprog!\n";
print MAIL "To: $to\n";
print MAIL "From: $from\n";
print MAIL "Subject: $subj\n";
print MAIL "$message\n";
if ($attach) {
print MAIL "$att\n";
}
close (MAIL);
}
But, better might be to use MIME encoding instead of UUENCODE.

socat blocking on stdin

I have a bash script as follow on an AIX host, myscript.sh:
MODE="$1"
if [ "$MODE" == "start" ]; then
socat -T100 -lf $LOGF -d -d -d -x TCP4-LISTEN:$LISTENINGPORT,bind=$LISTENINGADDR,reuseaddr,fork EXEC:"$0 proxy" &
PID=$!
echo $PID > $PIDFILE
echo "$0 $MODE started (pid=$PID)"
elif [ "$MODE" == "proxy" ]; then
cat - > $TMPFILE
# process $TMPFILE before the SSL connection.
cat $TMPFILE | socat -T 100 -lf $LOGF -d - OPENSSL:$HOST
rm -f $TMPFILE
Everything is fine when I run:
$ cat somefile | myscript.sh proxy | xxd
The problem raise when I connect to the socat listener with a test script:
my $file = $ARGV[0];
my $fsize = -s $file;
my $socket = IO::Socket::INET->new("127.0.0.1:$port")
or die "Couldn't connect to remote host: $!";
$socket->autoflush(1);
binmode($socket);
open (FILE,$file);
binmode(FILE);
my $buffer ;
while(sysread(FILE, $buffer, $blocksize)) {
print $socket $buffer ;
}
print "sent\n" ;
close (FILE) ;
my $answer = <$socket>;
if (defined($answer)) {
print $answer; # never reached
print "...\n" ;
} else {
die "connection reset by peer\n";
}
In myscript.sh, it blocks on the line:
cat - > $TMPFILE
In the test script, it blocks on the line:
my $answer = <$socket>;
At this point, the data has been received by the socat listener (checked with tcpdump).
However, when I Ctrl+c the test script before the socat timeout, the data goes through the pipe (i.e., the SSL server is eventually contacted).
What am I doing wrong?
Update:
Thanks for the tips about cat and EOF. For the time being, I have worked around the problem like so:
timeout 0.2 cat -u - > $TMPFILE 2>>/dev/null
# process $TMPFILE before the SSL connection.
cat $TMPFILE | socat -T 100 -lf $LOGF -d - OPENSSL:$HOST
It's ugly, and a waste 0.2 seconds, I hope to find a better solution.
But it does the job for now. The 2>>/dev/null part is because AIX complains about an invalid counter (related to the timeout command).
My first thought is that there is no linefeed in the data you're trying to receive with cat - or <STDIN> . Both commands in their default behavior will return data once they have a linefeed or their buffers of the file-descriptor is full (4KB by default in Linux).

Why can't I pipe the output of uuencode to mailx in a single Perl open statement?

Here's my code that is not working:
print "To: "; my $to=<>; chomp $to;
print "From: "; my $from=<>; chomp $from;
print "Attach: "; my $attach=<>; chomp $attach;
print "Subject: "; my $subject=<>; chomp $subject;
print "Message: "; my $message=<>; chomp $message;
my $mail_fh = \*MAIL;
open $mail_fh, "uuencode $attach $attach |mailx -m -s \"$subject\" -r $from $to";
print $mail_fh $message;
close($mail_fh);
The mailx command works fine off the command line, but not in this Perl script context.
Any idea what I'm missing?
I suspect that this line's format/syntax:
open $mail_fh, "uuencode $attach $attach |mailx -m -s \"$subject\" -r $from $to";
is the culprit.
You just need an extra | at the beginning:
open $mail_fh, "|uuencode $attach $attach |mailx -m -s \"$subject\" -r $from $to";
Do you really want to use external binaries for either the uuencode or the mailx bit? UUencode is almost trivial with pack.
There are other ways to send mail. See the How do I send mail? in perlfaq9.