Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PHP and sendmail???

Status
Not open for further replies.

tijerina

Vendor
Mar 4, 2003
132
US
I know how to send mail via sendmail, but I am lost as to where within my calendar script where I can place my sendmail script..

I have an interactive calendar that my users can schedule an appointment if they need services.

I would like to have my interactive calendar send me an email once a user submits their desired date and time for services.

The add event for the calendar is this:


-----add calendar event-----------------------

<head>
<title>My company Calendar: Add Event</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<LINK rel="stylesheet" type="text/css" name="style" href="../calendar.css">
</head>

<body bgcolor="#FFFFFF" text="#000000">

<BR><BR>
<center>
<form action="calAdd.php" method="post">
<table cellpadding="0" cellspacing="2" border="0" bgcolor="#000000"><tr><td>

<table cellpadding="3" cellspacing="0" border="0" bgcolor="#CC0000">
<tr><td><font class="addHead"><a href="../calendar.php" class="addHead">Calendar</a></font></TD></tr>
<tr><td>

<table cellpadding="3" cellspacing="0" border="0" bgcolor="#CCCCCC">
<tr>

<td width="95" align="right"><font class="AddLeft">Event</font></td>

<td width="405"><input type="text" name="event" maxlength="18"></td>
</tr>
<tr bgcolor="#E3E3E3">

<td width="95" align="right"><font class="AddLeft">Event<br>Description</font></td>

<td width="405"><TEXTAREA NAME="description" ROWS=6 COLS=40></TEXTAREA></td>
</tr>
<tr>

<td width="95" align="right"><font class="AddLeft">Date</font></td>

<td width="405" >
<?php

$day = getdate();
$current_year = $day['year'];
$month = $day['month'];
$today = $day['mday'];

echo "
<select name=\"month\">
<option value=\"January\"";
if($month=="January")
echo " selected ";
echo ">January</option>";

echo "<option value=\"February\"";
if($month=="February")
echo " selected ";
echo ">February</option>";

echo "<option value=\"March\"";
if($month=="March")
echo " selected ";
echo ">March</option>";

echo "<option value=\"April\"";
if($month=="April")
echo " selected ";
echo ">April</option>";

echo "<option value=\"May\"";
if($month=="May")
echo " selected ";
echo ">May</option>";

echo "<option value=\"June\"";
if($month=="June")
echo " selected ";
echo ">June</option>";

echo "<option value=\"July\"";
if($month=="July")
echo " selected ";
echo ">July</option>";

echo "<option value=\"August\"";
if($month=="August")
echo " selected ";
echo ">August</option>";

echo "<option value=\"September\"";
if($month=="September")
echo " selected ";
echo ">September</option>";

echo "<option value=\"October\"";
if($month=="October")
echo " selected ";
echo ">October</option>";

echo "<option value=\"November\"";
if($month=="November")
echo " selected ";
echo ">November</option>";

echo "<option value=\"December\"";
if($month=="December")
echo " selected ";
echo ">December</option>";
echo "</select>

<select name=\"day\">";

for($i = 1; $i <= 31; $i++){
echo "<option value=\"$i\"";
if($i == $today)
echo " selected ";
echo ">$i</option>
";
}
echo "</select>

<select name=\"year\">";



for($i = $current_year; $i < ($current_year + 5); $i++){
echo "<option value=\"$i\"";
if ($year == $i)
echo " selected ";
echo ">$i</option>
";
}

echo "</select>";
?>

</td>
</tr>
<tr bgcolor="#E3E3E3">

<td width="95" align="right"><font class="AddLeft">Submitted By</font></td>

<td width="405"><input type="text" name="submitted"></td>
</tr>
</table>
</td><tr>
<tr><td align="right"><input type="submit" value="Add Event"></td></tr>
</table>
</td></tr></table>
</form>
</center>


</body>
</html>

----end of add calendar event-----------------
The sendmail script that I would like to incorporate to the above is this:

----start of sendmail script------------------

<script>
if ($i==1) {
##NOTIFY THE INTERESTED PARTIES.
$server=`uname -n`;
chomp $server;
$stamp=`date`;
chomp $stamp;
$rc=`cat /tmp/incomplete_data | mailx -s "$server $stamp" scheduledappointments@mycompany.com`;
chomp $rc;
}
</script>

