By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,952 Members | 1,937 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,952 IT Pros & Developers. It's quick & easy.

How anyone can send email to members with a form?

P: 2
Hello everyone,

I'm new to PHP and creating an advertising website for massage therapists. On the profile page I'm creating a button so anyone can click on to send the advertiser an email.

I created a page called profile_sendemail.php which contains the form to send the email and here is the code:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. // Start_session, check if user is logged in or not, and connect to the database all in one included file
  3. include_once("scripts/checkuserlog.php");
  4. // Include the class files for auto making links out of full URLs and for Time Ago date formatting
  5. include_once("wi_class_files/autoMakeLinks.php");
  6. include_once ("wi_class_files/agoTimeFormat.php");
  7. // Create the two new objects before we can use them below in this script
  8. $activeLinkObject = new autoActiveLink;
  9. $myObject = new convertToAgo;
  10. ?>
  11.  
  12. <?php
  13. // Now let's initialize vars to be printed to page in the HTML section so our script does not return errors
  14. // they must be initialized in some server environments
  15. $id = "";
  16. $firstname = "";
  17. $middlename = "";
  18. $lastname = "";
  19. $email = "";
  20.  
  21. // If coming from category page
  22. if ($_GET['id']) {
  23.  
  24. $id = $_GET['id'];
  25.  
  26. } else if (isset($_SESSION['id'])) {
  27.  
  28. $id = $_SESSION['id'];
  29.  
  30. } else {
  31.  
  32. $id = $_SESSION['id'];
  33.  
  34. include_once "index.php";
  35. exit();
  36. }
  37. $id = mysql_real_escape_string($id);
  38. $id = eregi_replace("`", "", $id);
  39. $sql = mysql_query("SELECT * FROM myMembers WHERE id='$id'");
  40.  
  41. while($row = mysql_fetch_array($sql)){
  42.  
  43. $id = $row["id"];
  44. $firstname = $row["firstname"];
  45. $lastname = $row["lastname"];
  46.  
  47.  
  48. } // close while loop
  49.  
  50. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  51. $style_sheet = "default";
  52. ?>
  53.  
  54.  
  55. <?php
  56. ////////////////////////// MECHANISM TO SEND EMAILS ////////////////////////////////////////////////
  57.  
  58.  
  59. if(isset($_POST['email'])) {
  60.  
  61. // EDIT THE 2 LINES BELOW AS REQUIRED
  62. $email_to = "$email";
  63. $email_subject = "New appointment request on MassageTherapistsList.com";
  64.  
  65.  
  66. function died($error) {
  67. // your error code can go here
  68. echo "We are very sorry, but there were error(s) found with the form you submitted. ";
  69. echo "These errors appear below.<br /><br />";
  70. echo $error."<br /><br />";
  71. echo "Please go back and fix these errors.<br /><br />";
  72. die();
  73. }
  74.  
  75. // validation expected data exists
  76. if(!isset($_POST['name']) ||
  77. !isset($_POST['email']) ||
  78. !isset($_POST['comments'])) {
  79. died('We are sorry, but there appears to be a problem with the form you submitted.');
  80. }
  81.  
  82. $name = $_POST['name']; // required
  83. $email_from = $_POST['email']; // required
  84. $comments = $_POST['comments']; // required
  85.  
  86. $error_message = "";
  87. $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
  88. if(!preg_match($email_exp,$email_from)) {
  89. $error_message .= 'The Email Address you entered does not appear to be valid.<br />';
  90. }
  91. $string_exp = "/^[A-Za-z .'-]+$/";
  92. if(!preg_match($string_exp,$name)) {
  93. $error_message .= 'The Name you entered does not appear to be valid.<br />';
  94. }
  95. if(strlen($comments) < 2) {
  96. $error_message .= 'The Comments you entered do not appear to be valid.<br />';
  97. }
  98. if(strlen($error_message) > 0) {
  99. died($error_message);
  100. }
  101. $email_message = "Form details below.\n\n";
  102.  
  103. function clean_string($string) {
  104. $bad = array("content-type","bcc:","to:","cc:","href");
  105. return str_replace($bad,"",$string);
  106. }
  107.  
  108. $email_message .= "Name: ".clean_string($name)."\n";
  109. $email_message .= "Email: ".clean_string($email_from)."\n";
  110. $email_message .= "Comments: ".clean_string($comments)."\n";
  111.  
  112.  
  113. // create email headers
  114. $headers = 'From: '.$email_from."\r\n".
  115. 'Reply-To: '.$email_from."\r\n" .
  116. 'X-Mailer: PHP/' . phpversion();
  117. @mail($email_to, $email_subject, $email_message, $headers);
  118. ?>
  119.  
  120. <!-- include your own success html here -->
  121.  
  122. Thank you for contacting us. We will be in touch with you very soon.
  123.  
  124. <?php
  125.  
And the profile.php page I added the link with the following PHP function:

Expand|Select|Wrap|Line Numbers
  1. if (empty($_SESSION['id'])) {
  2. $_SESSION['id'] = $id;
  3. } else {
  4. $_SESSION['id']++;
  5. }
  6. ?>
  7.  
  8. <a href="profile_sendmail.php?id=".$id."<?php echo htmlspecialchars(SID); ?>">Send Email</a>
  9.  
However, when I click on "Send Email" I get the following url without id:

http://domain.com/profile_sendmail.php?id=

And of course a page Not Found error instead of the email form.

Any ideas what would be the best way to get the results I want?
Nov 3 '11 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.