Hi Forum users,

I have recently tried to put together a php contact form but when it sends it doesn't have the visitors email address. I receive the email with the subject and message addressed to myself from myself. I would also like to add the visitors name on the contact from, but have also failed at attempting that. Adding input type="text" name="Visitor" to the form and

$visitor = Trim(stripslashes($_POST['visitor']));

to the php form data and adding the $vistor in the create and send didn't appear to work.
Could someone please be of assistance?

<?php
require_once "Mail.php";

//Store form data in local variables
$EmailFrom = Trim(stripslashes($_POST['EmailFrom'])); 
$EmailTo = "myemail@gmail.com";
$Subject = Trim(stripslashes($_POST['Subject'])); 
$Message = Trim(stripslashes($_POST['Message']));


// Require fields
$validationOK=true;
if (Trim($EmailFrom)=="") $validationOK=false;
if (!$validationOK) {
  print "<meta http-equiv=\"refresh\" content=\"0;URL=error.html\">";
  exit;
}

$validationOK=true;
if (Trim($Subject)=="") $validationOK=false;
if (!$validationOK) {
  print "<meta http-equiv=\"refresh\" content=\"0;URL=error.html\">";
  exit;
}

$validationOK=true;
if (Trim($Message)=="") $validationOK=false;
if (!$validationOK) {
  print "<meta http-equiv=\"refresh\" content=\"0;URL=error.html\">";
}


//smtp variables
$host = "smtp.gmail.com";
$port = "587";
$username = "myemail@gmail.com";
$password = "xxxxx";


//reCaptcha validation
require_once('recaptchalib.php');
$privatekey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
$resp = recaptcha_check_answer ($privatekey,
                                $_SERVER["REMOTE_ADDR"],
                                $_POST["recaptcha_challenge_field"],
                                $_POST["recaptcha_response_field"]);

if (!$resp->is_valid) {
  print "<meta http-equiv=\"refresh\" content=\"0;URL=captcha.html\">";
  exit;
}


//create and send
$headers = array ('From' => $EmailFrom,
'To' => $EmailTo,
'Subject' => "Web contact - $Subject");
$smtp = Mail::factory('smtp',
array ('host' => $host,
   'auth' => true,
   'port' => $port,
   'username' => $username,
   'password' => $password));

$mail = $smtp->send($EmailTo, $headers, $Message);


//show form submission result
if (PEAR::isError($mail)) {
print "<meta http-equiv=\"refresh\" content=\"0;URL=error.html\">";
exit;
} else {
print "<meta http-equiv=\"refresh\" content=\"0;URL=ok.html\">";
}
?>

Form

<div id="eform">
<center><form method="POST" action="contact.php">
Fields marked (*) are required.

<p>Email Address: *<br>
<input type="text" name="EmailFrom"></p>
<p>Subject: <br>
<input type="text" name="Subject"></p>
<p>Message:<br>
<textarea name="Message" rows="10" cols="30"></textarea>
<p><script type="text/javascript"
   src="http://api.recaptcha.net/challenge?k=6Lc4eAEAAAAAALC7mtowaUZ2z0sVF1pX_txsraLK">
</script></p>
<noscript>
   <iframe src="http://api.recaptcha.net/noscript?k=6Lc4eAEAAAAAALC7mtowaUZ2z0sVF1pX_txsraLK"
       height="300" width="500" frameborder="0"></iframe><br>
   <textarea name="recaptcha_challenge_field" rows="3" cols="40">
   </textarea>
   <input type="hidden" name="recaptcha_response_field" 
       value="manual_challenge">
</noscript>
<p><input type="submit" name="submit" value="Submit"></p>
</form></center>
   </div>

Does anyone know of a good php contact form tutorial using pear?

hey,

underneath:
$EmailFrom = Trim(stripslashes($_POST));

add

$EmailFrom = Trim(stripslashes($_POST));
echo "<h2>$EmailFrom</h2>";

i've not got time to upload the files myself but let me know if it prints the senders email address or not then i'll look into it further for you

Thanks Kevin but it doesn't seem to work.
Still recieve email in my gmail account from myself.

echo "<p><br><H1>Debug</h1>";
echo '<pre>';
echo print_r(get_defined_vars());
echo '</pre>';

add that underneath the last bit of code I gave you then tell me if you can see anything that you typed in the form in amongst all that (for example the EmailFrom variable, etc)

Kevin,

I've posted the whole debug code, just made a few changes to my email address ect. Looks as though the EmailFrom appears quite a few times.

steve@big.com

