Jump to content

PHP send mail error


newguy1

Recommended Posts

Hi guys please i need your help . i created a HTML/PHP form . The PHP form handles posting to my mail while the HTML is the front face / webpage . Anyway i created a form that has fields such as name (textbox), email(textbox) , inquiry(textarea) , state (textbox) country (dropdown list ) , how did you find us (dropdown list). However whenever i test the form all it posts to my mailbox is name , email , inquiry .  Please how do i make the other fields get recognized / how do i get the details in the other fields to be posted to my mailbox.

Link to comment
Share on other sites

here is the code for the php

<?php

 

$name =  $_POST['username'];

$email = $_POST['email'];

$text = $_POST['inquiry'];

 

 

//To, Subject, Message, Header

mail('support@mysite.com','basic inquiry',"$text",'From: '.

 

$name .'<' . $email  . '>');

 

header('location: step3.html')

 

?>

Link to comment
Share on other sites

in this code you are only grabbing  3 of the 6 input fields from your form (username, email, inquiry)  to get the other ones you need to create the variables 

 

$state =  $_POST['state'];

$country =  $_POST['country'];

$findus =  $_POST['findus'];

 

then i would create a $message variable

 

$message= "Form Message \n\n

Name: $name\n

Email: $email\n

Inquiry: $text\n

State: $state\n

Country:  $country\n

How did you find us: $findus\n";

 

Then for the mail()

 

mail('support@mysite.com','basic inquiry',"$message",'From: '.

 

$name .'<' . $email  . '>');

 

 

 

Link to comment
Share on other sites

add an input field to the form <input type="text" name=="Subject"> then on the receiving side add $subject=$_POST['Subject'];    then  in the mail() function add it to the subject portion

 

mail('support@mysite.com','$subject',"$message",'From: '.

 

$name .'<' . $email  . '>');

Link to comment
Share on other sites

Hi Shawn

 

thanks for your help , My subject area now accepts any title / subject the visitor chooses.

add an input field to the form <input type="text" name=="Subject"> then on the receiving side add $subject=$_POST['Subject'];    then  in the mail() function add it to the subject portion

 

mail('support@mysite.com','$subject',"$message",'From: '.

 

$name .'<' . $email  . '>');

 

However this code didn't work for me .

in this code you are only grabbing  3 of the 6 input fields from your form (username, email, inquiry)  to get the other ones you need to create the variables 

 

$state =  $_POST['state'];

$country =  $_POST['country'];

$findus =  $_POST['findus'];

 

then i would create a $message variable

 

$message= "Form Message \n\n

Name: $name\n

Email: $email\n

Inquiry: $text\n

State: $state\n

Country:  $country\n

How did you find us: $findus\n";

 

Then for the mail()

 

mail('support@mysite.com','basic inquiry',"$message",'From: '.

 

$name .'<' . $email  . '>');

 

 

 

 

 

i used it this way .

<?php

 

$name =  $_POST['username'];

$email = $_POST['email'];

$text = $_POST['inquiry'];

$subject=$_POST['Subject'];

$state=$_POST['state'];

$city=$_POST['city'];

$country=$_POST['country'];

 

$message= "Form Message \n\n

Name: $name\n

Email: $email\n

Inquiry: $text\n

State: $state\n

city: $city\n

Country:  $country\n

How did you find us: $findus\n";

 

 

 

//To, Subject, Message, Header

mail('email@mywebsite.com',"$subject" ,"$text",'From: '.

 

$name .'<' . $email  . '>');

 

header('location: step3.html')

 

?>

 

am i doing anything wrong .

Link to comment
Share on other sites

//To, Subject, Message, Header

mail('email@mywebsite.com',"$subject" ,"$text",'From: '.

 

$name .'<' . $email  . '>');

 

header('location: step3.html')

 

?>

 

you need to change the message part of the mail() function from $text to $message....

 

 

that should do it... and happy new years to you as well...

 

 

 

Link to comment
Share on other sites

can you post the html code for the form..... in order to validate it you will need to use javascript you can either have it check the fields when the submit button is pressed, or on the fly, I prefer on the fly personally it lets the user know as they go .....

 

 

it will be easiest to help you though after i see the html for the form fields....

 

 

Link to comment
Share on other sites

can you post the html code for the form..... in order to validate it you will need to use javascript you can either have it check the fields when the submit button is pressed, or on the fly, I prefer on the fly personally it lets the user know as they go .....

 

 

it will be easiest to help you though after i see the html for the form fields....

 

 

 

 

Here is the code

<?php

$name =$_POST['username'];
$email =$_POST['email'];
$phone=$_POST['phone'];
$subject=$_POST['Subject'];
$addressline1=$_POST['address1'];
$addressline2=$_POST['address2'];
$state=$_POST['state'];
$city=$_POST['city'];
$country=$_POST['country'];


