Jump to content
Larry Ullman's Book Forums

Recommended Posts

Hello,

 

I am trying to set up a page where a member who is logged in can access other member's pages, then send an email to the member who has posted a certain page so that the member who is logged in can make comments. At this stage the logged in member doesn't necessarily know the email address of the member who had posted the page that the logged in member has pulled up.

 

I have tried several combinations and I am not getting an error message. The email is not being received at the test email inbox.

 

Any help would be appreciated. Thanks.

 

Following is my current code:

 

$q = "SELECT topics.users_id, users.email FROM topics INNER JOIN users ON topics.users_id = users.id WHERE topics.id=$id" ;

$r = mysqli_query ($connect, $q);

 

if (mysqli_num_rows($r) > 0) {

 

while ($row = mysqli_fetch_array($r, MYSQLI_NUM)) {

}

}

 

echo '<h3>Contact Me</h3>';

 

// Check for form submission:

if ($_SERVER['REQUEST_METHOD'] == 'POST') {

 

// Minimal form validation:

if (!empty($_POST['name']) && !empty($_POST['email']) && !empty($_POST['comments']) ) {

 

// Create the body:

$body = "Name: {$_POST['name']}\n\nComments: {$_POST['comments']}";

 

// Make it no longer than 100 characters long:

$body = wordwrap($body, 100);

 

// Send the email:

mail('$row[1]', 'Message from SplendedTopics.com', $body, "From: {$_POST['email']}");

 

// Print a message:

echo '<p><em>Thank you for contacting a member of Spendid Topics.</em></p>';

 

// Clear $_POST (so that the form's not sticky):

$_POST = array();

 

} else {

echo '<p style="font-weight: bold; color: #C00">Please fill out the form completely.</p>';

}

 

} // End of main isset() IF.

 

?>

 

<p>Please fill out this form to contact the poster of this notice.</p>

 

<form action="EmailContactPoster.php" method="post">

 

<p>Your Name: <input type="text" name="name" size="30" maxlength="60" value="<?php if (isset($_POST['name'])) echo $_POST['name']; ?>" /></p>

 

<p>Your Email Address: <input type="text" name="email" size="30" maxlength="80" value="<?php if (isset($_POST['email]'])) echo $_POST['email']; ?>" /></p>

 

<p>Comments: <textarea name="comments" rows="5" cols="30"><?php if (isset($_POST['comments'])) echo $_POST['comments']; ?></textarea></p>

 

<input type="hidden" name="id" value="' . $id . '" />

 

<p><input type="submit" name="submit" value="Contact Poster!" /></p>

</form>

Link to comment
Share on other sites

Marie, forgive me for asking, but I couldn't understand what you are trying to do and what you ultimately want.

Are you trying to set up a site where members can share links with each other, and other members can comment on the posted links?

 

If that's the case, you may want to consider whether sending an email in the first place is necessary.

When I used to use Facebook (I have since quit Facebook because I can't stand it), I would get rather annoyed at the frequency that I'd get automated emails from them telling me that someone commented on a pic I was in, etc.

Of course, I can't speak on behalf of the average user, but I imagine that most people (like me) don't want their inboxes flooded with a bunch of unnecessary emails.

 

Anyway, that's more a design decision than anything, but either way, it's hard for us to help you without first establishing what it is you want to do.

 

As a general piece of advice, you may want to start by testing the mail function by using fixed values that you know are okay. That way, you can confirm whether it's the mail function being improperly configured or your code that's the problem.

 

I apologize for not being able to provide a more concrete answer, but I'm at work now, where resources are limited, and I'm not too familiar with the mail function.

  • Upvote 2
Link to comment
Share on other sites

So you've tried sending emails to several different domains, and in all cases, no error occurs, but you never receive the email?

Is your script running on a server provided by a hosting service or on your own server?

Could you please give us some more details about the environment?

Link to comment
Share on other sites

Hello,

 

Yes, I have pulled up different "pages" that have been posted by my registered test members and sent emails to these people and none arrive. In all cases I am using various emails that are mine so when my test members have registered or forgotten passwords, etc. the email addresses have worked.

 

The script is running on a server provided by a hosting service so it is live.

 

When I used the original script from the book and used a valid email address, I was receiving emails, but of course, I want the script to accept whatever email address has been brought up by the database.

 

From the book,

 

// Send the email:

mail('youremail@example.com', 'Message from SplendedTopics.com', $body, "From: {$_POST['email']}");

 

Thanks,

 

Marie

Link to comment
Share on other sites

If the mail function works when you explicitly type in an email address but then it doesn't work when you use a variable that contains a email address string, then the problem is the variable and/or the retrieval of the data from the DB.

Before calling the mail function, have you tried echoing the value stored in the variable to ensure it's what you want?

Link to comment
Share on other sites

Hello,

Okay I have attempted to echoe the value and nothing came up so it is not working the way I would like it to work. Right now my code is messed up so I will have to take another look at it.

 

Marie

Link to comment
Share on other sites

Well, I guess that's your answer. Thanks for confirming that.

As a first step, I'd try executing your query with static values from phpMyAdmin to confirm that your SQL query works the way you want it to.

After that, I'd deal with the PHP logic side of things.

Good luck.

Link to comment
Share on other sites

Hello,

 

I revisited my code and am it will now echo the value that I want from the database. However, when I enter that same value in the following line, I am still not getting any emails at that email address.

 

I have tried the following: -

 

mail(' . $row[1] . ', 'Message from SplendedTopics.com', $body, "From: {$_POST['email']}");

 

and also

 

mail('$row[1]', 'Message from SplendedTopics.com', $body, "From: {$_POST['email']}");

 

and variations of the database query.

 

Thanks,

 

Marie

Link to comment
Share on other sites

This is pretty basic, but you are actually asking the mail function to use the String "$row[1]", and not the VALUE of $row[1]. There's a big difference.

 

 

$to = $row[1];$subject = 'Message from SplendedTopics.com';$from = "From: {$_POST['email']}";mail($to, $subject, $body, $from);

 

 

I would've thrown in some validation there too.

  • Upvote 1
Link to comment
Share on other sites

Hello,

 

Thank you. I thought that I was following another script that would make sense in this situation. I guess it is always good to get back to basics and not complicate anything.

 

So, now the script is working and I have another scipt on the same page that is working separately. Now I have to get them working together. Hopefully, I can do that myself. I will get back to the forum.

 

Marie

Link to comment
Share on other sites

Such things are easily overlooked. Done stupid errors like that one too many times myself. ;)

 

