Free Web Hosting Forum
(#1 (permalink))
Old
Junior Member
Mtechcenter is on a distinguished road
 
Posts: 12
Join Date: Oct 2011
Default PHP mail() - 10-28-2011, 04:22 PM

Hi i'm having problems with my contact form on my webpage

I'm not receiving the mails from my contact form,

so now i've just setup a very simple contact form for testing.

I did several test but never any result.

this is de code of my contact form:

<form action="FormToEmail.php" method="post">
<table border="0" style="background:#ececec" cellspacing="5">
<tr align="left"><td>Name</td><td><input type="text" size="30" name="name"></td></tr>
<tr align="left"><td>Email address</td><td><input type="text" size="30" name="email"></td></tr>
<tr align="left"><td valign="top">Comments</td><td><textarea name="comments" rows="6" cols="30"></textarea></td></tr>
<tr align="left"><td>&nbsp;</td><td><input type="submit" value="Send"></td></tr>
</table>
</form>


This is the code of my FormToEmail.php :

<?php

error_reporting(E_ALL ^ E_NOTICE);

$my_email = "mtechcenter@mtechcenter.site88.net";

$from_email = "mtechcenter@mtechcenter.site88.net";

$continue = "/";

$errors = array();

// Remove $_COOKIE elements from $_REQUEST.

if(count($_COOKIE)){foreach(array_keys($_COOKIE) as $value){unset($_REQUEST[$value]);}}

// Validate email field.

if(isset($_REQUEST['email']) && !empty($_REQUEST['email']))
{

$_REQUEST['email'] = trim($_REQUEST['email']);

if(substr_count($_REQUEST['email'],"@") != 1 || stristr($_REQUEST['email']," ") || stristr($_REQUEST['email'],"\\") || stristr($_REQUEST['email'],":")){$errors[] = "Email address is invalid";}else{$exploded_email = explode("@",$_REQUEST['email']);if(empty($exploded_email[0]) || strlen($exploded_email[0]) > 64 || empty($exploded_email[1])){$errors[] = "Email address is invalid";}else{if(substr_count($exploded_email[1],".") == 0){$errors[] = "Email address is invalid";}else{$exploded_domain = explode(".",$exploded_email[1]);if(in_array("",$exploded_domain)){$errors[] = "Email address is invalid";}else{foreach($exploded_domain as $value){if(strlen($value) > 63 || !preg_match('/^[a-z0-9-]+$/i',$value)){$errors[] = "Email address is invalid"; break;}}}}}}

}

// Check referrer is from same site.

if(!(isset($_SERVER['HTTP_REFERER']) && !empty($_SERVER['HTTP_REFERER']) && stristr($_SERVER['HTTP_REFERER'],$_SERVER['HTTP_HOST']))){$errors[] = "You must enable referrer logging to use the form";}

// Check for a blank form.

function recursive_array_check_blank($element_value)
{

global $set;

if(!is_array($element_value)){if(!empty($element_v alue)){$set = 1;}}
else
{

foreach($element_value as $value){if($set){break;} recursive_array_check_blank($value);}

}

}

recursive_array_check_blank($_REQUEST);

if(!$set){$errors[] = "You cannot send a blank form";}

unset($set);

// Display any errors and exit if errors exist.

if(count($errors)){foreach($errors as $value){print "$value<br>";} exit;}

if(!defined("PHP_EOL")){define("PHP_EOL", strtoupper(substr(PHP_OS,0,3) == "WIN") ? "\r\n" : "\n");}

// Build message.

function build_message($request_input){if(!isset($message_o utput)){$message_output ="";}if(!is_array($request_input)){$message_out put = $request_input;}else{foreach($request_input as $key => $value){if(!empty($value)){if(!is_numeric($key)){$ message_output .= str_replace("_"," ",ucfirst($key)).": ".build_message($value).PHP_EOL.PHP_EOL;}else{$mes sage_output .= build_message($value).", ";}}}}return rtrim($message_output,", ");}

$message = build_message($_REQUEST);

$message = $message . PHP_EOL.PHP_EOL."-- ".PHP_EOL."Thank you for using FormToEmail from http://FormToEmail.com";

$message = stripslashes($message);

$subject = "FormToEmail Comments";

$subject = stripslashes($subject);

if($from_email)
{

$headers = "From: " . $from_email;
$headers .= PHP_EOL;
$headers .= "Reply-To: " . $_REQUEST['email'];

}
else
{

$from_name = "";

if(isset($_REQUEST['name']) && !empty($_REQUEST['name'])){$from_name = stripslashes($_REQUEST['name']);}

$headers = "From: {$from_name} <{$_REQUEST['email']}>";

}

mail($my_email,$subject,$message,$headers);

?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body bgcolor="#ffffff" text="#000000">

<div>
<center>
<b>Thank you <?php if(isset($_REQUEST['name'])){print stripslashes($_REQUEST['name']);} ?></b>
<br>Your message has been sent
<p><a href="<?php print $continue; ?>">Click here to continue</a></p>

</center>
</div>

</body>
</html>

the e-mail adres i use is mtechcenter@mtechcenter.site88.net

and the site i host is mtechcenter.site88.net

So can anyone help me with this please?

Thanks in advance

Sam
Reply With Quote
Sponsored Links
(#2 (permalink))
Old
SGuiGGZ's Avatar
Junior Member
SGuiGGZ is on a distinguished road
 
Posts: 3
Join Date: Oct 2011
Location: Under your Bed
Default 10-29-2011, 04:22 AM

You may need to set some PHP.ini settings before the mail system will actually send the messages.

ini_set('SMTP','mailserver.domain.com');
ini_set('sendmail_from','generally the from address of sender');

These will only work if your host has SMTP turned on. If they do not, the mail() command will send the e-mail, but won't know that anything went wrong. Contact your hosting provider to see what they suggest for connections. 000webhost, doesn't offer smtp (the service that php's mail() function relies on) but the mail() command will run without returning an error code. Their paid services, however, boast smtp.

Quick version: If you're not paying for it, and your hosted here, you can't use mail() without setting the php_ini header to a valid smtp relay hosted by some other company. Just a tip, the five bucks a month hosting here is cheaper than any reliable smtp forwarder i can find.
Reply With Quote
(#3 (permalink))
Old
SGuiGGZ's Avatar
Junior Member
SGuiGGZ is on a distinguished road
 
Posts: 3
Join Date: Oct 2011
Location: Under your Bed
Default 10-30-2011, 10:39 PM

Hello again, I've an adjustment to recommend.
Remove the ini_set('sendmail_from','generally the from address of sender');

and modify the ini_set to be ini_set('SMTP','smtp.000webhost.com');

make sure these lines are added BEFORE the mail() command.
Reply With Quote
(#4 (permalink))
Old
Junior Member
Mtechcenter is on a distinguished road
 
Posts: 12
Join Date: Oct 2011
Default 10-30-2011, 11:09 PM

Thank you for your reply, i tried your advice
but it still doesn't do a thing.

Greets Sam
Reply With Quote
(#5 (permalink))
Old
Member
Hans Henrik is on a distinguished road
 
Posts: 57
Join Date: Oct 2011
Location: Norway
Default 10-31-2011, 10:19 AM

basically theres just a bunch of minor typing/space errors in your code...
here is a fixed (and AStyle'd) version:
PHP Code:
<?php

error_reporting
(E_ALL E_NOTICE);

$my_email "mtechcenter@mtechcenter.site88.net";

$from_email "mtechcenter@mtechcenter.site88.net";

$continue "/";

$errors = array();

// Remove $_COOKIE elements from $_REQUEST.

if(count($_COOKIE))
{
    foreach(
array_keys($_COOKIE) as $value)
    {
        unset(
$_REQUEST[$value]);
    }
}

// Validate email field.

if(isset($_REQUEST['email']) && !empty($_REQUEST['email']))
{

    
$_REQUEST['email'] = trim($_REQUEST['email']);

    if(
substr_count($_REQUEST['email'],"@") != || stristr($_REQUEST['email']," ") || stristr($_REQUEST['email'],"\\") || stristr($_REQUEST['email'],":"))
    {
        
$errors[] = "Email address is invalid";
    }
    else
    {
        
$exploded_email explode("@",$_REQUEST['email']);
        if(empty(
$exploded_email[0]) || strlen($exploded_email[0]) > 64 || empty($exploded_email[1]))
        {
            
$errors[] = "Email address is invalid";
        }
        else
        {
            if(
substr_count($exploded_email[1],".") == 0)
            {
                
$errors[] = "Email address is invalid";
            }
            else
            {
                
$exploded_domain explode(".",$exploded_email[1]);
                if(
in_array("",$exploded_domain))
                {
                    
$errors[] = "Email address is invalid";
                }
                else
                {
                    foreach(
$exploded_domain as $value)
                    {
                        if(
strlen($value) > 63 || !preg_match('/^[a-z0-9-]+$/i',$value))
                        {
                            
$errors[] = "Email address is invalid";
                            break;
                        }
                    }
                }
            }
        }
    }

}

// Check referrer is from same site.

if(!(isset($_SERVER['HTTP_REFERER']) && !empty($_SERVER['HTTP_REFERER']) && stristr($_SERVER['HTTP_REFERER'],$_SERVER['HTTP_HOST'])))
{
    
$errors[] = "You must enable referrer logging to use the form";
}

// Check for a blank form.

function recursive_array_check_blank($element_value)
{

    global 
$set;

    if(!
is_array($element_value))
    {
        if(!empty(
$element_value))
        {
            
$set 1;
        }
    }
    else
    {

        foreach(
$element_value as $value)
        {
            if(
$set)
            {
                break;
            }
            
recursive_array_check_blank($value);
        }

    }

}

recursive_array_check_blank($_REQUEST);

if(!
$set)
{
    
$errors[] = "You cannot send a blank form";
}

unset(
$set);

// Display any errors and exit if errors exist.

if(count($errors))
{
    foreach(
$errors as $value)
    {
        print 
"$value<br>";
    }
    exit;
}

if(!
defined("PHP_EOL"))
{
    
define("PHP_EOL"strtoupper(substr(PHP_OS,0,3) == "WIN") ? "\r\n" "\n");
}

// Build message.

function build_message($request_input)
{
    if(!isset(
$message_output))
    {
        
$message_output ="";
    }
    if(!
is_array($request_input))
    {
        
$message_output $request_input;
    }
    else
    {
        foreach(
$request_input as $key => $value)
        {
            if(!empty(
$value))
            {
                if(!
is_numeric($key))
                {
                    
$message_output .= str_replace("_"," ",ucfirst($key)).": ".build_message($value).PHP_EOL.PHP_EOL;
                }
                else
                {
                    
$message_output .= build_message($value).", ";
                }
            }
        }
    }
    return 
rtrim($message_output,", ");
}

$message build_message($_REQUEST);

$message $message PHP_EOL.PHP_EOL."-- ".PHP_EOL."Thank you for using FormToEmail from http://FormToEmail.com";

$message stripslashes($message);

$subject "FormToEmail Comments";

$subject stripslashes($subject);

if(
$from_email)
{

    
$headers "From: " $from_email;
    
$headers .= PHP_EOL;
    
$headers .= "Reply-To: " $_REQUEST['email'];

}
else
{

    
$from_name "";

    if(isset(
$_REQUEST['name']) && !empty($_REQUEST['name']))
    {
        
$from_name stripslashes($_REQUEST['name']);
    }

    
$headers "From: {$from_name} <{$_REQUEST['email']}>";

}
/**
echo '$my_email:';
var_dump($my_email);
echo '$subject:';
var_dump($subject);
echo '$message:';
var_dump($message);
echo '$headers:';
var_dump($headers);
*/
mail($my_email,$subject,$message,$headers);

?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body bgcolor="#ffffff" text="#000000">

<div>
<center>
<b>Thank you <?php if(isset($_REQUEST['name'])){print stripslashes($_REQUEST['name']);} ?></b>
<br>Your message has been sent
<p><a href="<?php print $continue?>">Click here to continue</a></p>

</center>
</div>

</body>
</html>
your welcome

and on 000webhost, you dont have to ini_set anything for using the mail() function.. but Do Not Use mb_send_mail() ( http://www.000webhost.com/forum/customer-assistance/26849-disabled-security-function-mail-encoding-function.html ) function on 000webhost... (i don't know why, i have asked about it, but not gotten any good answers as of yet. its disabled in php.ini)

Last edited by Hans Henrik; 10-31-2011 at 10:28 AM.
Reply With Quote
(#6 (permalink))
Old
Junior Member
Mtechcenter is on a distinguished road
 
Posts: 12
Join Date: Oct 2011
Default 10-31-2011, 10:56 AM

Thank you hans henrik, i'll try it right away,,

besides is it possible that you have tested this form,because i got an email with this:
Name: test

Email: test@test.test

Comments: test

greets sam
Reply With Quote
(#7 (permalink))
Old
Member
Hans Henrik is on a distinguished road
 
Posts: 57
Join Date: Oct 2011
Location: Norway
Default 10-31-2011, 10:57 AM

yeap, that was me, hope it wasn't a problem
(i put it up at www.hanshenrik.tk/mail.php just to test, will remove it soon edit: didnt remove it (yet at least), but changed addresses)


to get my attention, send a private message

Last edited by Hans Henrik; 10-31-2011 at 11:06 AM.
Reply With Quote
(#8 (permalink))
Old
Junior Member
Mtechcenter is on a distinguished road
 
Posts: 12
Join Date: Oct 2011
Default 10-31-2011, 11:04 AM

No not a problem at all., i'm glad that you could help me..
I've tested it and i works great ..

Thank you very much..
Reply With Quote
(#9 (permalink))
Old
Junior Member
Mtechcenter is on a distinguished road
 
Posts: 12
Join Date: Oct 2011
Default 10-31-2011, 04:40 PM

The first time i used it , it worked great and now it doen't anymore.
But i didn't changed a thing :/ so what could be wrong now ?
i've read in other posts that not every email comes trough but i've sended at least 10 mails with the form and i don't receive a single one of them.
Reply With Quote
(#10 (permalink))
Old
Member
Hans Henrik is on a distinguished road
 
Posts: 57
Join Date: Oct 2011
Location: Norway
Default 10-31-2011, 05:00 PM

(remember to remove the /** */ parts!)
replace this:
PHP Code:
/**
echo '$my_email:';
var_dump($my_email);
echo '$subject:';
var_dump($subject);
echo '$message:';
var_dump($message);
echo '$headers:';
var_dump($headers);
*/
mail($my_email,$subject,$message,$headers); 
with this:
PHP Code:
$logfile=fopen('maillog.html','a+');

fwrite($logfile,'<br/>$my_email:');
fwrite($logfile,htmlentities(var_export($my_email,true)));

fwrite($logfile,'<br/>$subject:');
fwrite($logfile,htmlentities(var_export($subject,true)));

fwrite($logfile,'<br/>$message:');
fwrite($logfile,htmlentities(var_export($message,true)));

fwrite($logfile,'<br/>$headers:');
fwrite($logfile,htmlentities(var_export($headers,true)));

fwrite($logfile,'<br/>mail() return value:');
fwrite($logfile,var_export(mail($my_email,$subject,$message,$headers),true));

fclose($logfile); 
then try to send a mail. next time it does not work, check the maillog.html - what does it say?


to get my attention, send a private message
Reply With Quote
Reply

Tags
mail(), php

Thread Tools
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

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




Powered by vBulletin® Version 3.8.2
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.5.2
vBulletin Skin developed by: vBStyles.com