automactic email notification

automactic email notification

am 13.04.2011 17:23:41 von Chris Stinemetz

I have an all-in-one form which allows record edits, adds and deletes
in a mysql table.

I would like to add the ability to automatically send email update to
a distribution list whenever a record is edited, deleted, or added.

I won't be hosting my own mail server so instead I will be using remote SMTP.

The mail script I created works just fine I am just not sure how to
put this all together for the notifications when the table is altered.

Thank you in advance!

Mail Script:

require_once "Mail.php";

$from = "Someone ";
$to = "Someone Recipient ";
$subject = "Hi!";
$body = "Hi,\n\nHow are you?";

$host = "example.com";
$port = "587";
$username = "example";
$password = "example";

$headers = array ('From' => $from,
'To' => $to,
'Subject' => $subject);
$smtp = Mail::factory('smtp',
array ('host' => $host,
'port' => $port,
'auth' => true,
'username' => $username,
'password' => $password));

$mail = $smtp->send($to, $headers, $body);

if (PEAR::isError($mail)) {
echo("

" . $mail->getMessage() . "

");
} else {
echo("

Message successfully sent!

");
}
?>



Form Script:

"http://www.w3.org/TR/html4/strict.dtd">


ATC West Swap










West RF Engineering Dashboard









$objConnect = mysql_connect("localhost","cstine","cstine") or
die(mysql_error());
$objDB = mysql_select_db("atc");

//*** Add Condition ***//
if($_POST["hdnCmd"] == "Add")
{
$strSQL = "INSERT INTO lowmou ";
$strSQL .="(SiteID,NewSiterraSiteID,ATCSiteID,CricketRegion,Comments ,
Progress,willsitebedecom,relocationTowerVendor) ";
$strSQL .="VALUES ";
$strSQL .="('".$_POST["txtAddSiteID"]."','".$_POST["txtAddNewSiterra SiteID"]."'
";
$strSQL .=",'".$_POST["txtAddATCSiteID"]."' ";
$strSQL .=",'".$_POST["txtAddCricketRegion"]."','".$_POST["txtAddCom ments"]."'
";
$strSQL .=",'".$_POST["txtAddProgress"]."' ";
$strSQL .=",'".$_POST["txtAddwillsitebedecom"]."','".$_POST["txtAddr elocationTowerVendor"]."')
";
$objQuery = mysql_query($strSQL);

if(!$objQuery)
{
echo "Error Save [".mysql_error()."]";
}
//header("location:$_SERVER[PHP_SELF]");
//exit();
}

//*** Update Condition ***//
if($_POST["hdnCmd"] == "Update")
{
$strSQL = "UPDATE lowmou SET ";
$strSQL .="SiteID = '".$_POST["txtEditSiteID"]."' ";
$strSQL .=",NewSiterraSiteID = '".$_POST["txtEditNewSiterraSiteID"]."' ";
$strSQL .=",ATCSiteID = '".$_POST["txtEditATCSiteID"]."' ";
$strSQL .=",CricketRegion = '".$_POST["txtEditCricketRegion"]."' ";
$strSQL .=",Comments = '".$_POST["txtEditComments"]."' ";
$strSQL .=",Progress = '".$_POST["txtEditProgress"]."' ";
$strSQL .=",willsitebedecom = '".$_POST["txtEditwillsitebedecom"]."' ";
$strSQL .=",relocationTowerVendor =
'".$_POST["txtEditrelocationTowerVendor"]."' ";
$strSQL .="WHERE SiteID = '".$_POST["hdnEditSiteID"]."' ";
$objQuery = mysql_query($strSQL);
if(!$objQuery)
{
echo "Error Update [".mysql_error()."]";
}
//header("location:$_SERVER[PHP_SELF]");
//exit();
}

//*** Delete Condition ***//++++
if($_GET["Action"] == "Del")
{
$strSQL = "DELETE FROM lowmou ";
$strSQL .="WHERE SiteID = '".$_GET["srID"]."' ";
$objQuery = mysql_query($strSQL);
if(!$objQuery)
{
echo "Error Delete [".mysql_error()."]";
}
//header("location:$_SERVER[PHP_SELF]");
//exit();
}

$strSQL = "SELECT * FROM lowmou";
$objQuery = mysql_query($strSQL) or die ("Error Query [".$strSQL."]");
?>
">















while($objResult = mysql_fetch_array($objQuery))
{
?>

if($objResult["SiteID"] == $_GET["srID"] and $_GET["Action"] == "Edit")
{
?>











}
else
{
?>












}
?>
}
?>











SiteID NewSiterraSiteID ATCSiteID CricketRegion Comments Progress Will site be decom? Relocation Tower Vendor Edit Delete

value="">
value="">
value=""> value="">
name="txtEditCricketRegion" size="5"
value="">
size="25" value=""> size="25" value=""> size="5" value=""> name="txtEditrelocationTowerVendor" size="20"
value="">

OnClick="frmMain.hdnCmd.value='Update';frmMain.submit();">
OnClick="window.location='';">
href="?Action=Edit&srID=">Edit ?Action=Del&srID=';}">Delete
size="5">
name="txtAddCricketRegion" size="5">
name="txtAddwillsitebedecom" size="5">

OnClick="frmMain.hdnCmd.value='Add';frmMain.submit();">


mysql_close($objConnect);
?>



--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: automactic email notification

am 13.04.2011 18:08:04 von Jim Giner

Without looking at your HUGE code stream, I'm curious what your question
really is. If you know how to send an email, and you know how to update
your table, then isn't your task simply to send an email after updating the
table? I would make the email a function to get it out of the "way" and
then add a call to it right after I did an update. The user probably gets a
message of the successful update, so when you do that, that's when you call
your email function. Maybe have a couple of arguments in the function to
pass some specifics and that's that.



--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: Re: automactic email notification

am 13.04.2011 18:20:47 von Chris Stinemetz

On Wed, Apr 13, 2011 at 11:08 AM, Jim Giner
wrote:
> Without looking at your HUGE code stream, I'm curious what your question
> really is. =A0If you know how to send an email, and you know how to updat=
e
> your table, then isn't your task simply to send an email after updating t=
he
> table?

Yes.

>=A0I would make the email a function to get it out of the "way" and
> then add a call to it right after I did an update. =A0The user probably g=
ets a
> message of the successful update, so when you do that, that's when you ca=
ll
> your email function. =A0Maybe have a couple of arguments in the function =
to
> pass some specifics and that's that.
>

Could you possbily show me an example? I am a newby when it comes to PHP.
Thank you!


>
>
> --
> PHP Database Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>

--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: Re: automactic email notification

am 13.04.2011 18:37:36 von Jim Giner

Looking at all that code you already wrote, you're not that much of a
newbie.

Read up on functions. (A function is merely the same code you already run
but it's set aside, out of the main execution path of your script, so that
you can call it from anyplace that you need to execute that particular set
of code.) Put the function's code near the top of your script. Say you
named your function "SendAnEmail". Call it when you need by typing a line
that reads "SendAnEmail();".

You might want to read up on arguments when learning about functions. Then
you could have a couple of them in your function that would allow you to
pass specific info regarding the email you're sending, such as a timestamp
for when the email was triggered, who triggered perhaps, what record key was
changed. Things like that. Then you'd have a function header like:

function SendAnEmail($when,$who,$key)

and when you called the function you simply type

SendAnEmail($a,$b,$c) where $a is the datetime var you have for when it is,
$b is the user who did it, and $c is the key that you updated. In the
function those would be represented by the var names you use in the function
header and you can then put them in your email code where you want them to
show up. For ex., your email message might read like this:

"At $when an update was performed by $who to the record with the key of
$key."

That's enough to get you going - much more than I intended.

(rest deleted for brevity's sake :) )



--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php