1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

php Email Notification

Discussion in 'PHP & Perl' started by crosscheck, May 14, 2011.

  1. crosscheck

    crosscheck Regular Member

    Joined:
    Aug 12, 2010
    Messages:
    412
    Likes Received:
    221
    Occupation:
    The Boss
    Location:
    West Coast EH
    I am looking for a script that will send a email when a button is clicked or after a form is submited
     
  2. OrderZero

    OrderZero Newbie

    Joined:
    May 4, 2011
    Messages:
    7
    Likes Received:
    2
    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?
     
    • Thanks Thanks x 1
  3. gnote

    gnote Registered Member

    Joined:
    Mar 10, 2009
    Messages:
    80
    Likes Received:
    6
    Occupation:
    Programmer
    Location:
    USA
    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 <webmaster@example.com>' [/COLOR][COLOR=#007700]. [/COLOR][COLOR=#dd0000]"\r\n" [/COLOR][COLOR=#007700].
        [/COLOR][COLOR=#dd0000]'Reply-To: SiteName <webmaster@example.com>' [/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] 
         
    
     
  4. OrderZero

    OrderZero Newbie

    Joined:
    May 4, 2011
    Messages:
    7
    Likes Received:
    2
    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');
    ?>
     
  5. yohame

    yohame Newbie

    Joined:
    Jan 8, 2011
    Messages:
    17
    Likes Received:
    0
    Home Page:
    It's great info. I'm looking for it.
     
  6. aReJay

    aReJay Power Member

    Joined:
    Apr 29, 2009
    Messages:
    736
    Likes Received:
    237
    Location:
    Down under
    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";
    }
     
  7. takita

    takita Newbie

    Joined:
    Jun 20, 2009
    Messages:
    28
    Likes Received:
    70
    Google "php mail" and click on the w3schools link (The one I use); Super easy to use. enjoy.
     
  8. VIC SEO

    VIC SEO Elite Member

    Joined:
    Feb 19, 2010
    Messages:
    2,156
    Likes Received:
    363
    Gender:
    Male
    Occupation:
    SEO Specialist
    Location:
    iSynergyMedia
    Home Page:
  9. goodbuyer

    goodbuyer Junior Member

    Joined:
    Aug 13, 2010
    Messages:
    118
    Likes Received:
    18
    make sure to validate your php form, check security, spammers still use badly programmed forms as mail relays to send bulk mail.
     
  10. stoichen

    stoichen Newbie

    Joined:
    Jan 6, 2009
    Messages:
    46
    Likes Received:
    10
    Location:
    Scotland
    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.
     
  11. AZer0

    AZer0 Newbie

    Joined:
    May 6, 2011
    Messages:
    26
    Likes Received:
    3
    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
    }
     
  12. sfidirectory

    sfidirectory Senior Member

    Joined:
    Mar 29, 2010
    Messages:
    899
    Likes Received:
    483
    Occupation:
    Web developer/BTC enthusiast
    Location:
    php artisan make:migration
    Home Page:
    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 :).
     
  13. EJIKK

    EJIKK Newbie

    Joined:
    Aug 2, 2011
    Messages:
    17
    Likes Received:
    0
    it's so simple to google it, crosscheck )