Jump to content
Larry Ullman's Book Forums

Email Does Not Include Message


Recommended Posts

The following code inserts  a message into my message board, that part functions with no problem.

It is then supposed to send me an email to alert me that a message has been sent to me.

I receive the email with the alert saying posting added by the user but the message just contains the word message. I am obviously doing something silly,

<?php 
session_start() ;
// Redirect user if he is not logged in
if ( !isset( $_SESSION[ 'member_id' ] ) ) { 
require ( 'login_functions.php' ) ; 
load() ; 
}
?>
<!doctype html>
<html lang=en>
<head>
<title>The form for posting messages</title>
<meta charset=utf-8>
<link rel="stylesheet" type="text/css" href="forum.css">
<style type="text/css">
#tab-navigation ul { margin-left:85px; }
form { padding-left:215px; }
</style>
</head>
<body>
<div id='container'>
<?php // The form for posting messages
include ( 'includes/header_post.php' ) ;
echo '<h2>Post a Message</h2>';
// Display the form fields
echo '<form action="process_post.php" method="post" accept-charset="utf-8">
<p>Choose the Subject: <select name="subject">
<option value="Category One">Category One</option>
<option value="category Two">Category Two</option>
</select></p>
<p>Message:<br><textarea name="message" rows="5" cols="50"></textarea></p>
<p><input name="submit" type="submit" value="post"></p>
</form>';
include ( 'includes/footer.php' ) ;
//posting an entry into the database table automaticlally sends an email to the forum moderator 
// Assign the the variables
$subject = "Posting added to message board";
$user = isset($_SESSION['uname']) ? $_SESSION['uname'] : "";
$message='message';
$body = "Posting added by . $user . $message";
mail("me@myisp.co.uk", $subject, $body, "From:me@myisp.co.uk\r\n");?>
</div>
</body>
</html

please would you enlighten me.

Link to comment
Share on other sites

You're confusing your syntax on the following two lines:

$message='message';
$body = "Posting added by . $user . $message";
 
For one, "message" is being output because your $message variable is being set to the literal string "message". I would start by changing that to a more meaningful message.
Second, when using double quotes, you can output the value of variables directly, without the need to concatenate strings together.
Third, even if you do decide to concatenate strings together (because, for example, you want to use single quotes instead of double quotes), you need to properly stop and start your strings with extra sets of quotes.
 
As such, I'd change the first line above to something like the following:
$message = $_POST['message'];
 
And then change the second line to either of the following:
$body = "$user added the following message:\r\n$message";
$body = '$user added the following message:' . "\r\n" . $message;
 
Hope that helps.
  • Upvote 1
Link to comment
Share on other sites

Reply to Hartley San

Many thanks for you prompt reply, sadly with neither solution the email contain the message, this is what I receive:

with the firts solution I get:   mechanic7 added the following message:

with the second solution I get:  $user added the following message:

The message is inserted into the database table so I think I must be assigning it incorrectly for the email.

I repeat the relevant part of the better code hoping that you can spot the problem
 

echo '<form action="process_post.php" method="post" accept-charset="utf-8">
<p>Choose the Subject: <select name="subject">
<option value="Category One">Category One</option>
<option value="Category Two">Category Two</option>
</select></p>
<p>Message:<br><textarea name="message" rows="5" cols="50"></textarea></p>
<p><input name="submit" type="submit" value="post"></p>
</form>';
include ( 'includes/footer.php' ) ;
//posting an entry into the database table automaticlally sends a message to the forum moderator 
// Assign the subject
$subject = "Posting added to message board";
$user = isset($_SESSION['uname']) ? $_SESSION['uname'] : "";
//$message="message";
$message=$_POST['message'];
$body = "$user added the following message:\r\n$message";
mail("me@myisp.co.uk", $subject, $body, "From:admin@myisp.co.uk\r\n");?>

Link to comment
Share on other sites

I have just realized why the message is not included. The code sends the email the immediately the page is loaded and before the user has time to enter a message into the form. An if clause is needed before the email is sent but I would like some help with this, Please would you suggest some code that will do the trick?

Best wishes

Konfused

Link to comment
Share on other sites

Do an if statement that checks for form submission, and only send the email when the form is properly submitted and all values are valid. For example:

if (isset($_POST['message'])) {
  // Validate here.
  
  if (all-validation-successful) {
    // Send email here.
  }
}
Link to comment
Share on other sites

Hello Hartley Stan

I tried your last suggestion but no messge was contained in the email. I think the email instructions should be removed from the post.php page and they should appear in the process_post.php file. I don't know the best way of including the email code into the process_post.php file, I would be grateful for your advice.
   
Here is the process_post.php file

<?php 
// Start the session
session_start();
// Include the login functions to check for errors
require ( 'login_functions.php' ) ;
// If users are not logged in, redirect them
if ( !isset( $_SESSION[ 'member_id' ] ) ) { load() ; }
//Connect to the database
    require ( 'mysqli_connect.php' ) ;
// Has the form been submitted?
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
// Check that the user has entered a subject and a message
  if ( empty($_POST['subject'] ) ) { echo '<p>You forgot to enter a subject.</p>'; }
  if ( empty($_POST['message'] ) ) { echo '<p>You forgot to enter a message.</p>'; }
   if ( !empty( $_POST['message'])) 
  { 
  $message = mysqli_real_escape_string( $dbcon, strip_tags(trim( $_POST['message'] )) ) ; 
  }
// If successful, insert the post into the database table
  if( !empty($_POST['subject']) &&  !empty($_POST['message']) )
  {
 //Make the insert query
    $q = "INSERT INTO forum(uname, subject, message, post_date) 
          VALUES ('{$_SESSION['uname']}', '{$_POST['subject']}','$message',NOW() )";
    $result = mysqli_query ( $dbcon, $q ) ;
// If it fails display an error message
    if (mysqli_affected_rows($dbcon) != 1) { echo '<p>Error</p>'.mysqli_error($dbcon); } else { load('forum.php'); }
// Close the database connection
    mysqli_close( $dbcon ) ; 
    }
 } 
// Create a link back to the forum page.
//echo '<p><a href="forum.php">Forum</a>' ;
//include ( 'includes/footer.php' ) ;
?>
Link to comment
Share on other sites

It's HartleySan, not Hartley Stan. Anyway ...

 

If a message is not being set in the email, then the $message variable is likely not being set in your PHP script to begin with.

Please echo $message to the screen in your PHP script and verify that it's even set before doing anything else.

Once you've verified that, then getting the message to appear in the email should be quite trivial.

 

Please let us know what you find.

Link to comment
Share on other sites

 Share

×
×
  • Create New...