PHP AND HTML Email Script Help

mattblog

New Member
Messages
463
Reaction score
13
Points
0
I made a script to send a email to myself for link exchange. I tried it but it doesnt send the email. can anyone check if i did anything wrong. This was coded in html and refers to an action from a php script.

Html

Code:
<form name="LinkExchange_ContactForm" method="post" action="send_form_email.php">
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Request a Link Exchange - Techsavy Technology Forums</title>
<!-- Mr Bot, Don't Write These Down Please! -->
<meta name="robots" content="noindex" />
<!-- Okay, Thanks Bot for Listening -->
<body style="background-image: url('../../../under_maintenance/bg.gif'); background-repeat: repeat-x">
</head>
<!-- BACKGROUND END -->
<!-- LOGO AND INFORMATION BEGINS-->
<!-- DO NOT DELETE FOLLOWING TAGS, THEY FORMAT THE INFORMATION BELOW THE HEADER -->
<br /><br /><br />
<!-- LOGO -->
<img alt="" src="../../../under_maintenance/logo.png" align="topleft/" />
<br /><br /><br />
<!-- START_FORM -->
<center>
<form name="contactform" method="post" action="http://www.techsavy.cz.cc/forms/linkexchange/send_form_email.php">
<table width="700px">
<tr>
 <td valign="center">
  <label for="first_name"><h5><font color="#747170">First Name*</font></h5></label>
 </td>
 <td valign="center">
  <input  type="text" name="first_name" maxlength="50" size="30">
 </td>
</tr>
<tr>
 <td valign="center">
  <label for="email"><h5><font color="#747170">Email Address*</font></h5></label>
 </td>
 <td valign="center">
  <input  type="text" name="email" maxlength="80" size="30">
 </td>
</tr>
<tr>
 <td valign="center"">
  <label for="website"><h5><font color="#747170">Website*</font></h5></label>
 </td>
 <td valign="center">
  <input  type="text" name="website" maxlength="80" size="30">
 </td>
</tr>
<tr>
 <td valign="center"">
  <label for="website"><h5><font color="#747170">Your Website Topic*</font></h5></label>
 </td>
 <td valign="center">
  <input  type="text" name="website_topic" maxlength="80" size="30">
 </td>
</tr>
<tr>
 <td valign="center"">
  <label for="website"><h5><font color="#747170">Where will my Link be located on your site? *</font></h5></label>
 </td>
 <td valign="center">
  <input  type="text" name="link_place" maxlength="80" size="30">
 </td>
</tr>
<tr>
 <td valign="center"">
  <label for="website"><h5><font color="#747170">Title of Your Website*</font></h5></label>
 </td>
 <td valign="center">
  <input  type="text" name="your_link_title" maxlength="80" size="30">
 </td>
</tr>
 <td valign="center"">
  <label for="website"><h5><font color="#747170">Will My Link be On Every Page of Your Website?*</font></h5></label>
 </td>
 <td valign="center">
  <input  type="checkbox" name="link_every_page" maxlength="100" size="100">
 </td>
</tr>
 <td valign="center">
  <label for="comments"><h5><font color="#747170">Comments</font></h5></label>
 </td>
 <td valign="center">
  <textarea  name="comments" maxlength="1000" cols="25" rows="6"></textarea>
 </td>
</tr>
<td>
 <td colspan="2" style="text-align:center">
  <input type="submit" value="Submit">
 </td>
</tr>
</table>
</center>
<!-- END_FORM -->
</form>
</body>
</html>


PHP Action Script

Code:
<?php
if(isset($_POST['email'])) {
    $email_to = "hidden for privacy";
    $email_subject = "Link Exchange Request";

    
    function died($error) {
        // your error code can go here
        echo "We are very sorry, but there were error(s) found with the form your submitted. ";
        echo "These errors appear below.<br /><br />";
        echo $error."<br /><br />";
        echo "Please go back and fix these errors.<br /><br />";
        die();
    }
    
    // validation expected data exists
    if(!isset($_POST['first_name']) ||
        !isset($_POST['email']) ||
        !isset($_POST['website']) ||
        !isset($_POST['website_topic']) ||
        !isset($_POST['link_place']) ||
        !isset($_POST['your_link_title']) ||
        !isset($_POST['link_every_page']) ||
        !isset($_POST['comments'])) {
        died('We are sorry, but there appears to be a problem with the form your submitted.');        
    }
    
    $first_name = $_POST['first_name']; // required
    $email_from = $_POST['email']; // required
    $website = $_POST['website']; // required
        $website_topic = $_POST['website_topic']; // required
        $link_place = $_POST['link_place']; // required
    $your_link_title = $_POST['your_link_title']; // required
    $link_every_page = $_POST['link_every_page']; // required
    $comments = $_POST['comments']; // not required
    
    $error_message = "";
    $email_exp = "^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$";
  if(!eregi($email_exp,$email_from)) {
      $error_message .= 'The Email Address you entered does not appear to be valid.<br />';
  }

    $error_message = "";
    $website_exp = "^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$";
  if(!eregi($email_exp,$email_from)) {
      $error_message .= 'The Website you entered does not appear to be valid.<br />';
  }

    $string_exp = "^[a-z .'-]+$";
  if(!eregi($string_exp,$first_name)) {
      $error_message .= 'The First Name you entered does not appear to be valid.<br />';

  }
  if(strlen($error_message) > 0) {
      died($error_message);
  }
    $email_message = "Form details below.\n\n";
    
    function clean_string($string) {
      $bad = array("content-type","bcc:","to:","cc:","href");
      return str_replace($bad,"",$string);
    }
    
    $email_message .= "First Name: ".clean_string($first_name)."\n";
    $email_message .= "Email: ".clean_string($email_from)."\n";
    $email_message .= "Website: ".clean_string($website)."\n";
    $email_message .= "Website Topic: ".clean_string($website_topic)."\n";
    $email_message .= "Link Location: ".clean_string($link_place)."\n";
    $email_message .= "Their Link Title: ".clean_string($your_link_title)."\n";
    $email_message .= "Is The Link on Every Page?: ".clean_string($link_every_page)."\n";
    $email_message .= "Comments ".clean_string($comments)."\n";

    
    
// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);  
?>

Thank you for Submitting your Link Exchange Request. Your Request is Currently Being Processed and You will Receive a Reply Soon!
<br />
<a href="http://www.techsavy.cz.cc/">Click Here to Go Back to the Techsavy Homepage</a>

<?
}
?>

