-1

I have been trying to get a forum to work. My teacher wasn't satisfied with my previous 3 versions so I decided to ask my nephew for some help. After him helping me I only have one problem left and that is the e-mail function. Every time someone posts a reply to a question I want the user who asked the question to recieve an e-mail with the answer in it. The lay-out doesn't matter as long as he gets the answer. After 2 days I still haven't gotten it to work so I decided to ask my question here and hope to get an answer. Btw the language of the text is dutch if you are wondering. The code is in english.

<?php
//create_cat.php
include 'connect.php';
include 'header.php';

if($_SERVER['REQUEST_METHOD'] != 'POST'){
   //het kan niet direct worden opgeroepen, dit geeft een error
   echo 'Dit bestand is niet manueel bereikbaar.';
}else{
   //status effe checken
   if(!$_SESSION['signed_in']){
        echo 'Je moet aangemeld zijn om een antwoord te geven';
   }else{
        $sql = "INSERT INTO posts(post_inh,post_datu,post_topic,post_door) 
            VALUES ('" . $_POST['reply-content'] . "',
                    NOW(),
                    " . mysql_real_escape_string($_GET['id']) . ",
                    " . $_SESSION['geb_id'] . ")";

        $result = mysql_query($sql);
        if(!$result){
           echo 'Je antwoord is niet opgeslagen probeer later opnieuw';
        }else{
           echo 'Je antwoor is opgeslagen, bekijk <a href="topic.php?id=' . htmlentities($_GET['id']) . '">het</a>.';
        }
   }
}

include 'footer.php';
?>

This is the code where I want to put it in. The email adres of the person who asked the question is in the database and is called: geb_email

I would really appreciate some help because my previous 7 attempts failed and I'm losing hope.

I'm sorry for the bad english, English is not my main language.

edit:

<?php
error_reporting(E_ERROR | E_PARSE);
//create_cat.php
include 'connect.php';
include 'header.php';

$sql = "SELECT
        topic_id,
        topic_onderwerp
    FROM
        topics
    WHERE
        topics.topic_id = " . mysql_real_escape_string($_GET['id']);

$result = mysql_query($sql);

if(!$result)
{
echo 'Het onderwerp kan nu niet worden weergegeven, probeer later opnieuw';
}
else
{
    if(mysql_num_rows($result) == 0)
{
    echo 'Dit onderwerp bestaat niet.';
}
else
{
    while($row = mysql_fetch_assoc($result))
    {

        echo '<table class="topic" border="1">
                <tr>
                    <th colspan="2">' . $row['topic_onderwerp'] . '</th>
                </tr>';


        $posts_sql = "SELECT
                    posts.post_topic,
                    posts.post_inh,
                    posts.post_datu,
                    posts.post_door,
                    gebruikers.geb_id,
                    gebruikers.geb_naam
                FROM
                    posts
                LEFT JOIN
                    gebruikers
                ON
                    posts.post_door = gebruikers.geb_id
                WHERE
                    posts.post_topic = " . mysql_real_escape_string($_GET['id']);

        $posts_result = mysql_query($posts_sql);

        if(!$posts_result)
        {
            echo '<tr><td>De posts kunnen niet worden weergegeven. Probeer later opnieuw.</tr></td></table>';
        }
        else
        {

            while($posts_row = mysql_fetch_assoc($posts_result))
            {
                echo '<tr class="topic-post">
                        <td class="geb-post">' . $posts_row['geb_naam'] . '<br/>' . date('d-m-Y H:i', strtotime($posts_row['post_datu'])) . '</td>
                        <td class="post-inhoud">' . htmlentities(stripslashes($posts_row['post_inh'])) . '</td>
                      </tr>';
            }
        }

        if(!$_SESSION['signed_in'])
        {
            echo '<tr><td colspan=2>je moet <a href="signin.php">aangemeld zijn</a> om te antwoorden. Je kunt ook <a href="signup.php">een account </a> maken.';
        }
        else
        {
            //de reply cell/box of hoe je het ook wil noemen
            echo '<tr><td colspan="2"><h2>Reply:</h2><br />
                <form method="post" action="reply.php?id=' . $row['topic_id'] . '">
                    <textarea name="reply-content"></textarea><br /><br />

                    <input type="submit" value="Finish" />
                </form></td></tr>'

            ;
        }

$email_to="fakemail@mail.com"; //edited this quicly to prevent spam :p
$email_subject="Antwoord vraag forum";

$headers = "Van: forum";
mail($email_to, $email_subject, $email_message, $headers);  

        //en mijn grote fout hier was natuurlijk de tabel niet afsluiten
        echo '</table>';
    }
}
}

include 'footer.php';
?>

This is the topic.php code. I tried some thing here (removed the code breaking ones). All that needs to happen now is get the user, email and text into the $email variable. A lot of thanks.

Wolfeh
  • 1
  • 2

1 Answers1

0

You should not use MySQL driver as it is deprecated - use MySQLi or PDO instead!

You can send an email with this function:

mail("recipient@mail.com", "Subject", "Message (simple use enter for new line)", "From: Sender name <sender@mail.com>");

If it does not work, you should use a library like PHPMailer to send via SMTP (a library is recommended by me if you just can send with mail also).

You should not use !$_SESSION[...] as it can throw notices/even warning. Use empty() or !isset() instead.

Richard
  • 2,840
  • 3
  • 25
  • 37
  • I have gotten a little bit farther but I'm still stuck. Getting the right text in the mail is pretty difficult; Anyways thanks for the help. – Wolfeh Jun 20 '15 at 13:29
  • @Wolfeh: What is your problem with the text? – Richard Jun 20 '15 at 13:33
  • Well the problem is that reply.php is accesd by topic.php. reply.php gets all the information out of topic.php like topic_id = 1 topic_door (who posted it) = 1 and so on. I'm trying to link it to the user and then to the users e-mail. That is going ok. The main problem is that the text in reply is put in the reply-content text-area and I can't get the information from that textarea in the message. If you want I can just put all the code here so that you can see it. I probably provided not enough data. I'm sorry for that its my first time posting something here and I'm kind of desperate. – Wolfeh Jun 20 '15 at 13:45
  • @Wolfeh: Yes, please __edit__ the code into your __question__. – Richard Jun 20 '15 at 13:46
  • I edited my post and added the topic.php code. – Wolfeh Jun 20 '15 at 13:58
  • 1
    I finaly got everything to work. I went to MySQLi for my driver and everything is working ok now. – Wolfeh Jun 21 '15 at 15:38