TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 08-28-2009, 09:40 PM   #1 (permalink)
The Acquainted
 
Randy's Avatar
 
Join Date: May 2007
Location: Your G/F's Closet
Posts: 114
Thanks: 7
Randy is on a distinguished road
Default Sending Mail

Alright so your first thought is this isnt advanced probably but in my opinion it is.

So i am using this script called 'contact-pop' its a jquery model that uses an ajax powered php form to send mail. It works all good before i edit it how i want.

I want to make it so that the user picks a subject and it sends to a certain email. I've got the switching email part down uses if and elseif statements but i am having some issues with sending.

See Code At: http://pastie.org/598270

It is attempting to send the email on line 134

email it is going to is defined on line 22
__________________
Real Programmers always confuse Christmas and Halloween because Oct31 == Dec25 - Andrew Rutherford
Send a message via AIM to Randy Send a message via MSN to Randy
Randy is offline  
Reply With Quote
Old 08-29-2009, 05:47 AM   #2 (permalink)
The Addict
 
Join Date: May 2009
Posts: 287
Thanks: 5
adamdecaf is on a distinguished road
Default

Please post your code on the post, it helps others.

PHP Code:
<?php

class ContactPop {
    
/****** config *********/

    // the title of the emails that the form sends
    
var $emailTitle 'MYSITE Contact Form';

    var 
$thankYouMessage "Thank you for contacting us, we'll get back to you shortly.";

    
/******* end config ********/


    
var $error '';

