php Email Notification

crosscheck

Regular Member
Joined
Aug 12, 2010
Messages
424
Reaction score
231
I am looking for a script that will send a email when a button is clicked or after a form is submited
 
Code:
<?php
if($_POST['submit'])
           mail('YOURMAIL', 'Someone Sent You Their Email!', "Someone sent you this email: " . $_POST['email']);
?>
<html>
<body>
<form method=POST>
Your Email: <input type='text' name='email' />
<input type='submit' name='submit' value='Send My Email To crosscheck!' />
</form>
</body>
</html>

Viola?
 
OrderZeros got it
check out the php mail function for more info, there are a some cool headers you can edit as well

http://php.net/manual/en/function.mail.php

another example, allowing your site name to display in the 'from' column instead of the email address
Code:
 [COLOR=#000000] [COLOR=#0000bb]<?php
$to      [/COLOR][COLOR=#007700]= [/COLOR][COLOR=#dd0000]$_POST['email'][/COLOR][COLOR=#007700];
[/COLOR][COLOR=#0000bb]$subject [/COLOR][COLOR=#007700]= [/COLOR][COLOR=#dd0000]'the subject'[/COLOR][COLOR=#007700];
[/COLOR][COLOR=#0000bb]$message [/COLOR][COLOR=#007700]= [/COLOR][COLOR=#dd0000]'hello'[/COLOR][COLOR=#007700];
[/COLOR][COLOR=#0000bb]$headers [/COLOR][COLOR=#007700]= [/COLOR][COLOR=#dd0000]'From: SiteName <[email protected]>' [/COLOR][COLOR=#007700]. [/COLOR][COLOR=#dd0000]"\r\n" [/COLOR][COLOR=#007700].
    [/COLOR][COLOR=#dd0000]'Reply-To: SiteName <[email protected]>' [/COLOR][COLOR=#007700]. [/COLOR][COLOR=#dd0000]"\r\n" [/COLOR][COLOR=#007700].
    [/COLOR][COLOR=#dd0000]'X-Mailer: PHP/' [/COLOR][COLOR=#007700]. [/COLOR][COLOR=#0000bb]phpversion[/COLOR][COLOR=#007700]();

[/COLOR][COLOR=#0000bb]mail[/COLOR][COLOR=#007700]([/COLOR][COLOR=#0000bb]$to[/COLOR][COLOR=#007700], [/COLOR][COLOR=#0000bb]$subject[/COLOR][COLOR=#007700], [/COLOR][COLOR=#0000bb]$message[/COLOR][COLOR=#007700], [/COLOR][COLOR=#0000bb]$headers[/COLOR][COLOR=#007700]);
[/COLOR][COLOR=#0000bb]?>[/COLOR] [/COLOR]
 
In addition to this if you'd like to see if someone's opened your email you can add an an <img tag that takes the src param as a PHP page so when the user opens the email and views images the php page will be GET'd thus 'calling back' to the owner letting them know they opened it.

Pretty useful when funneling out your lists.

E.g.
PHP:
<?php
if(isset($_GET['email')):
      file_put_contents("email_log.txt", $_GET['email'] . " Opened Your Email On " . date("D M j G:i:s T Y"));
else:
      mail('somemail', 'LOOK AT THIS', "<img src='" . __FILE__ . "?email=" . base64_encode('somemail') . "' width=0 height=0>", 'MIME-Version: 1.0\r\nContent-type: text/html; charset=iso-8859-1\r\n');
?>
 
if you have a big for you can append this to the message body to get all the fields displayed nicely

PHP:
$message = "";
foreach($_POST as $key=>$post){
$message .= $key .": ". $post ."\r\n";
}
 
Google "php mail" and click on the w3schools link (The one I use); Super easy to use. enjoy.
 
Here is a tutorial link in which contact form is submitted through email;

http://www.phpeasystep.com/phptu/8.html
 
make sure to validate your php form, check security, spammers still use badly programmed forms as mail relays to send bulk mail.
 
I use this nice little function to remove any nastiness:

Code:
function spam_scrubber($value) { // removes potentially malicious code from the email
        
        $very_bad = array('to:', 'cc:', 'bcc:', 'content-type:', 'mime-version:', 'multipart-mixed:','content-transfer-encoding:');
        
        foreach ($very_bad as $v) {
            
            if (stripos($value, $v) !==false) return '';
            
        }
            
            $value = str_replace(array("\r","\n", "%0a", "�0d"), ' ', $value);
            
            return trim($value);
            
        } // End of spam_scrubber() function.
        
        $scrubbed = array_map('spam_scrubber', $_POST);

You can then access anything posted in the form by using:

Code:
$scrubbed['name'] // replace name with your form value

Always worth sticking some javascript validation in there as well. There are hundreds of scripts out there that will do the job.
 
You always want to check to make sure the mail sent too.
Code:
<?php
if(!mail(mail,values,here)){
     echo "The mail failed to send!";
     //I usually put a backup method here, example a gmail server
}
 
Here is one I have used in the past (have a look at it and try to understand it before you implement it - thats the key to learning scripting):

The HTML:

Code:
<fieldset>

<legend>Contact Form</legend>
<form action="submit.php" method="post" enctype="application/x-www-form-urlencoded">
<ul>
<li><label for="name">Your Name:</label><input type="text" name="name" size="25" maxlength="200" id="yourname" /><br /></li>
<li><label for="email">E-mail:</label><input type="text" name="email" size="25" maxlength="200" id="email" /><br /></li>
<li><label for="phone">Phone:</label><input type="text" name="phone" size="25" maxlength="200" id="phone" /><br/></li>
<li><label for="comments">Your enquiry:</label>
<textarea name="comments" rows="10" cols="40" id="comments"></textarea></li>
</ul>
<p><input type="submit" value="Submit!" /></p>
</form>
</fieldset>
The code for the processing script "submit.php":

Code:
<?php

$name = htmlspecialchars($_POST['name']);

$email    = htmlspecialchars($_POST['email']);

$phone   = htmlspecialchars($_POST['phone']);

$comments = htmlspecialchars($_POST['comments']);

?>

 

<?php



if(isset($_POST['email'])) {

    

    // EDIT THE 2 LINES BELOW AS REQUIRED

    $email_to = "PUT YOUR EMAIL HERE";

    $email_subject = "PUT A SUBJECT HERE";

    

    

    function died($error) {

        // your error code can go here

        echo "We are very sorry, but there were error(s) found with the form you 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['name']) ||

        !isset($_POST['email']) ||

        !isset($_POST['phone']) ||

        !isset($_POST['comments'])) {

        died('We are sorry, but there appears to be a problem with the form you submitted.');     

    }

    

    $name = htmlentities($_POST['name']); // required

    $email = htmlentities($_POST['email']); // required

    $phone = htmlentities($_POST['phone']); // not required

    $comments = htmlentities($_POST['comments']); // required

    

    $error_message = "";

    $email_exp = "^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$";

  if(!eregi($email_exp,$email)) {

    $error_message .= 'The Email Address you entered does not appear to be valid.<br />';

  }

    $string_exp = "^[a-z .'-]+$";

  if(!eregi($string_exp,$yourname)) {

    $error_message .= 'The First Name you entered does not appear to be valid.<br />';

  }

  if(strlen($comments) < 2) {

    $error_message .= 'The Comments you entered do 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 .= "Name: ".clean_string($name)."\n";

    $email_message .= "Email: ".clean_string($email)."\n";

    $email_message .= "Telephone: ".clean_string($phone)."\n";

    $email_message .= "Comments: ".clean_string($comments)."\n";

    

    

// create email headers

$headers = 'From: '.$email."\r\n".

'Reply-To: '.$email."\r\n" .

'X-Mailer: PHP/' . phpversion();

@mail($email_to, $email_subject, $email_message, $headers);

?>

 

<!-- include your own success html here -->

 

<p>Thank you for contacting us, we will reply ASAP</p>



<p>Here is a copy of what you submitted to us:</p>



<ul>

<li>Your name is: <?php echo $_POST['name']; ?></li>



<li>Your e-mail: <?php echo $_POST['email']; ?></li>



<li>Your phone number: <?php echo $_POST['phone']; ?></li>

<li>Comments: <?php echo $_POST['comments']; ?></li>

</ul>

 

<?

}

?>
Hope this helps :).
 
Back
Top