<!-- send an email .. -->
<html><head><title>Email sent</title>

<script type="text/javascript">

function check1(i0) {
var aa,nerr,j,aline,a2,amess,acode,acode1,stuff,stuffs,i1,i2;

i1=document.getElementById('status1');
stuff=i1.innerHTML ;
stuffs=stuff.split('\n');
aa='';
nerr=0;

for (var j=0;j<stuffs.length;j++) {
  aline=stuffs[j];
  a2=aline.split(':');
  amess=strip(a2[0]).toUpperCase();
  if (amess=='ERROR') {
     aa+='<li> Server Error: '+aline+'</li> \n';
     nerr++;
  }
  if (amess!=='DEBUG') continue ;
  amess=strip(a2[1]).toUpperCase();
  if (amess!=='RECV') continue;
  acode=strip(a2[2]).toUpperCase();
  acode1=acode.substr(0,1);
  if (acode1=='5') {               // an error!
      aa+='<li> SMTP Error: '+aline+'</li> \n';
      nerr++;
  }
}

if (nerr!==0) {
  i2=document.getElementById('status2');
  i2.style.display='block';
  i2.innerHTML='<b>'+nerr+' errors were encountered -- you can use the `Show email status details` link for the details </b> '
  i2.innerHTML=i2.innerHTML+'<ul>'+aa+'</ul>';
}

return 0 ;

}


function strip(avar) {
        return avar.replace(/^\s+|\s+$/g, '');
}
</script>

</head>

<body onLoad="check1(1)">
<a href="/dsurvey/admin.htm">Admin</a><p/>
<a href="#" onClick="document.getElementById('status1').style.display='block';return false">Show email status details ...</a>
<div id="status1" style="border:1px solid gray;margin:10px 5% 10px 5%;white-space:pre;display:none">

<?php
// Send an email

require("phpVars.php");

include("Mail.php");

$a=doit1(1,$hdrMysql);


function doit1($a,$hdrMysql) {

$todo=extractRequestvar('TODO','','No TODO in call to doSmtpVals.php');
$sessName=extractRequestvar('SESSNAME','1','');
$aread=extractRequestvar('READ','0','');

// === connect to mysql databases
if ($aread=='1') {
  $amess=connectEmailDatabase($sessName);
  print $amess ;
  return 1 ;
}
// if here, sending an email
$link=mysql_connect('localhost','dsurveyAdmin','admin');
if (!$link) {

   $amess0='Error: Could not connect to mysql (with username=`dsurveyAdmin` and password=`admin`): ' . mysql_error();
//   $amess0=rawurlencode($amess0);
//   $amess='$message='.$amess0;
   print $amess0;
   return $amess0 ;
}


$mdata=$hdrMysql . 'SMTPS' ;
$qq=mysql_select_db($mdata, $link);

//  no SMTPS database
if (!$qq) {                                // no SMTP database -- create it.
   $amess0='Error: No SMTPS mysql database.  You can use the Set SMTP parameters option on the admin page';
//   $amess0=rawurlencode($amess0);
//   $amess='$message='.$amess0;
   print $amess0;
   return $amess0 ;
}

// look for '1' entry (default)
$sql="SELECT * FROM basic where sessName='1'" ;
$qq=mysql_query($sql,$link);
if (!$qq) {
       mysql_close($link);

       $amess0='Error:  can not to read BASIC table in SMTP database;   ' . $mdata . ': '  . mysql_error();
//       $amess0=rawurlencode($amess0);
  //     $amess='$message='.$amess0;
   print $amess0;
      return $amess0 ;
}
 $rowcount=mysql_num_rows($qq);
 if ($rowcount==0) {
       $amess0='Error: Default SMTP information has not been created. You can use the Set SMTP parameters option on the admin page.';
//       $amess0=rawurlencode($amess0);
//       $amess='$message='.$amess0;
       print $amess0;
       return $amess0 ;
 }

 $stuff='';
 while($row = mysql_fetch_array($qq)) {
       $smtpGateway=($row['smtpGateway']);
       $username=($row['username']);
       $password=($row['password']);
       $defaultfrom=($row['defaultFrom']);
       $smtpPort=($row['smtpPort']);
 }
 mysql_close($link);

 if (trim($smtpGateway)=='') {
       $amess0='Error: The SMTP gateway information has not been set. You can use the Set SMTP parameters option on the admin page.';
//       $amess0=rawurlencode($amess0);
//       $amess='$message='.$amess0;
       print $amess0 ;
       return ;
 }
 if (trim($smtpPort)=='') {
       $amess0='Error: The SMTP port information has not been set. You can use the Set SMTP parameters option on the admin page.';
//       $amess0=rawurlencode($amess0);
//       $amess='$message='.$amess0;
       print $amess0 ;
       return ;
 }


//sendto sendfrom subject message
$sendto=trim(extractRequestvar('SENDTO','',''));
$sendfrom=trim(extractRequestvar('SENDFROM','',''));
$subject=extractRequestvar('SUBJECT','','');
$message=extractRequestvar('MESSAGE','','');
if (trim($sendto)=='') {
       $amess0='Error: You did not specify an address to send to ';
//       $amess0=rawurlencode($amess0);
//       $amess='$message='.$amess0;
       print $amess0 ;
       return ;
}
$nn = count(explode(" ",$sendto));
if ($nn>1) {
       $amess0='Error: Sorry, you can only send to one email address: '.$sendto ;
       print $amess0 ;
       return ;
}

if (!strpos($sendto,'@')) {
       $amess0='Error:  Bad To: email addreess: '.$sendto ;
       print $amess0 ;
       return ;
}

if (trim($sendfrom)=='') {
       $amess0='Error: Missing From: address ' ;
       print $amess0 ;
       return ;
}

/* mail setup recipients, subject etc */
$recipients = $sendto;
$headers["From"] = $sendfrom ;

//$headers["From"] = 'danielh@crosslink.net'; //$sendfrom;

$headers["Reply-to"] = $sendfrom ;

//$headers["Return-Path"] = $sendfrom ;   // overrides From:

$headers["To"] = $sendto ;
$headers["Subject"] = $subject;
$headers['Date'] = date("r") ;

//Sender address rejected: Access denied
$mailmsg = $message ;

/* SMTP server name, port, user/passwd */
$smtpinfo["host"] = $smtpGateway  ;
$smtpinfo["port"] = $smtpPort;


if (trim($username)=='' || trim($password)=='') {
  $smtpinfo["auth"] = false;
} else {
  $smtpinfo["auth"] = true;
  $smtpinfo["username"] = $username;
  $smtpinfo["password"] = $password;
}
$smtpinfo['debug'] = 'true';
//$smtpinfo["localhost"] = 'danielh@crosslink.net';

/* Create the mail object using the Mail::factory method */
$mail_object =& Mail::factory("smtp", $smtpinfo);

/* Ok send mail */
$mail_object->send($recipients, $headers, $mailmsg);

if (PEAR::isError($mail_object)) {
    echo(" Error: " . $mail->getMessage() . "</p>");
 } else {

 }

  $mail_object->disconnect();
}