Marie: What kind of editor do you use? I would recommend you download something like Eclipse or Netbeans. Both work on Mac/Windows. With syntax highlighting, you would've seen that the "variable" had the wrong color. I will also give you a lot extra like function/variable hinting. If you start typing 'mysql_'.. It will suggest function like mysql_query(), mysqli_real_escape_string, etc. It will make your life much easier.

  • Upvote 1
Link to comment
Share on other sites

Hello,

 

I am using Dreamweaver for most of my work and from time to time use TextWrangler. I have heard of Netbeans but not Eclipse. I will investigate both. So far have not been able to get the script working the way I would like. As I mentioned previously I know that there are sites that can do this - www.cellclients.com seems to do this no problem.

 

Marie

Link to comment
Share on other sites

Hello,

 

After a bit of trial and error and some help from my friends, the following script seems to accomplish my goal.

 

// Send the email:

mail($_POST['toemail'], 'Mywebsite.com', $body, "From: {$_POST['email']}");

 

 

<?php

 

if (array_key_exists("id",$_POST)) $id = mysqli_real_escape_string($connect,$_POST['id']);

 

$q = "SELECT notices.users_id, notices.company, notices.alternate, notices.amount, notices.currency, notices.timeframe, notices.location, notices.description, users.email FROM notices INNER JOIN users ON notices.users_id = users.id WHERE notices.id=$id" ;

$r = mysqli_query ($connect, $q);

 

//if (mysqli_num_rows($r) > 0) {

 

if (mysqli_num_rows($r) == 1) {

 

// Get the user's information:

$row = mysqli_fetch_array ($r, MYSQLI_NUM);

}

 

 

echo '<form action="ContactPerson.php" method="post">

 

<p><h2a>Company:</h2a> ' . $row[1] . '</h2a></p>

 

<p><h2a>Alternate:</h2a> ' . $row[2] . '</p>

 

<p><h2a>Amount:</h2a> ' . $row[3] . '</p>

 

<p><h2a>Currency:</h2a> ' . $row[4] . '</p>

 

<p><h2a>Timeframe:</h2a> ' . $row[5] . '</p>

 

<p><h2a>Location:</h2a> ' . $row[6] . '</p>

 

<p><h2a>Description:</h2a> ' . $row[7] . '</p>

 

<p><h2a>email:</h2a> ' . $row[8] . '</p>

 

<input type="hidden" name="toemail" value="'.$row[8].' ">';

 

?>

<p>Your Name: <input type="text" name="name" size="30" maxlength="60" value="<?php if (isset($_POST['name'])) echo $_POST['name']; ?>" /></p>

 

<p>Your Email Address: <input type="text" name="email" size="30" maxlength="80" value="<?php if (isset($_POST['email'])) echo $_POST['email']; ?>" /></p>

 

<p>Comments: <textarea name="comments" rows="5" cols="30"><?php if (isset($_POST['comments'])) echo $_POST['comments']; ?></textarea></p>

 

<?php

echo '<input type="hidden" name="id" value="' . $id . '" />

 

<p><input type="submit" name="submit" value="Send Your Email!" /></p> ';

 

?>

  • Upvote 1
Link to comment
Share on other sites

 Share

×
×
  • Create New...