-----end of sendmail script --------------------

The calendar function works great, but I can't figure out where to put the sendmail script, or if anybody has another idea I could use to receive emails once the add event is triggered, that would help out tremendously..


Thanks..



 
Correction on my end, the file that I need to incorporate my sendmail script is this one:


-----add event script--------------

<html>
<head>
<title>Flat Calendar: Add</title>
<?php
if(ereg("[a-zA-Z0-9]",$event))
{
?>

<META HTTP-EQUIV="refresh" content="1;URL=../calendar.php">
<LINK rel="stylesheet" type="text/css" name="style" href="../calendar.css">
</head>

<body bgcolor="#FFFFFF" text="#000000">



<?php


//returns highest key in the database
function getMaxKey($db) {

$maxKey = 0;

$sortby = "event_key";
$result = $db->getall();

foreach($result as $item){
$key = $item["event_key"];
if($key > $maxKey)
$maxKey = $key;
}

return $maxKey;

}

// Include the FFDB library
include("../ffdb.inc.php");

//open db or create new db
$db = new FFDB();
if (!$db->open("../calendar"))
{
// Define the database shema.
// Note that the "last_name" field is our key.
$schema = array(
array("event_key", FFDB_INT, "key"),
array("event_name", FFDB_STRING),
array("event_description", FFDB_STRING),
array("event_submitted_by", FFDB_STRING),
array("event_month", FFDB_STRING),
array("event_day", FFDB_INT),
array("event_year", FFDB_INT)
);

// Try and create it...
if (!$db->create("calendar", $schema))
{
echo "Error creating database\n";
return;
}
}

//if no key file create a new one
if(!file_exists("key.dat"))
{
$newKey = getMaxKey($db);
$newFile = fopen("key.dat", "w") Or die("Can't open file");
fwrite($newFile,$newKey);
fclose($newFile);

}

//add a record
//convert forms to record
$fileread = fopen("key.dat", "r")Or die("Can't open file");
$data = (int) fread($fileread, 10);
fclose($fileread);
$data++;
$fileread = fopen("key.dat", "w") Or die("Can't open file");
fwrite($fileread,$data);
fclose($fileread);

//removes escape slashes
$event = stripslashes($event);
$description = stripslashes($description);
$submitted = stripslashes($submitted);

//add html entities
$event = htmlentities($event,ENT_QUOTES);
$submitted = htmlentities($submitted,ENT_QUOTES);

$record["event_key"] = $data;
$record["event_name"] = $event;
$record["event_description"] = $description;
$record["event_submitted_by"] = $submitted;
$record["event_month"] = $month;
list($record["event_day"]) = sscanf($day, "%d"); // string -> int
list($record["event_year"]) = sscanf($year, "%d"); // string -> int

// Add a _new_ entry
echo("");
if (!$db->add($record))
echo("failed!\n");
else {



//table to display after adding
$addedTable ="


<center><font class=\"back\">Record Added: taking you back</font> </center>
<table cellpadding=\"0\" cellspacing=\"2\" border=\"0\" bgcolor=\"#000000\" align=\"center\"><tr><td>
<table cellpadding=\"3\" cellspacing=\"0\" border=\"0\" bgcolor=\"#CC0000\" align=\"center\">
<tr><td><font class=\"addHead\"><a href=\"calendar.php\" class=\"addHead\">Calendar</a></font></TD></tr>
<tr><td>

<table cellpadding=\"3\" cellspacing=\"0\" border=\"0\" bgcolor=\"#CCCCCC\">

<tr>
<td width=\"150\" align=\"right\" height=\"26\"><font class=\"AddLeft\">Event : </font></td>
<td width=\"350\" height=\"26\"><font class=\"AddRight\">$event</font></td>
</tr>

<tr bgcolor=\"#E3E3E3\">
<td width=\"150\" align=\"right\"><font class=\"AddLeft\">Event Description : </font></td>
<td width=\"350\"><font class=\"AddRight\">$description</font></td>
</tr>

<tr>
<td width=\"150</h5>\" align=\"right\"><font class=\"AddLeft\">Date : </font></td>
<td width=\"350\" ><font class=\"AddRight\">$day $month $year</font></td>
</tr>

<tr bgcolor=\"#E3E3E3\">
<td width=\"150\" align=\"right\"><font class=\"AddLeft\">Submitted By : </font></td>
<td width=\"350\"><font class=\"AddRight\">$submitted</font></td>
</tr>
</table>
</td><tr>
<tr><td align=\"right\"></td></tr>
</table>
</td></tr></table>

";

echo $addedTable;
}
}
else {
echo "You must enter an event name. Please go back to the previous
page and do so.";
}
?>
</body>
</html>
--------end of add event script ----------------------