Debug
Array
(
    [GLOBALS] => Array
        (
            [GLOBALS] => Array
 *RECURSION*
            [_POST] => Array
                (
                    [EmailFrom] => steve@big.com
                    [Subject] => test
                    [Message] => test
                    [recaptcha_challenge_field] => xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
                    [recaptcha_response_field] => making student
                    [submit] => Submit
                )

            [_GET] => Array
                (
                )

            [_COOKIE] => Array
                (
                )

            [_FILES] => Array
                (
                )

            [_SERVER] => Array
                (
                    [ALL_HTTP] => HTTP_CACHE_CONTROL:no-cache
HTTP_CONNECTION:Keep-Alive
HTTP_CONTENT_LENGTH:325
HTTP_CONTENT_TYPE:application/x-www-form-urlencoded
HTTP_ACCEPT:image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*
HTTP_ACCEPT_ENCODING:gzip, deflate
HTTP_ACCEPT_LANGUAGE:en-us
HTTP_HOST:<snipped>
HTTP_REFERER:<snipped>
HTTP_USER_AGENT:Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; InfoPath.1; .NET CLR 2.0.50727)
HTTP_UA_CPU:x86

                    [HTTPS] => off
                    [SCRIPT_NAME] => /contact.php
                    [HTTP_COOKIE] => 
                    [AUTH_PASSWORD] => 
                    [AUTH_TYPE] => 
                    [AUTH_USER] => 
                    [CONTENT_LENGTH] => 325
                    [CONTENT_TYPE] => application/x-www-form-urlencoded
                    [PATH_TRANSLATED] => C:\Inetpub\wwwroot\site
                    [QUERY_STRING] => 
                    [REMOTE_ADDR] => 124.xxx.xxx.xxx
                    [REMOTE_HOST] => 124.xxx.xxx.xxx
                    [REMOTE_USER] => 
                    [REQUEST_METHOD] => POST
                    [SERVER_NAME] => <snipped>
                    [SERVER_PORT] => 80
                    [SERVER_PROTOCOL] => HTTP/1.1
                    [SERVER_SOFTWARE] => Microsoft-IIS/6.0
                    [APPL_MD_PATH] => /LM/W3SVC/1/ROOT
                    [APPL_PHYSICAL_PATH] => C:\Inetpub\wwwroot\site\
                    [INSTANCE_ID] => 1
                    [INSTANCE_META_PATH] => /LM/W3SVC/1
                    [LOGON_USER] => 
                    [REQUEST_URI] => /contact.php
                    [URL] => /contact.php
                    [SCRIPT_FILENAME] => C:\Inetpub\wwwroot\site\contact.php
                    [ORIG_PATH_INFO] => /contact.php
                    [PATH_INFO] => 
                    [ORIG_PATH_TRANSLATED] => C:\Inetpub\wwwroot\site\contact.php
                    [DOCUMENT_ROOT] => C:\Inetpub\wwwroot\site
                    [PHP_SELF] => /contact.php
                    [HTTP_CACHE_CONTROL] => no-cache
                    [HTTP_CONNECTION] => Keep-Alive
                    [HTTP_CONTENT_LENGTH] => 325
                    [HTTP_CONTENT_TYPE] => application/x-www-form-urlencoded
                    [HTTP_ACCEPT] => image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*
                    [HTTP_ACCEPT_ENCODING] => gzip, deflate
                    [HTTP_ACCEPT_LANGUAGE] => en-us
                    [HTTP_HOST] => <snipped>
                    [HTTP_REFERER] => <snipped>
                    [HTTP_USER_AGENT] => Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; InfoPath.1; .NET CLR 2.0.50727)
                    [HTTP_UA_CPU] => x86
                    [REQUEST_TIME] => 1207989030
                )

            [_PEAR_default_error_mode] => 1
            [_PEAR_default_error_options] => 1024
            [_PEAR_destructor_object_list] => Array
                (
                )

            [_PEAR_shutdown_funcs] => Array
                (
                )

            [_PEAR_error_handler_stack] => Array
                (
                )

            [EmailFrom] => steve@big.com
        )

    [_POST] => Array
        (
            [EmailFrom] => steve@big.com
            [Subject] => test
            [Message] => test
            [recaptcha_challenge_field] => xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
            [recaptcha_response_field] => making student
            [submit] => Submit
        )

    [_GET] => Array
        (
        )

    [_COOKIE] => Array
        (
        )

    [_FILES] => Array
        (
        )

    [_SERVER] => Array
        (
            [ALL_HTTP] => HTTP_CACHE_CONTROL:no-cache
HTTP_CONNECTION:Keep-Alive
HTTP_CONTENT_LENGTH:325
HTTP_CONTENT_TYPE:application/x-www-form-urlencoded
HTTP_ACCEPT:image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*
HTTP_ACCEPT_ENCODING:gzip, deflate
HTTP_ACCEPT_LANGUAGE:en-us
HTTP_HOST:<snipped>
HTTP_REFERER:<snipped>
HTTP_USER_AGENT:Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; InfoPath.1; .NET CLR 2.0.50727)
HTTP_UA_CPU:x86

            [HTTPS] => off
            [SCRIPT_NAME] => /contact.php
            [HTTP_COOKIE] => 
            [AUTH_PASSWORD] => 
            [AUTH_TYPE] => 
            [AUTH_USER] => 
            [CONTENT_LENGTH] => 325
            [CONTENT_TYPE] => application/x-www-form-urlencoded
            [PATH_TRANSLATED] => C:\Inetpub\wwwroot\site
            [QUERY_STRING] => 
            [REMOTE_ADDR] => 124.xxx.xxx.xxx
            [REMOTE_HOST] => 124.xxx.xxx.xxx
            [REMOTE_USER] => 
            [REQUEST_METHOD] => POST
            [SERVER_NAME] => <snipped>
            [SERVER_PORT] => 80
            [SERVER_PROTOCOL] => HTTP/1.1
            [SERVER_SOFTWARE] => Microsoft-IIS/6.0
            [APPL_MD_PATH] => /LM/W3SVC/1/ROOT
            [APPL_PHYSICAL_PATH] => C:\Inetpub\wwwroot\site\
            [INSTANCE_ID] => 1
            [INSTANCE_META_PATH] => /LM/W3SVC/1
            [LOGON_USER] => 
            [REQUEST_URI] => /contact.php
            [URL] => /contact.php
            [SCRIPT_FILENAME] => C:\Inetpub\wwwroot\site\contact.php
            [ORIG_PATH_INFO] => /contact.php
            [PATH_INFO] => 
            [ORIG_PATH_TRANSLATED] => C:\Inetpub\wwwroot\site\contact.php
            [DOCUMENT_ROOT] => C:\Inetpub\wwwroot\site
            [PHP_SELF] => /contact.php
            [HTTP_CACHE_CONTROL] => no-cache
            [HTTP_CONNECTION] => Keep-Alive
            [HTTP_CONTENT_LENGTH] => 325
            [HTTP_CONTENT_TYPE] => application/x-www-form-urlencoded
            [HTTP_ACCEPT] => image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*
            [HTTP_ACCEPT_ENCODING] => gzip, deflate
            [HTTP_ACCEPT_LANGUAGE] => en-us
            [HTTP_HOST] => <snipped>
            [HTTP_REFERER] => <snipped>
            [HTTP_USER_AGENT] => Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; InfoPath.1; .NET CLR 2.0.50727)
            [HTTP_UA_CPU] => x86
            [REQUEST_TIME] => 1207989030
        )

    [_PEAR_default_error_mode] => 1
    [_PEAR_default_error_options] => 1024
    [_PEAR_destructor_object_list] => Array
        (
        )

    [_PEAR_shutdown_funcs] => Array
        (
        )

    [_PEAR_error_handler_stack] => Array
        (
        )

    [EmailFrom] => steve@big.com
)
1