// ========================================
// conect to a dsurvey session mysql database
function connectEmailDatabase($aname) {

global $svydir,$rootdir,$dsvydir,$wwwdir,$instruments_dir,$sessions_dir,$data_dir,$lookup_dir,$myScriptName;
global $pinfo_dir,$debug,$hdrMysql,$mysqlHdr,$currentDatabase,$tmpdir,$backup_dir;


$link=mysql_connect('localhost','dsurveyAdmin','admin');
if (!$link) {

   $amess0=' Could not connect to mysql (with username=`dsurveyAdmin` and password=`admin`): ' . mysql_error();
   $amess0=rawurlencode($amess0);
   $amess='Error:'.$amess0;
   return $amess ;
}

$mdata=$hdrMysql . 'SMTPS' ;
$qq=mysql_select_db($mdata, $link);
//  no such database, then create

if (!$qq) {                                // no SMTP database
     $amess0="Error: SMTP database not created. You can use the Set SMTP parameters option on the admin page.";
     $amess0=rawurlencode($amess0);
     $amess='$message='.$amess0;
    return $amess ;
}


// look for '1' entry (default)
$sql="SELECT * FROM basic where sessName='1'" ;
$qq=mysql_query($sql,$link);
if (!$qq) {
       mysql_close($link);
       $amess0='Unable to read BASIC table in SMTP database;   ' . $mdata . ': '  . mysql_error();
       $amess0=rawurlencode($amess0);
       $amess='Error:'.$amess0;
      return $amess ;
}
 $rowcount=mysql_num_rows($qq);
 if ($rowcount==0) {
       $amess0='Default SMTP information has not been created. You can use the Set SMTP parameters option on the admin page.';
       $amess0=rawurlencode($amess0);
       $amess='Error: '.$amess0;
       return $amess ;
 }

 $stuff='';
 while($row = mysql_fetch_array($qq)) {
       $a1=($row['smtpGateway']);
       $stuff=$stuff . "GATEWAY=$a1 ";
       $a1=($row['username']);
       $stuff=$stuff . ", USER=$a1 ";
       $a1=($row['password']);
       $stuff=$stuff . ", PWD=$a1 ";
       $a1=($row['defaultFrom']);
       $stuff=$stuff . ", DEFAULT_FROM=$a1 ";
       $stuff=$stuff . ", FILE=$mdata ";
 }
 mysql_close($link);
 return $stuff ;

}

// for later implementation: check for session specific smtp parameters

?>
</div>
<hr />
<div id="status2" style="background-color:lime;margin:10px 10% 10px 10%">
E-mail was succesfully sent!
</div>

<em>End of email-status report </em>
</body></html>