$message= "Form Message \n\n
Name: $name\n
Email: $email\n
Phone: $phone\n
Address line1: $addressline1\n
Address line2: $addressline2\n
State: $state\n
city: $city\n
Country:  $country\n";





//To, Subject, Message, Header
mail('email@mywebsite.com',"$subject" ,"$message",'From: '. 

$name .'<' . $email  . '>');

header('location: step3.html')

?>

Link to comment
Share on other sites

I wrote a rather simple email script that I used when I was stuck to an old cell phone.

Excuse the terrible table set up in send.htm.

 

send_message.php

<?PHP 
// PHP Mail Script
$sendname = "From: awesomeemail@mywebsite.com";
$recvemail = $_POST["recvemail"];
$mailsub = $_POST["mailsub"];
$message =$_POST["message"] . "\n\n\n\n\n\nsent from mywebsite.com, you can not reply to this message";

$Home = "<a href=http://website.com>Home</a>";

if (mail($recvemail,$mailsub,$message,$sendname) == TRUE) {
	echo "Sent message.\n" . $Home;
} else {
	echo "Failed to send message!\n" . $Home;
}
?>

 

send.htm

<html>
<body>
<form method="post" action="Send_Message.php">
	<table frame="box">
		<tr>
			<td>
				<input type="text" name="recvemail" size="40" value="a@b.com"/>
			</td>
		</tr>
		<tr>
			<td>
				<input type="text" name="mailsub" size="40" value="Subject"/>
			</td>
		</tr>
	</table>
	<table frame="box">
		<tr>
			<td>
				<input type="text" name="message" size="40" value="Message"/>
			</td>
		</tr>
		<tr>
			<input type="submit" value="Send"/></form>
		<form action="index.php"><input type="submit" value="Cancel"/></form>
		</tr>
	</table>
</body>
</html>

 

Link to comment
Share on other sites

i came across a simple form validation script here http://www.javascript-coder.com/html-form/javascript-form-validation-example.html , i tried it out and on their website it works however on my own code it doesn't work , i know i am doing something wrong but i just can't spot where my code has gone wrong for example here is the code from the website

 

<form action="" name="myform" >
<table cellspacing="2" cellpadding="2" border="0">
<tr>
  <td align="right">First Name</td>
  <td><input type="text" name="FirstName"></td>
</tr>
<tr>
  <td align="right">Last Name</td>
  <td><input type="text" name="LastName"></td>
</tr>
<tr>
  <td align="right">EMail</td>
  <td><input type="text" name="Email"></td>
</tr>
<tr>
  <td align="right">Phone</td>
  <td><input type="text" name="Phone"></td>
</tr>
<tr>
  <td align="right">Address</td>
  <td><textarea cols="20" rows="5" name="Address"></textarea></td>
</tr>
<tr>
  <td align="right">Country</td>
  <td>
  <SELECT name="Country">
    <option value="" selected>[choose yours]
    <option value="008">Albania
    <option value="012">Algeria
    <option value="016">American Samoa
    <option value="020">Andorra
    <option value="024">Angola
    <option value="660">Anguilla
    <option value="010">Antarctica
    <option value="028">Antigua And Barbuda
    <option value="032">Argentina
    <option value="051">Armenia
    <option value="533">Aruba	
</SELECT>
</td>
</tr>
<tr>
  <td align="right"></td>
  <td><input type="submit" value="Submit"></td>
</tr>
</table>
</form>
<script language="JavaScript" type="text/javascript">
//You should create the validator only after the definition of the HTML form
var frmvalidator = new Validator("myform");
frmvalidator.addValidation("FirstName","req","Please enter your First Name");
frmvalidator.addValidation("FirstName","maxlen=20",
"Max length for FirstName is 20");
frmvalidator.addValidation("FirstName","alpha");

frmvalidator.addValidation("LastName","req");
frmvalidator.addValidation("LastName","maxlen=20");

frmvalidator.addValidation("Email","maxlen=50");
frmvalidator.addValidation("Email","req");
frmvalidator.addValidation("Email","email");

frmvalidator.addValidation("Phone","maxlen=50");
frmvalidator.addValidation("Phone","numeric");

frmvalidator.addValidation("Address","maxlen=50");
frmvalidator.addValidation("Country","dontselect=0");

</script> 

 

in the first line of the code it says "<form action="" name="myform" >" my form only has <form action> and "post" so i am wondering where the did "name" come from and what is its use also what does the line "//You should create the validator only after the definition of the HTML form

var frmvalidator = new Validator("myform");" mean exactly

 

Link to comment
Share on other sites

sorry for the late reply

 

I created a bit of code that makes use of ajax and gets the job done quite nicely

 

put both the files in same directory

 

first file sendmessagephp.php

<?php

//grabs the text that was entered for all the fields