ok

now delete/hide the debug code and add to the top of the page:

error_reporting(E_ALL);

and do the form again, and tell me what it says

The senders (email from) address appears and disappears extremly quick.

I tried another smtp host with a better result.
This time my gmail account received the email with the senders email address.
I have now tried to add the senders name to the form using the $Name function but it doesn't appear with the email. I'm not sure if I have included it correctly into the $headers.

<?php
require_once "Mail.php";

//Store form data in local variables
$Name = Trim(stripslashes($_POST['Name']));
$EmailFrom = Trim(stripslashes($_POST['EmailFrom'])); 
$EmailTo = "myemail@gmail.com";
$Subject = Trim(stripslashes($_POST['Subject'])); 
$Message = Trim(stripslashes($_POST['Message']));

//smtp variables
$host = "mail.newhost.com";
$username = "newemail@newhost.com";
$password = "xxxxxx";

//create and send
$headers = array ('Name' => $Name, 'From' => $EmailFrom,
'To' => $EmailTo, 
'Subject' => "Web contact - $Subject");
$smtp = Mail::factory('smtp',
array ('host' => $host,
   'auth' => true,
   'username' => $username,
   'password' => $password));

$mail = $smtp->send($EmailTo, $headers, $Message);


//show form submission result
if (PEAR::isError($mail)) {
print "<meta http-equiv=\"refresh\" content=\"0;URL=error.html\">";
exit;
} else {
print "<meta http-equiv=\"refresh\" content=\"0;URL=ok.html\">";
}
?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.