Click here to Skip to main content
15,884,836 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
Hi friends, I am new PHP. in this I want to send an email from my Static page which has some text fields and the user has to fill all the fields and then on clicking the submit button it has to send email to my email address(yahoo mail address)

I have done this but I am un able to send from Firefox but I am able to send from chrome and at the same time my page is re directing to PHP page which is not supposed to do that. here is the my code. Please find the bug in that and tell me the correct answer

XML
<input name="name" type="text" id="name" value="" />
<input name="email" type="text" id="email" value="" />
<input name="website" type="text" id="loc" value="" />
<input name="subject" type="text" id="phone"  value="" />
<textarea name="message" rows="20" cols="50"  id="message" ></textarea>
<input type="submit"  value="Submit" class="button" onClick="validate();">
                     <input type="reset" value="Reset" class="button">

var fname,phone,email,message,loc;
function validate()
{
    fname=document.getElementById('name').value;// alert(fname);
    loc=document.getElementById('loc').value;//alert(loc);
    phone=document.getElementById('phone').value;//alert(phone);
    email=document.getElementById('email').value;//alert(email);
    message=document.getElementById('message').value;//alert(message);
    if(notEmpty(fname,'Please Enter First Name') && notEmpty(loc,'Please Enter location') && notEmpty(message,'Please Enter Your Message'))
    {
        if(email!="")
        {
            if(emailValidator(email,'please Enter Valid MailId'));
        }
    //document.form1.submit();
    document.getElementById('conmsg').innerHTML="Processing....Please Wait..!";
    contactUs();
    }
}

function contactUs()
{
    var xml = null;
    if (window.XMLHttpRequest)
    {// code for IE7+, Firefox, Chrome, Opera, Safari
        xml=new XMLHttpRequest();
    }
    else
    {// code for IE6, IE5
        xml=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xml.onreadystatechange=function()
    {
        if (xml.readyState==4 && xml.status==200)
        {    //alert(xml.responseText);
            document.getElementById('conmsg').innerHTML = xml.responseText;
            document.getElementById('name').value='';
            document.getElementById('loc').value='';
            document.getElementById('phone').value='';
            document.getElementById('email').value='';
            document.getElementById('message').value='';
        }
    }
    var con= fname+":"+phone+":"+email+":"+message+":"+loc;
    xml.open("GET","putcontact.php?convar="+con,true);
    xml.send();
}
///////////////GetXmlHttpObject/////////////
function GetXmlHttpObject()
{
    var xmlHttp=null;
    try
    {
        // Firefox, Opera 8.0+, Safari
        xmlHttp=new XMLHttpRequest();
    }
    catch (e)
    {
        // Internet Explorer
        try
        {
            xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e)
        {
            xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
    }
    return xmlHttp;
}
</script>



and my PHP code is below
PHP
<?php
if($_GET['convar']!="")
{
    $varbles=explode(":",$_GET['convar']);
    $fname=$varbles[0];
    $phone=$varbles[1];
    $email=$varbles[2];
    $message=$varbles[3];
    $loc=$varbles[4];

    $mail_text= "Name:".$fname." \r\n Location: ".$loc." \r\n Mobile:".$phone." \r\n Email:".$email." \r\n Message:".$message."\r\n ";
            $to="ganeshxxxxxxx@yahoo.xxxxx";
            $to2="phaxxxxx@gmail.xxxx";



            //$header='mailserver details';
            $headers =  "From: ganeshxxxxx@yahoo.xxx" . "\r\n" .
                        "Reply-To:ganeshxxxxxx@yahoo.xxx" . "\r\n" .
                        "X-Mailer: PHP/" . phpversion();
            $subject = "Feed Back From Website:";
            $message =$mail_text;
                if(mail($to, $subject, $mail_text, $headers))
                {//we show the good guy only in one case and the bad one for the rest.
                    echo '<font color="#660000">Your message has been sent. Thank you for your Feed back.</font>';
                }
                else
                {
                    echo '<font color="#660000">There was an error in sending email!</font>';
                }

}
?>


Please tell me where I am wrong.
Posted
Comments
Er. Tushar Srivastava 21-Sep-12 10:17am    
Notice the $to = "ganeshxxxxxxx@yahoo.xxxxx" ..... what are these x's for?
jim lahey 21-Sep-12 12:02pm    
It's just possible that the OP doesn't want the whole world knowing their real email address. Keyword: spam.
Er. Tushar Srivastava 21-Sep-12 12:04pm    
Oooooo......... And I didn't gave it a thought :-)

I'm too lazy to find a bug; after all, this is your problem.

But I can see one vulnerability, which is practically much worse than the bug. With the bug, you just fail to send a e-mail. But if you fix the bug, the vulnerability can be used to turn your computer into a zombie sending viruses or spam — in no time.

I explained it in my past answer. Please see:
unable to send mail , it showing the error in below code .[^].

Program safely,
—SA
 
Share this answer
 
The error is arising from javascript, not php. You can use Firefox Error Console to see the error. It will give you some details about it...
Additionally GetXmlHttpObject() function is unnecessary. You didn't need it.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900