$Name=$_GET['name'];
$Email=$_GET['email'];
$Phone=$_GET['phone'];
$Subject=$_GET['subject'];
$Addressline1=$_GET['address1'];
$Addressline2=$_GET['address2'];
$State=$_GET['state'];
$City=$_GET['city'];
$Country=$_GET['country'];

//function that will see if the field is empty
function is_empty($field, $section)
{
if (empty($field))
{

$word=$section.'!';
return $word;
}

}


//function that will see if the field is an email (checks for an @
function is_email($field, $section)
{
$test=strpos($field, '@');

if($test === false)
{
$word=$section.'!';
return $word;
}

}


//running the is_empty function and creating an error message if any field is empty
$error=$error.is_empty($Name, 'username');
$error=$error.is_empty($Email, 'email');
$error=$error.is_empty($Phone, 'phone');
$error=$error.is_empty($Subject, 'subject');
$error=$error.is_empty($Addressline1, 'addressline1');
$error=$error.is_empty($Addressline2, 'addressline2');
$error=$error.is_empty($State, 'state');
$error=$error.is_empty($City, 'city');
$error=$error.is_empty($Country, 'country');

//running the is_email function and adding to the error message
$error=$error.is_email($Email, 'email');


//if theres an error msg it won't send the email
if (!empty($error))
{

die($error);
}
else
{
// no errors so message is created then sent
$Message= "Form Message \n\n
Name: $Name\n
Email: $Email\n
Phone: $Phone\n
Address line1: $Addressline1\n
Address line2: $Addressline2\n
State: $State\n
city: $City\n
Country:  $Country\n";




//To, Subject, Message, Header
mail('shayward2003@gmail.com',"$Subject" ,"$Message",'From: '. 

$Name .'<' . $Email  . '>');

echo'Message Sent';
}
?>

 

 

html file sendmessage.html

 

<script type="text/javascript">
//ajax function
function sendmessage(str)
{

xmlhttp=GetXmlHttpObject();
if (xmlhttp==null)
  {
  alert ("Browser does not support HTTP Request");
  return;
  }
  
//url to the php script
var url="sendmailphp.php";

//creating variables from the input fields
name=document.getElementById('username').value;
email=document.getElementById('email').value;
phone=document.getElementById('phone').value;
subject=document.getElementById('subject').value;
address1=document.getElementById('addressline1').value;
address2=document.getElementById('addressline2').value;
state=document.getElementById('state').value;
city=document.getElementById('city').value;
country=document.getElementById('country').value;

//adding the variables to the url
url=url+"?name="+name+"&email="+email+"&phone="+phone+"&subject="+subject+"&address1="+address1+"&address2="+address2+"&state="+state+"&city="+city+"&country="+country;

//function running the variables from the php
xmlhttp.onreadystatechange=stateChanged;

//sending the variables
xmlhttp.open("GET",url,true);
xmlhttp.send(null);

}


function stateChanged()
{
if (xmlhttp.readyState==4)
{
//turns the background of all invalid fields red
emptysections=xmlhttp.responseText;

test=emptysections.split('!');


for(var i in test)
{
if (test[i]!='')
{
    document.getElementById(test[i]).style.background='#FC9090';
}
}

}
}


//function needed for ajax
function GetXmlHttpObject()
{
if (window.XMLHttpRequest)
  {
  // code for IE7+, Firefox, Chrome, Opera, Safari
  return new XMLHttpRequest();

  }
if (window.ActiveXObject)
  {
  // code for IE6, IE5
  return new ActiveXObject("Microsoft.XMLHTTP");
  }
return null;
}


</script>
<table border="0" style="border-collapse:collapse;">

<tr><td>Name:</td><td> <input type="text" id="username" onfocus="this.style.background='';"></td></tr>
<tr><td>Email:</td><td> <input type="text" id="email" onfocus="this.style.background='';"></td></tr>
<tr><td>Phone: </td><td><input type="text" id="phone" onfocus="this.style.background='';"></td></tr>
<tr><td>Subject:</td><td> <input type="text" id="subject" onfocus="this.style.background='';"></td></tr>
<tr><td>Address Line 1:</td><td> <input type="text" id="addressline1" onfocus="this.style.background='';"></td></tr>
<tr><td>Address Line 2:</td><td> <input type="text" id="addressline2" onfocus="this.style.background='';"></td></tr>
<tr><td>State:</td><td> <input type="text" id="state" onfocus="this.style.background='';"></td></tr>
<tr><td>City:</td><td> <input type="text" id="city" onfocus="this.style.background='';"></td></tr>
<tr><td>Country:</td><td><input type="text" id="country" onfocus="this.style.background='';"></td></tr>
<tr><td><input type="submit" value="Send" onclick="sendmessage();" onfocus="this.style.background='';"></td></tr>

</table>







 

 

i added comment sections throughout both scripts to help you understand

demo of the code can be seen here  http://bit.ly/elJcAY

 

If you have any questions let me know

 

Shawn

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.