1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Email Notification Not Going Through

Discussion in 'Web Design & Development' started by JazzySOUL, May 5, 2012.

Thread Status:
Not open for further replies.
Advertisement
  1. JazzySOUL

    JazzySOUL Thread Starter

    Joined:
    Apr 26, 2011
    Messages:
    40
    I have a tough one here guys.

    So I am currently going through a tutorial: PHP With MySQL Beyond The Basics.

    It's about building a content management system.

    This is my first attempt at PHP & MySQL.

    I am at the “Sending Emails” part.

    I am using “Wampserver 2.2d”.

    I did “Using PHPMailer” successfully but am now stuck @ “Building notification for new comments”. This is the last stage in the whole tutorial.

    The goal of this part is to send an emnail notification through to my email when ever I receive a comment on my webpage.

    The email notification isn’t going through no matter how long I wait and it’s not because of my SMTP setup as the “Sending Emails” part was successful and I didn’t change anything in that regard.

    I have googled to no avail.

    Line 5 in comment.php seems to be the problem.

    I am using IMAP instead of POP3, could that be an issue? Attached is info that might help you help me. Thanks.

    -----------------------------------------------------------------------------------------------------------------------------

    Error Messages @ comment.php:

    Notice: Use of undefined constant LIB_PATH - assumed 'LIB_PATH' in C:\wamp\www\photo_gallery\includes\comment.php on line 5


    Notice: Use of undefined constant DS - assumed 'DS' in C:\wamp\www\photo_gallery\includes\comment.php on line 5


    Warning: require_once(LIB_PATHDSdatabase.php) [function.require-once]: failed to open stream: No such file or directory in C:\wamp\www\photo_gallery\includes\comment.php on line 5


    Fatal error: require_once() [function.require]: Failed opening required 'LIB_PATHDSdatabase.php' (include_path='.;C:\php\pear') in C:\wamp\www\photo_gallery\includes\comment.php on line 5


    PHP Notice: Use of undefined constant LIB_PATH - assumed 'LIB_PATH' in C:\wamp\www\photo_gallery\includes\comment.php on line 5



    Comment.php:

    <?php

    // If it's going to need the database, then it's
    // probably smart to require it before we start.
    require_once(LIB_PATH.DS.'database.php'); // [[[[this is line 5]]]]

    class Comment extends DatabaseObject {

    protected static $table_name="comments";
    protected static $db_fields=array('id', 'photograph_id', 'created', 'author', 'body');

    public $id;
    public $photograph_id;
    public $created;
    public $author;
    public $body;

    // "new" is a reserved word so we use "make" (or "build")
    public static function make($photo_id, $author="Anonymous", $body="") {
    if(!empty($photo_id) && !empty($author) && !empty($body)) {
    $comment = new Comment();
    $comment->photograph_id = (int)$photo_id;
    $comment->created = strftime("%Y-%m-%d %H:%M:%S", time());
    $comment->author = $author;
    $comment->body = $body;
    return $comment;
    } else {
    return false;
    }
    }

    public static function find_comments_on($photo_id=0) {
    global $database;
    $sql = "SELECT * FROM " . self::$table_name;
    $sql .= " WHERE photograph_id=" .$database->escape_value($photo_id);
    $sql .= " ORDER BY created ASC";
    return self::find_by_sql($sql);
    }

    public function try_to_send_notification() {
    $mail = new PHPMailer();

    $mail->IsSMTP();
    $mail->Host = "server.solutions.local";
    $mail->Port = 25;
    $mail->SMTPAuth = false;
    $mail->Username = "[email protected]";
    $mail->Password = "0987y";

    $mail->FromName = "Photo Gallery";
    $mail->From = "[email protected]";
    $mail->AddAddress("[email protected]", "Photo Gallery Admin");
    $mail->Subject = "New Photo Gallery Comment";
    $created = datetime_to_text($this->created);
    $mail->Body =<<<EMAILBODY

    //A new comment has been received in the Photo Gallery.

    At {$created}, {$this->author} wrote:

    {$this->body}

    EMAILBODY;

    $result = $mail->Send();
    return $result;

    }

    // Common Database Methods
    public static function find_all() {
    return self::find_by_sql("SELECT * FROM ".self::$table_name);
    }

    public static function find_by_id($id=0) {
    $result_array = self::find_by_sql("SELECT * FROM ".self::$table_name." WHERE id={$id} LIMIT 1");
    return !empty($result_array) ? array_shift($result_array) : false;
    }

    public static function find_by_sql($sql="") {
    global $database;
    $result_set = $database->query($sql);
    $object_array = array();
    while ($row = $database->fetch_array($result_set)) {
    $object_array[] = self::instantiate($row);
    }
    return $object_array;
    }

    public static function count_all() {
    global $database;
    $sql = "SELECT COUNT(*) FROM ".self::$table_name;
    $result_set = $database->query($sql);
    $row = $database->fetch_array($result_set);
    return array_shift($row);
    }

    private static function instantiate($record) {
    // Could check that $record exists and is an array
    $object = new self;
    // Simple, long-form approach:
    // $object->id = $record['id'];
    // $object->username = $record['username'];
    // $object->password = $record['password'];
    // $object->first_name = $record['first_name'];
    // $object->last_name = $record['last_name'];

    // More dynamic, short-form approach:
    foreach($record as $attribute=>$value){
    if($object->has_attribute($attribute)) {
    $object->$attribute = $value;
    }
    }
    return $object;
    }

    private function has_attribute($attribute) {
    // We don't care about the value, we just want to know if the key exists
    // Will return true or false
    return array_key_exists($attribute, $this->attributes());
    }

    protected function attributes() {
    // return an array of attribute names and their values
    $attributes = array();
    foreach(self::$db_fields as $field) {
    if(property_exists($this, $field)) {
    $attributes[$field] = $this->$field;
    }
    }
    return $attributes;
    }

    protected function sanitized_attributes() {
    global $database;
    $clean_attributes = array();
    // sanitize the values before submitting
    // Note: does not alter the actual value of each attribute
    foreach($this->attributes() as $key => $value){
    $clean_attributes[$key] = $database->escape_value($value);
    }
    return $clean_attributes;
    }

    public function save() {
    // A new record won't have an id yet.
    return isset($this->id) ? $this->update() : $this->create();
    }

    public function create() {
    global $database;
    // Don't forget your SQL syntax and good habits:
    // - INSERT INTO table (key, key) VALUES ('value', 'value')
    // - single-quotes around all values
    // - escape all values to prevent SQL injection
    $attributes = $this->sanitized_attributes();
    $sql = "INSERT INTO ".self::$table_name." (";
    $sql .= join(", ", array_keys($attributes));
    $sql .= ") VALUES ('";
    $sql .= join("', '", array_values($attributes));
    $sql .= "')";
    if($database->query($sql)) {
    $this->id = $database->insert_id();
    return true;
    } else {
    return false;
    }
    }

    public function update() {
    global $database;
    // Don't forget your SQL syntax and good habits:
    // - UPDATE table SET key='value', key='value' WHERE condition
    // - single-quotes around all values
    // - escape all values to prevent SQL injection
    $attributes = $this->sanitized_attributes();
    $attribute_pairs = array();
    foreach($attributes as $key => $value) {
    $attribute_pairs[] = "{$key}='{$value}'";
    }
    $sql = "UPDATE ".self::$table_name." SET ";
    $sql .= join(", ", $attribute_pairs);
    $sql .= " WHERE id=". $database->escape_value($this->id);
    $database->query($sql);
    return ($database->affected_rows() == 1) ? true : false;
    }

    public function delete() {
    global $database;
    // Don't forget your SQL syntax and good habits:
    // - DELETE FROM table WHERE condition LIMIT 1
    // - escape all values to prevent SQL injection
    // - use LIMIT 1
    $sql = "DELETE FROM ".self::$table_name;
    $sql .= " WHERE id=". $database->escape_value($this->id);
    $sql .= " LIMIT 1";
    $database->query($sql);
    return ($database->affected_rows() == 1) ? true : false;

    // NB: After deleting, the instance of User still
    // exists, even though the database entry does not.
    // This can be useful, as in:
    // echo $user->first_name . " was deleted";
    // but, for example, we can't call $user->update()
    // after calling $user->delete().
    }

    }

    ?>


    database.php:


    <?php


    require_once(LIB_PATH.DS."config.php");

    class MySQLDatabase {

    private $connection;
    public $last_query;
    private $magic_quotes_active;
    private $real_escape_string_exists;

    function __construct() {
    $this->open_connection();
    $this->magic_quotes_active = get_magic_quotes_gpc();
    $this->real_escape_string_exists = function_exists( "mysql_real_escape_string" );
    }

    public function open_connection() {
    $this->connection = mysql_connect(DB_SERVER, DB_USER, DB_PASS);
    if (!$this->connection) {
    die("Database connection failed: " . mysql_error());
    } else {
    $db_select = mysql_select_db(DB_NAME, $this->connection);
    if (!$db_select) {
    die("Database selection failed: " . mysql_error());
    }
    }
    }

    public function close_connection() {
    if(isset($this->connection)) {
    mysql_close($this->connection);
    unset($this->connection);
    }
    }

    public function query($sql) {
    $this->last_query = $sql;
    $result = mysql_query($sql, $this->connection);
    $this->confirm_query($result);
    return $result;
    }

    public function escape_value( $value ) {
    if( $this->real_escape_string_exists ) { // PHP v4.3.0 or higher
    // undo any magic quote effects so mysql_real_escape_string can do the work
    if( $this->magic_quotes_active ) { $value = stripslashes( $value ); }
    $value = mysql_real_escape_string( $value );
    } else { // before PHP v4.3.0
    // if magic quotes aren't already on then add slashes manually
    if( !$this->magic_quotes_active ) { $value = addslashes( $value ); }
    // if magic quotes are active, then the slashes already exist
    }
    return $value;
    }

    // "database-neutral" methods
    public function fetch_array($result_set) {
    return mysql_fetch_array($result_set);
    }

    public function num_rows($result_set) {
    return mysql_num_rows($result_set);
    }

    public function insert_id() {
    // get the last id inserted over the current db connection
    return mysql_insert_id($this->connection);
    }

    public function affected_rows() {
    return mysql_affected_rows($this->connection);
    }

    private function confirm_query($result) {
    if (!$result) {
    $output = "Database query failed: " . mysql_error() . "<br /><br />";
    //$output .= "Last SQL query: " . $this->last_query;
    die( $output );
    }
    }

    }

    $database = new MySQLDatabase();
    $db =& $database;

    ?>



    initialize.php:


    <?php

    // Define the core paths
    // Define them as absolute paths to make sure that require_once works as expected

    // DIRECTORY_SEPARATOR is a PHP pre-defined constant
    // (\ for Windows, / for Unix)
    defined('DS') ? null : define('DS', DIRECTORY_SEPARATOR);

    defined('SITE_ROOT') ? null :
    define('SITE_ROOT', DS.'wamp'.DS.'www'.DS.'photo_gallery');

    defined('LIB_PATH') ? null : define('LIB_PATH', SITE_ROOT.DS.'includes');

    // load config file first
    require_once(LIB_PATH.DS.'config.php');

    // load basic functions next so that everything after can use them
    require_once(LIB_PATH.DS.'functions.php');

    // load core objects
    require_once(LIB_PATH.DS.'session.php');
    require_once(LIB_PATH.DS.'database.php');
    require_once(LIB_PATH.DS.'database_object.php');
    require_once(LIB_PATH.DS.'pagination.php');

    // load database-related classes
    require_once(LIB_PATH.DS.'user.php');
    require_once(LIB_PATH.DS.'photograph.php');
    require_once(LIB_PATH.DS.'comment.php');
    require_once(LIB_PATH.DS."phpMailer".DS."class.phpmailer.php");
    require_once(LIB_PATH.DS."phpMailer".DS."class.smtp.php");
    //require_once(LIB_PATH.DS."phpMailer".DS."language".DS."phpmailer.lang-en.php");
    ?>
     
As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Loading...
Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/1052108