0

I am totally unfamiliar with bash scripting, however I came across the following script that php's sendmail routes through so I can trace any scripts that may be comprised by spammers.

This works great however the $PWD variable is not showing the file name, its only showing the file's working directory.

Bash script: /usr/local/bin/sendmail2

#!/bin/sh
# Logging sendmail wrapper

SENDMAIL="/usr/sbin/sendmail -t -i"
LOGFILE="/home/mail.log"

DT=`date "+%Y-%m-%d %H:%M:%S"`
DTFN=`date "+%Y%m%d-%H%M%S"`
#TMPFP=`tempfile --prefix=lsm_`
TMPFP=`mktemp`

cat | tee "$TMPFP" | $SENDMAIL $*
RETVAL=$?

TO=`grep "To:" <"$TMPFP"`
rm -f "$TMPFP"

echo "$DT: $PWD sent $TO" >> $LOGFILE

exit $RETVAL

Test script: /home/mysite/test.php:

<?php
$to = "my@email.com";
$subject = "Test mail";
$message = "Hello! This is a simple email message.";
$from = "my@email.com";
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
echo "Mail Sent.";
?>

php.ini:

sendmail_path = "/usr/local/bin/sendmail2"
Joe
  • 1,762
  • 9
  • 43
  • 60

1 Answers1

2

well, ${PWD} is short for "print working directory" (pwd is the command for that), and therefore it will give you only the working directory. that's by design.

also, the working directory doesn't necessarily have anything todo with the full path of the script (neither your sendmail2 script, nor the php-script that calls sendmail2).

umläute
  • 28,885
  • 9
  • 68
  • 122
  • ah shoot, was hoping there was a fix around this, no point in logging the emails as the folders have dozens of php scripts – Joe Feb 05 '13 at 17:26
  • Is it feasible for your php scripts to add an identifying parameter which your sendmail2 script can strip out before it calls sendmail? If it's the first parameter it's easy to shift it out of the arguments, if it is the last, it's only a little harder: [http://stackoverflow.com/questions/1215538/bash-extract-parameters-before-last-parameter-in](http://stackoverflow.com/questions/1215538/bash-extract-parameters-before-last-parameter-in) – William Feb 05 '13 at 18:35