Can anyone check for me?
 

AngusThermopyle

Active Member
Messages
319
Reaction score
52
Points
28
HTML:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Request a Link Exchange - Techsavy Technology Forums</title>
<!-- Mr Bot, Don't Write These Down Please! -->
<meta name="robots" content="noindex" />
<!-- Okay, Thanks Bot for Listening -->
</head>
<body style="background-image: url('../../../under_maintenance/bg.gif'); background-repeat: repeat-x">

Remove the form tag at the beginning an put the closing head tag before the opening body tag.

Remove this test:
PHP:
isset($_POST['link_every_page'])

If they do not check that option, nothing is sent and the test will fail. Do you really want that?

Otherwise, I put it on my account (Chopin) and it sent the email.
Tried with 'email' field hotmail.com and gmail.com and garbage.com .
 
Last edited:

mattblog

New Member
Messages
463
Reaction score
13
Points
0
I dont why, but it did not work for me. I did everything you said and it didnt work.
 

bhupendra2895

New Member
Messages
554
Reaction score
20
Points
0
Use preg instead of eregi, because This function has been DEPRECATED as of PHP 5.3.0. Relying on this feature is highly discouraged.
According to php.net

PHP:
$email_exp = "^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$";
  if(!eregi($email_exp,$email_from)) {
      $error_message .= 'The Email Address you entered does not appear to be valid.<br />';
  }

    $error_message = "";
    $website_exp = "^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$";
  if(!eregi($email_exp,$email_from)) {
      $error_message .= 'The Website you entered does not appear to be valid.<br />';
  }

    $string_exp = "^[a-z .'-]+$";
  if(!eregi($string_exp,$first_name)) {
      $error_message .= 'The First Name you entered does not appear to be valid.<br />';

  }

Although I can't solve your mail problem, but use functions which are not going to be deprecated in future.I know your host has php5.2 but who knows in future they can upgrade it or you can have VPS with php 5.3.
 

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
How do you know that it's not sending the e-mail, as opposed to the e-mail isn't being received properly?

Don't ignore errors from functions. Test the return value of mail and output a helpful error message that doesn't reveal too much information.

To catch other HTML errors (such as c.solomon mentioned), use the W3C's validator. One major syntax problem is you've got extra double-quotes in a number of attributes.

When posting code, write a minimal test case. It helps you narrow down on the problem and is less cruft for others to wade through.

To make your code more readable, please indent your code to show nesting level and use
HTML:
 tags to colorize the source:
[html]<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <title>Request a Link Exchange - Techsavy Technology Forums</title>
    <meta name="robots" content="noindex" />
  </head>
  <body style="background-image: url('../../../under_maintenance/bg.gif'); background-repeat: repeat-x">
    <form name="contactform" method="post" action="http://www.techsavy.cz.cc/forms/linkexchange/send_form_email.php">
        <label for="first_name">First Name*</label>
        <input  name="first_name" maxlength="50" size="30">

Don't use HTML elements for formatting, as you do with <br/>, <h5>, <font> and others. HTML defines the structure of a document, not how it's presented. Use CSS for styling. <h5> is used to mark a heading. <font>, <center> and <br> are presentational elements and should be avoided. Along the same lines, don't use tables for layout. There are plenty of ways of styling forms without using tables ("Applying CSS to forms", "Fancy Form Design Using CSS").

Don't use die when outputting HTML. It will result in invalid HTML.

If you find yourself repeatedly writing very similar code, you should abstract it into a function or rewrite it as a loop or switch. Here, you could make better use of both functions and loops.
 
Last edited:
Top