    function 
getFormHtml($ajax 0) {

        
$postedName $_POST['name'];
        
$postedEmail $_POST['email'];
        
$postedMessage $_POST['message'];

        
$sendto $_POST['subject'];
        if (
$sendto == 'option1')
        { 
$recipient 'option1@mail.com'; }
        elseif (
$sendto == 'option2')
        { 
$recipient 'option2@mail.com'; }
        elseif (
$sendto == 'option3')
        { 
$recipient 'option3@mail.com'; }
        elseif (
$sendto == 'option4')
        { 
$recipient 'option4@mail.com'; }
        elseif (
$sendto == 'optoin5')
        { 
$recipient 'option5@mail.com'; }


        
$formHtml '';

        
// send congratulations message
        
if ( isset($_POST['httpReferer']) && !$this->error ) {
            
$out '<p id="contact-pop-error" class="formItem">' $this->thankYouMessage '</p>';

            if ( 
$ajax $out .= '<a href="#" class="close-overlay">Close</a>';

            return 
$out;
        }

        if ( 
$this->error $formHtml .= '<p id="contact-pop-error" class="formItem">' $this->error '</p>';


        
$httprefi $_SERVER["HTTP_REFERER"];

        
$cancelLink $ajax '<a href="#" class="close-overlay">Cancel</a>' '';

        
$formHtml .= <<<EOT
        
        <input type="hidden" name="httpReferer" value="
$httprefi" />
        
        <div class="formItem">
            <label>Pick a relevant subject:</label>
            <select class="inputText2" name="subject">
            <option class="dropdown" value="option1">option1</option>
            <option class="dropdown" value="option2">option2</option>
            <option class="dropdown" value="option3">option3</option>
            <option class="dropdown" value="option4">option4</option>
            <option class="dropdown" value="option5">option5</option>
            </select>
        </div>
        
        <div class="formItem">
            <label>Name:</label>
            <input type="text" name="name" class="inputText" value="
$postedName" size="35" />
        </div>
        
        <div class="formItem">
            <label>Company:</label>
            <input type="text" name="company" class="inputText" value="
$postedCompany" size="35" />
        </div>
        
        <div class="formItem">
            <label>Email:</label>
            <input type="text" name="email" class="inputText" value="
$postedEmail" size="35" />
        </div>
        
        <div class="formItem">
            <label>Website:</label>
            <input type="text" name="website" class="inputText" value="
$postedWebsite" size="35" />
        </div>
        
        <div class="formItem">
            <label>Message:</label>
            <textarea name="message" class="textarea" rows="7" cols="38">
$postedMessage</textarea>
        </div>
        
        <div class="formItem">
            <input type="submit" value="Send Message" class="submit" />
        </div>
        
EOT;

        return 
$formHtml;
    }

    function 
checkEmail($emailAddress) {
        if (
preg_match('/[^\x00-\x20()<>@,;:\\".[\]\x7f-\xff]+(?:\.[^\x00-\x20()<>@,;:\\".[\]\x7f-\xff]+)*\@[^\x00-\x20()<>@,;:\\".[\]\x7f-\xff]+(?:\.[^\x00-\x20()<>@,;:\\".[\]\x7f-\xff]+)+/i'$emailAddress)){
            
$emailArray explode("@",$emailAddress);
            if (
checkdnsrr($emailArray[1])){
                return 
TRUE;
            }
        }
        return 
false;
    }

    function 
processForm() {

        
// check data
        
if ( !$_POST['name'] ) $this->error .= 'Please enter your name<br />';
        if ( !
$this->checkEmail$_POST['email'] ) ) $this->error .= 'Please enter a valid email address<br />'
        if ( !
$_POST['message'] ) $this->error .= 'Please enter a message<br />';

        if ( !
$this->error $this->sendFormEmail();
    }

    function 
sendFormEmail() {
        
$message "Name: " stripslashes($_POST['name']) . 
            
"\nEmail: " $_POST['email'] . 
            
"\n\nMessage: " stripslashes($_POST['message']);

        if ( 
$_POST['ajaxForm'] ) $message .= "\n\nFrom a Contact-Pop Form on page: " $_SERVER["HTTP_REFERER"];
        else 
$message .= "\n\nReferrer: " $_POST['httpReferer'];

        
$message .= "\n\nBrowser Info: " $_SERVER["HTTP_USER_AGENT"] .
            
"\nIP: " $_SERVER["REMOTE_ADDR"] .
            
"\n\nDate: " date("Y-m-d h:i:s");

        
mail($this->sendto$this->emailTitle$message'From: ' $_POST['name'] . ' <' $_POST['email'] . '>');
    }
}

$contactPop = new ContactPop();

if (isset(
$_POST['httpReferer'])) $contactPop->processForm();

// echo the ajax version of the form
if ( isset($_REQUEST['ajaxForm']) && $_REQUEST['ajaxForm']) {
    echo 
$contactPop->getFormHtml(1);
}
// or echo the full page version of the form
else {

?>

    <?=$contactPop->getFormHtml(); ?>

<?php 
}
?>
The If/Then/Else If statement contains no "else" block, so if something fails all the tests it is not modified.
__________________
My Site
adamdecaf is offline  
Reply With Quote
Old 08-29-2009, 01:57 PM   #3 (permalink)
The Contributor
 
ioan1k's Avatar
 
Join Date: Mar 2009
Location: US
Posts: 76
Thanks: 0
ioan1k is on a distinguished road
Default

It would be much easier to use a switch block

php Code:
switch ($_POST['subject']) {
    default:
    case 'option1':
        $recipiant = 'option1@mail.com';
        break;
    case 'option2':
        $recipiant = 'option2@mail.com';
        break;
    case 'option3':
        $recipiant = 'option3@mail.com';
        break;
    case 'option4':
        $recipiant = 'option4@mail.com';
        break;
    case 'option5':
        $recipiant = 'option5@mail.com';
        break;
}
__________________
My Portfolio - Work - Need freelance Work?
I've been developing 5 years now, and I learn something new everyday
ioan1k is offline  
Reply With Quote
Old 08-29-2009, 07:20 PM   #4 (permalink)
The Acquainted
 
Randy's Avatar
 
Join Date: May 2007
Location: Your G/F's Closet
Posts: 114
Thanks: 7
Randy is on a distinguished road
Default

Alright so i fixed the if statement to and have an else and it still doesnt seem to be working:

PHP Code:
<?php


class ContactPop {
    
/****** config *********/
       
    // the title of the emails that the form sends
    
var $emailTitle 'AppQuack Contact Form';
    
    var 
$thankYouMessage "Thank you for contacting us, we'll get back to you shortly.";
    
    
/******* end config ********/
    
    
    
var $error '';
    
    function 
getFormHtml($ajax 0) {
        
        
$postedName $_POST['name'];
        
$postedEmail $_POST['email'];
        
$postedMessage $_POST['message'];
        
        
$sendto $_POST['subject'];
        if (
$sendto == 'webquack')
        { 
$recipient 'web@appquack.com'; }
        elseif (
$sendto == 'macquack')
        { 
$recipient 'mac@appquack.com'; }
        elseif (
$sendto == 'winquack')
        { 
$recipient 'win@appquack.com'; }
        elseif (
$sendto == 'interview')
        { 
$recipient 'interview@appquack.com'; }
        elseif (
$sendto == 'general')
        { 
$recipient 'general@appquack.com'; }
        else { 
$recipient 'general@appquack.com'; }
        
        
$formHtml '';
        
        
// send congratulations message
        
if ( isset($_POST['httpReferer']) && !$this->error ) {
            
$out '<p id="contact-pop-error" class="formItem">' $this->thankYouMessage '</p>';
            
            if ( 
$ajax $out .= '<a href="#" class="close-overlay">Close</a>';
            
            return 
$out;
        }
        
        if ( 
$this->error $formHtml .= '<p id="contact-pop-error" class="formItem">' $this->error '</p>';

        
        
$httprefi $_SERVER["HTTP_REFERER"];
        
        
$cancelLink $ajax '<a href="#" class="close-overlay">Cancel</a>' '';
        
        
$formHtml .= <<<EOT
        
        <input type="hidden" name="httpReferer" value="
$httprefi" />
        
        <div class="formItem">
            <label>Pick a relevant subject:</label>
            <select class="inputText2" name="subject">
            <option class="dropdown" value="webquack">WebQuack</option>
            <option class="dropdown" value="macquack">MacQuack</option>
            <option class="dropdown" value="winquack">WinQuack</option>
            <option class="dropdown" value="interview">Interview Requests</option>
            <option class="dropdown" value="general">General Inquiry</option>
            </select>
        </div>
        
        <div class="formItem">
            <label>Name:</label>
            <input type="text" name="name" class="inputText" value="
$postedName" size="35" />
        </div>
        
        <div class="formItem">
            <label>Company:</label>
            <input type="text" name="company" class="inputText" value="
$postedCompany" size="35" />
        </div>
        
        <div class="formItem">
            <label>Email:</label>
            <input type="text" name="email" class="inputText" value="
$postedEmail" size="35" />
        </div>
        
        <div class="formItem">
            <label>Website:</label>
            <input type="text" name="website" class="inputText" value="
$postedWebsite" size="35" />
        </div>
        
        <div class="formItem">
            <label>Message:</label>
            <textarea name="message" class="textarea" rows="7" cols="38">
$postedMessage</textarea>
        </div>
        
        <div class="formItem">
            <input type="submit" value="Send Message" class="submit" />
        </div>
        
EOT;
        
        return 
$formHtml;
    }
    
    function 
checkEmail($emailAddress) {
        if (
preg_match('/[^\x00-\x20()<>@,;:\\".[\]\x7f-\xff]+(?:\.[^\x00-\x20()<>@,;:\\".[\]\x7f-\xff]+)*\@[^\x00-\x20()<>@,;:\\".[\]\x7f-\xff]+(?:\.[^\x00-\x20()<>@,;:\\".[\]\x7f-\xff]+)+/i'$emailAddress)){
            
$emailArray explode("@",$emailAddress);
            if (
checkdnsrr($emailArray[1])){
                return 
TRUE;
            }
        }
        return 
false;
    }
    
    function 
processForm() {
        
        
// check data
        
if ( !$_POST['name'] ) $this->error .= 'Please enter your name<br />';
        if ( !
$this->checkEmail$_POST['email'] ) ) $this->error .= 'Please enter a valid email address<br />'
        if ( !
$_POST['message'] ) $this->error .= 'Please enter a message<br />';
        
        if ( !
$this->error $this->sendFormEmail();
    }
    
    function 
sendFormEmail() {
        
$message "Name: " stripslashes($_POST['name']) . 
            
"\nEmail: " $_POST['email'] . 
            
"\n\nMessage: " stripslashes($_POST['message']);
        
        if ( 
$_POST['ajaxForm'] ) $message .= "\n\nFrom a Contact-Pop Form on page: " $_SERVER["HTTP_REFERER"];
        else 
$message .= "\n\nReferrer: " $_POST['httpReferer'];
        
        
$message .= "\n\nBrowser Info: " $_SERVER["HTTP_USER_AGENT"] .
            
"\nIP: " $_SERVER["REMOTE_ADDR"] .
            
"\n\nDate: " date("Y-m-d h:i:s");
        
        
mail($this->sendto$this->emailTitle$message'From: ' $_POST['name'] . ' <' $_POST['email'] . '>');
    }
}

$contactPop = new ContactPop();

if (isset(
$_POST['httpReferer'])) $contactPop->processForm();

// echo the ajax version of the form
if ( isset($_REQUEST['ajaxForm']) && $_REQUEST['ajaxForm']) {
    echo 
$contactPop->getFormHtml(1);
}
// or echo the full page version of the form
else {

?>
    
    <?=$contactPop->getFormHtml(); ?>

<?php 
}
?>
__________________
Real Programmers always confuse Christmas and Halloween because Oct31 == Dec25 - Andrew Rutherford
Send a message via AIM to Randy Send a message via MSN to Randy
Randy is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP Mail - Help sending multiple attachments xperience Absolute Beginners 5 09-06-2010 08:27 AM
Mail function.. Tanax Advanced PHP Programming 8 05-05-2008 08:20 PM
My mail server is making this noise: AAArrruuuuggggha. delayedinsanity General 1 04-25-2008 01:51 PM
mail() always returns false solistus Advanced PHP Programming 1 04-23-2008 07:44 AM
Creating Mail Queues webosb Advanced PHP Programming 1 11-16-2007 01:59 AM


All times are GMT. The time now is 10:24 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design