Thanks..
 
I have a sendmail script that I have used on another perl script that I created, so I figured I could just cut and paste it into my calendar script. Here is the sendmail script..

<script>
if ($i==1) {
##NOTIFY THE INTERESTED PARTIES.
$server=`uname -n`;
chomp $server;
$stamp=`date`;
chomp $stamp;
$rc=`cat /tmp/incomplete_data | mailx -s "$server $stamp" scheduledappointments@mycompany.com`;
chomp $rc;
}
</script>


Question: How do I incorporate the above sendmail script to the add event file. The add event file is the correction I submitted on the second post..

Thanks.

 
Thanks,

I did go your route but I am getting this crazy error, I am not sure if my "if" statement is correct. Can you please check it for me.. Here is the code with the mail option in it, it is marked in red..

What I am trying to do here is say: If $record is populated, then send me an email..

Do I even have the mail script in the right place?

--------start of code snipet----------------

<html>
<head>
<title>Flat Calendar: Add</title>
<?php
if(ereg("[a-zA-Z0-9]",$event))
{
?>

<META HTTP-EQUIV="refresh" content="1;URL=../calendar.php">
<LINK rel="stylesheet" type="text/css" name="style" href="../calendar.css">
</head>

<body bgcolor="#FFFFFF" text="#000000">



<?php


//returns highest key in the database
function getMaxKey($db) {

$maxKey = 0;

$sortby = "event_key";
$result = $db->getall();

foreach($result as $item){
$key = $item["event_key"];
if($key > $maxKey)
$maxKey = $key;
}

return $maxKey;

}

// Include the FFDB library
include("../ffdb.inc.php");

//open db or create new db
$db = new FFDB();
if (!$db->open("../calendar"))
{
// Define the database shema.
// Note that the "last_name" field is our key.
$schema = array(
array("event_key", FFDB_INT, "key"),
array("event_name", FFDB_STRING),
array("event_description", FFDB_STRING),
array("event_submitted_by", FFDB_STRING),
array("event_month", FFDB_STRING),
array("event_day", FFDB_INT),
array("event_year", FFDB_INT)
);

// Try and create it...
if (!$db->create("calendar", $schema))
{
echo "Error creating database\n";
return;
}
}

//if no key file create a new one
if(!file_exists("key.dat"))
{
$newKey = getMaxKey($db);
$newFile = fopen("key.dat", "w") Or die("Can't open file");
fwrite($newFile,$newKey);
fclose($newFile);

}

//add a record
//convert forms to record
$fileread = fopen("key.dat", "r")Or die("Can't open file");
$data = (int) fread($fileread, 10);
fclose($fileread);
$data++;
$fileread = fopen("key.dat", "w") Or die("Can't open file");
fwrite($fileread,$data);
fclose($fileread);

//removes escape slashes
$event = stripslashes($event);
$description = stripslashes($description);
$submitted = stripslashes($submitted);

//add html entities
$event = htmlentities($event,ENT_QUOTES);
$submitted = htmlentities($submitted,ENT_QUOTES);

$record["event_key"] = $data;
$record["event_name"] = $event;
$record["event_description"] = $description;
$record["event_submitted_by"] = $submitted;
$record["event_month"] = $month;
list($record["event_day"]) = sscanf($day, "%d"); // string -> int
list($record["event_year"]) = sscanf($year, "%d"); // string -> int


if ($record=1) {
##NOTIFY THE INTERESTED PARTIES.
$stamp=`date`;
chomp $stamp;
mail("scheduledappointment@mycompany.com", "You have a scheduled appoint at "Check it NOW!\n And don't forget to call the customer!\n $stamp);
}


// Add a _new_ entry
echo("");
if (!$db->add($record))
echo("failed!\n");
else {



//table to display after adding
$addedTable ="


<center><font class=\"back\">Record Added: taking you back</font> </center>
<table cellpadding=\"0\" cellspacing=\"2\" border=\"0\" bgcolor=\"#000000\" align=\"center\"><tr><td>
<table cellpadding=\"3\" cellspacing=\"0\" border=\"0\" bgcolor=\"#CC0000\" align=\"center\">
<tr><td><font class=\"addHead\"><a href=\"calendar.php\" class=\"addHead\">Calendar</a></font></TD></tr>
<tr><td>

<table cellpadding=\"3\" cellspacing=\"0\" border=\"0\" bgcolor=\"#CCCCCC\">

<tr>
<td width=\"150\" align=\"right\" height=\"26\"><font class=\"AddLeft\">Event : </font></td>
<td width=\"350\" height=\"26\"><font class=\"AddRight\">$event</font></td>
</tr>

<tr bgcolor=\"#E3E3E3\">
<td width=\"150\" align=\"right\"><font class=\"AddLeft\">Event Description : </font></td>
<td width=\"350\"><font class=\"AddRight\">$description</font></td>
</tr>

<tr>
<td width=\"150</h5>\" align=\"right\"><font class=\"AddLeft\">Date : </font></td>
<td width=\"350\" ><font class=\"AddRight\">$day $month $year</font></td>
</tr>

<tr bgcolor=\"#E3E3E3\">
<td width=\"150\" align=\"right\"><font class=\"AddLeft\">Submitted By : </font></td>
<td width=\"350\"><font class=\"AddRight\">$submitted</font></td>
</tr>
</table>
</td><tr>
<tr><td align=\"right\"></td></tr>
</table>
</td></tr></table>

";

echo $addedTable;
}
}
else {
echo "You must enter an event name. Please go back to the previous
page and do so.";
}
?>
</body>
</html>
--------end of code snipet------------------------
 
I am rushing and it is evident. Gang, I am so sorry for posting incomplete messages, I will be more careful...

Here is the error..


Parse error: parse error, unexpected T_VARIABLE in /home/virtual/site1/fst/var/ on line 105



Thanks..
 
Still hard to say. PHP reports a parse error at the line at which it realizes it is confused by your code, not at the line at which the error actually resides. Start at line 105 and work toward the beginning of your script looking for problems.



One thing I have noticed is your use of the "=" operator in this line:

if ($record=1) {

Is it your intention to assign the value of 1 to the variable $record? If not, I recommend that you use one of PHP's "==" or "===" comparison operators instead.



Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Just a note:
1. `date` is really not necessary. the date() function in PHP can be used without having to invoke a shell.

2. Also the if is wrong:
Code:
if($record = 1) ....
The single equal sign is an assignment, not a comparison operator. So, it will always be true since the assignment happens.

3. The error is due to:
Code:
 ...to call the customer!\n $stamp);
}
# missing closing double quote after $stamp
to call the customer!\n $stamp[COLOR=red]"[/color]);
}
 
