submit a record and send email

Discussion in 'PHP' started by akalehzan, Jun 21, 2012.

  1. #1
    Hi All,

    I have this PHP code that Inserts a record to mysql database.

    I was wondering if someone could help me to modify this code to send email upon submitting the record.
    send email and the user.

    here is the php code:
    ///////////////////////////////
    <?php require_once('Connections/psyc.php'); ?>
    <?php
    if (!function_exists("GetSQLValueString")) {
    function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
    {
    if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
    }

    $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

    switch ($theType) {
    case "text":
    $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
    break;
    case "long":
    case "int":
    $theValue = ($theValue != "") ? intval($theValue) : "NULL";
    break;
    case "double":
    $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
    break;
    case "date":
    $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
    break;
    case "defined":
    $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
    break;
    }
    return $theValue;
    }
    }

    $editFormAction = $_SERVER['PHP_SELF'];
    if (isset($_SERVER['QUERY_STRING'])) {
    $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
    }

    if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form2")) {
    $insertSQL = sprintf("INSERT INTO helpu (ID, date_created, sunetid, first_name, last_name, email, phone, Reported_issue) VALUES (%s, %s, %s, %s, %s, %s, %s, %s)",
    GetSQLValueString($_POST['ID'], "int"),
    GetSQLValueString($_POST['date_created'], "date"),
    GetSQLValueString($_POST['sunetid'], "text"),
    GetSQLValueString($_POST['first_name'], "text"),
    GetSQLValueString($_POST['last_name'], "text"),
    GetSQLValueString($_POST['email'], "text"),
    GetSQLValueString($_POST['phone'], "text"),
    GetSQLValueString($_POST['Reported_issue'], "text"));

    mysql_select_db($database_psyc, $psyc);
    $Result1 = mysql_query($insertSQL, $psyc) or die(mysql_error());
    }
    ?>
    <style type="text/css">
    .headin_1 {
    text-align: center;
    }
    .Tables {
    }
    </style>
    <div>
    <p>Helpu</p>
    <p>&nbsp;</p>
    <form action="<?php echo $editFormAction; ?>" method="post" name="form2" id="form2">
    <table align="center">
    <tr valign="baseline">
    <td nowrap="nowrap" align="right">ID:</td>
    <td><input type="text" name="ID" value="" size="32" /></td>
    </tr>
    <tr valign="baseline">
    <td nowrap="nowrap" align="right">Date_created:</td>
    <td><input type="text" name="date_created" value="" size="32" /></td>
    </tr>
    <tr valign="baseline">
    <td nowrap="nowrap" align="right">Sunetid:</td>
    <td><input type="text" name="sunetid" value="" size="32" /></td>
    </tr>
    <tr valign="baseline">
    <td nowrap="nowrap" align="right">First_name:</td>
    <td><input type="text" name="first_name" value="" size="32" /></td>
    </tr>
    <tr valign="baseline">
    <td nowrap="nowrap" align="right">Last_name:</td>
    <td><input type="text" name="last_name" value="" size="32" /></td>
    </tr>
    <tr valign="baseline">
    <td nowrap="nowrap" align="right">Email:</td>
    <td><input type="text" name="email" value="" size="32" /></td>
    </tr>
    <tr valign="baseline">
    <td nowrap="nowrap" align="right">Phone:</td>
    <td><input type="text" name="phone" value="" size="32" /></td>
    </tr>
    <tr valign="baseline">
    <td nowrap="nowrap" align="right">Reported_issue:</td>
    <td><input type="text" name="Reported_issue" value="" size="32" /></td>
    </tr>
    <tr valign="baseline">
    <td nowrap="nowrap" align="right">&nbsp;</td>
    <td><input type="submit" value="Insert record" /></td>
    </tr>
    </table>
    <input type="hidden" name="MM_insert" value="form2" />
    </form>
    <p>&nbsp;</p>
    </div>
    <p>&nbsp;</p>
    ////////////////////////////////

    I have also attached the index.php file in here.

    Thanks for any help,


    Abby
     

    Attached Files:

    akalehzan, Jun 21, 2012 IP
  2. davidson_11

    davidson_11 Active Member

    Messages:
    33
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    80
    #2
    Do you want to use SMTP or the general PHP mailing function to accomplish this?
     
    davidson_11, Jun 22, 2012 IP
  3. akalehzan

    akalehzan Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    The web-server will be running on MAC OSX 10.7 lion server , so whichever is easier and more compatible to setup.
    On the Server I have already enabled the PHP and Email. So probably PHP would the one we need to use??

    Thanks in advance,

    Abby
     
    akalehzan, Jun 23, 2012 IP
  4. lampChamp

    lampChamp Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    After this line
    $Result1 = mysql_query($insertSQL, $psyc) or die(mysql_error());
    PHP:
    Add this
    mail('email@address.com', 'Subject', 'Message', "From: Notification <notification@website.com>\r\n");
    PHP:
    The from field can be changed to show any email you want, but could end up being caught by a spam filter.

    Alternatively, you can use HTML code in the email, by adding Content-Type to the 4th argument (the email headers)
    mail('email@address.com', 'Subject', '<p>HTML message - <a href="#">Link</a></p>', "From: Notification <notification@website.com>\r\nContent-Type: text/html\r\n");
    PHP:
    Keep in mind PHP there must be an SMTP server configured to work with PHP, as it may fail. Not sure if OS X has a native SMTP handler.
     
    lampChamp, Jun 23, 2012 IP
  5. maxking1234

    maxking1234 Peon

    Messages:
    22
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Keep in mind the mail() function is not very secure.
     
    maxking1234, Jun 24, 2012 IP
  6. akalehzan

    akalehzan Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Adding this line:
    mail('email@address.com', 'Subject', '<p>HTML message - <a href="#">Link</a></p>', "From: Notification <notification@website.com>\r\nContent-Type: text/html\r\n");

    Works very well!!! thank you!!

    Now, if I want to add some of the fields into the email content how should I do it.
    Fields name:

    first_name
    Last_name
    Phone
    email
    Reported_issue


    Thanks in advance,
     
    akalehzan, Jun 27, 2012 IP
  7. akalehzan

    akalehzan Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Using :
    .PHP_EOL


    Did what I wanted!!!

    here is part of code:


    $to = "x@yahoo.com";
    $subject = "Helpu Notification System.";
    $message =
    "First_name: ".$_POST['first_name'].PHP_EOL .
    "Last_name: " .$_POST['last_name'].PHP_EOL.
    "Email: " .$_POST['email'].PHP_EOL.
    "Reported_Issues: ". $_POST['Reported_issue'].PHP_EOL.
    "Phone_Number: " . $_POST['phone'];
    $headers = "From: Helpu System\r\n" .
    $from = "Helpu System";
    $headers = "From:" . $_POST['first_name'] ;
    $body =
    mail($to,$subject,$message , $body, $headers );
     
    akalehzan, Jul 5, 2012 IP