Okay, I am trying something totally different. I was thinking, why don't I just put a generic mail script at the bottom of my php file and when the file is triggered (hence - when a user adds an appointment) it will then spawn the email.

I did just that and I am not getting an email.

Did I place this mail script in the correct place, the mail script is in read at the bottom of the code?

<html>
<head>
<title>Flat Calendar: Add</title>
<?php
if(ereg("[a-zA-Z0-9]",$event))
{
?>

<META HTTP-EQUIV="refresh" content="1;URL=../calendar.php">
<LINK rel="stylesheet" type="text/css" name="style" href="../calendar.css">
</head>

<body bgcolor="#FFFFFF" text="#000000">



<?php


//returns highest key in the database
function getMaxKey($db) {

$maxKey = 0;

$sortby = "event_key";
$result = $db->getall();

foreach($result as $item){
$key = $item["event_key"];
if($key > $maxKey)
$maxKey = $key;
}

return $maxKey;

}

// Include the FFDB library
include("../ffdb.inc.php");

//open db or create new db
$db = new FFDB();
if (!$db->open("../calendar"))
{
// Define the database shema.
// Note that the "last_name" field is our key.
$schema = array(
array("event_key", FFDB_INT, "key"),
array("event_name", FFDB_STRING),
array("event_description", FFDB_STRING),
array("event_submitted_by", FFDB_STRING),
array("event_month", FFDB_STRING),
array("event_day", FFDB_INT),
array("event_year", FFDB_INT)
);

// Try and create it...
if (!$db->create("calendar", $schema))
{
echo "Error creating database\n";
return;
}
}

//if no key file create a new one
if(!file_exists("key.dat"))
{
$newKey = getMaxKey($db);
$newFile = fopen("key.dat", "w") Or die("Can't open file");
fwrite($newFile,$newKey);
fclose($newFile);

}

//add a record
//convert forms to record
$fileread = fopen("key.dat", "r")Or die("Can't open file");
$data = (int) fread($fileread, 10);
fclose($fileread);
$data++;
$fileread = fopen("key.dat", "w") Or die("Can't open file");
fwrite($fileread,$data);
fclose($fileread);

//removes escape slashes
$event = stripslashes($event);
$description = stripslashes($description);
$submitted = stripslashes($submitted);

//add html entities
$event = htmlentities($event,ENT_QUOTES);
$submitted = htmlentities($submitted,ENT_QUOTES);

$record["event_key"] = $data;
$record["event_name"] = $event;
$record["event_description"] = $description;
$record["event_submitted_by"] = $submitted;
$record["event_month"] = $month;
list($record["event_day"]) = sscanf($day, "%d"); // string -> int
list($record["event_year"]) = sscanf($year, "%d"); // string -> int


// Add a _new_ entry
echo("");
if (!$db->add($record))
echo("failed!\n");
else {



//table to display after adding
$addedTable ="


<center><font class=\"back\">Record Added: taking you back</font> </center>
<table cellpadding=\"0\" cellspacing=\"2\" border=\"0\" bgcolor=\"#000000\" align=\"center\"><tr><td>
<table cellpadding=\"3\" cellspacing=\"0\" border=\"0\" bgcolor=\"#CC0000\" align=\"center\">
<tr><td><font class=\"addHead\"><a href=\"calendar.php\" class=\"addHead\">Calendar</a></font></TD></tr>
<tr><td>

<table cellpadding=\"3\" cellspacing=\"0\" border=\"0\" bgcolor=\"#CCCCCC\">

<tr>
<td width=\"150\" align=\"right\" height=\"26\"><font class=\"AddLeft\">Event : </font></td>
<td width=\"350\" height=\"26\"><font class=\"AddRight\">$event</font></td>
</tr>

<tr bgcolor=\"#E3E3E3\">
<td width=\"150\" align=\"right\"><font class=\"AddLeft\">Event Description : </font></td>
<td width=\"350\"><font class=\"AddRight\">$description</font></td>
</tr>

<tr>
<td width=\"150</h5>\" align=\"right\"><font class=\"AddLeft\">Date : </font></td>
<td width=\"350\" ><font class=\"AddRight\">$day $month $year</font></td>
</tr>

<tr bgcolor=\"#E3E3E3\">
<td width=\"150\" align=\"right\"><font class=\"AddLeft\">Submitted By : </font></td>
<td width=\"350\"><font class=\"AddRight\">$submitted</font></td>
</tr>
</table>
</td><tr>
<tr><td align=\"right\"></td></tr>
</table>
</td></tr></table>

";

echo $addedTable;
}
}
else {
echo "You must enter an event name. Please go back to the previous
page and do so.";
}

?>
</body>


<script>
mail("appointment@mycompany.com", "You have a scheduled appointment at "Check it NOW!\n And don't forget to call the customer!");
</script>


</html>

All I want this script to do is when this file is triggered and at the end of the script, it will read the mail script and send an email. I am not sure why I am not getting an email. The email address is correct.
 
<script> as a measure to trigger PHP execution? I haven't seen that every before - but maybe I am mistaken.
I recommend do replace the <script> and </script> tag with
<?php
....
?>
Then the PHP interpreter will be invoked and a mail will be sent.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top