PHP Form Email Request Not Bringing form data through - php

I have been working on a php script and a html form (both in separate files) that takes data from a form and emails it to me. I am having difficulty getting the form data that the user fills out to come through on the email.
I would like for the email to contain the user's email as the From in the email.
Can anyone help me figure out what is wrong with the files?
HTML Form:
<form id="ContactUs" name="ContactUs" method="post" action="sendform/sendform.php">
<table width="60%" border="0" cellpadding="2px" style="position:relative; left:20%">
<tr>
<td width="25%" align="right">Name:</td>
<td width="75%" align="left"><input name="ContactName" type="text" size="40" maxlength="100" /></td>
</tr>
<tr>
<td align="right">Email Address:</td>
<td align="left"><span id="sprytextfield1">
<input name="EmailAddress" type="text" id="EmailAddress" size="40" maxlength="150" />
<span class="textfieldRequiredMsg">A value is required.</span></span></td>
</tr>
<tr>
<td align="right">Phone Number:</td>
<td align="left"><span id="sprytextfield2">
<label for="PhoneNumber"></label>
<input name="PhoneNumber" type="text" id="PhoneNumber" maxlength="15" />
<span class="textfieldRequiredMsg">A value is required.</span></span></td>
</tr>
<tr>
<td align="right">Practice Name:</td>
<td align="left"><input name="PracticeName" type="text" size="40" maxlength="100" /></td>
</tr>
<tr>
<td align="right">Message:</td>
<td align="left"><textarea name="Message" cols="40" rows="10"> </textarea></td>
</tr>
<tr>
<td align="center" width="25%"></td>
<td align="center" width="75%"><input name="SubmitForm" type="submit" id="SubmitForm" onclick="MM_popupMsg('Are you sure you would like to submit this form?\r');return document.MM_returnValue" value="Submit Form" /><input type="reset" name="ResetForm" id="ResetForm" value="Reset Form" /></td>
</table>
</form>
Separate PHP file named sendform.php
<?php
// Where to redirect after form is processed.
$url = 'ThankYou.php';
// multiple recipients
$to = 'T#domain.com';
// subject
$subject = 'Someone sent you a contact request';
$headers = 'From: '.$EmailAddress.'/r/n';
$headers .= 'MIME-Version: 1.0\r\n';
$headers .= 'Content-Type: text/html; charshet=ISO-8859-1\r\n';
// message
$messagetext = '<html><body>';
$messagetext .= ' <p>Website Form Submit</p>';
$messagetext .= ' <table>';
$messagetext .= ' <tr><td align="right">Name:</td><td>'; $_GET[$ContactName] .'</td></tr>';
$messagetext .= ' <tr><td align="right">Email Address:</td><td align="left">'; $EmailAddress .'</td></tr>';
$messagetext .= ' <tr><td align="right">Phone Number:</td><td align="left">'; $PhoneNumber .'</td></tr>';
$messagetext .= ' <tr><td align="right">Practice Name:</td><td align="left">'; $PracticeName .'</td></tr>';
$messagetext .= ' <tr><td align="right">Message:</td><td align="left">'; $Message .'</textarea></td></tr>';
$messagetext .= ' </table></body></html>';
// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Mail it
mail($to, $subject, $messagetext, $headers);
//echo $message;
echo '<META HTTP-EQUIV=Refresh CONTENT="0; URL='.$url.'">'
?>

You're using POST in your form but you are not referencing the post variables. Instead of $_GET[$ContactName], use $_POST[$ContactName]. Do this for all POSTed variables.

One step at a time! Forget the email function altogether for now, do things in order.
Make your form, then make sure it's displaying as you expect in the browser.
Then on the processing page (sendform/sendform.php), echo out all the data which would be sent from the form to make sure they're all there, present and ok.
ie
print_r($_POST);
THEN once you are happy with that stage, you can apply the vars in the email function and test the final result, tweaking where required or fixing bugs etc.
At present, however, you're not doing anything with the POSTed data. In sendform.php, you have this:
$headers = 'From: '.$EmailAddress.'/r/n';
However there is no reference to it in that file to set it, or set that variable to POSTed or GET data. As such, it'll be empty.
You should check your error logs too, as this would have shown you this var is empty and lead you to back track to your issue. error logs are a must whenever coding in PHP, even for the pro's.
You also have this:
$messagetext .= '<tr><td align="right">Name:</td>
<td>'; $_GET[$ContactName] .'</td></tr>
';
Your form is set to POST data, not GET.
So your data is stored in PHP's $_POST array. So using your form names, the email address would be:
$_POST['EmailAddress'];
// You can use this, or set it to a variable, ie
$EmailAddress = $_POST['EmailAddress'];
Important note:
You really want to validate people's inputs before emailing, or you could open yourself up to be a spammers delight. Such as strlen() to match your form maxlengths, is_numeric(), and regex to check their inputted data is not some Javascript or whatever trying to send thousands of emails through your site (it happens!).
Using them all in conjunction with each other where appropriate, you ensure users can only enter data you allow and you stop any bad things coming through.
Send them back to the form with an error message if there is anything wrong, do this until you're happy the data is acceptable to send through your server's mail system and then allow it.
People using your badly written code to spam the world wont just be an annoyance to you having to resolve that. You could end up having your domain name where the form is hosted blacklisted as a spammy site - not good.

Here is a tested version with the From: now showing in the appropriate location of the Email.
Quick note: There was two seperate header bodies which broke up your code.
One under $subject and the other over $messagetext. All are inside one body of headers now.
Plus, you had $headers = 'From: '.$EmailAddress.'/r/n'; which is invalid.
This should have read as $headers = 'From: '.$EmailAddress.'\r\n'; with \ instead of /.
More on the mail() function can be read by visiting PHP.net on mail() - here.
Working code
<?php
$url = 'ThankYou.php';
$ContactName = $_POST['ContactName'];
$EmailAddress = $_POST['EmailAddress'];
$PhoneNumber = $_POST['PhoneNumber'];
$Message = $_POST['Message'];
$PracticeName = $_POST['PracticeName'];
$to = "email#example.com";
$subject = 'Someone sent you a contact request';
// message
$messagetext = '<html><body>';
$messagetext .= ' <p>Website Form Submit</p>';
$messagetext .= ' <table>';
$messagetext .= ' <tr><td align="right">Name:</td><td>'.$ContactName.'</td></tr>';
$messagetext .= ' <tr><td align="right">Email Address:</td><td align="left">'.$EmailAddress.'</td></tr>';
$messagetext .= ' <tr><td align="right">Phone Number:</td><td align="left">'.$PhoneNumber.'</td></tr>';
$messagetext .= ' <tr><td align="right">Practice Name:</td><td align="left">'.$PracticeName.'</td></tr>';
$messagetext .= ' <tr><td align="right">Message:</td><td align="left">'.$Message.'</textarea></td></tr>';
$messagetext .= ' </table></body></html>';
// To send HTML mail, the Content-type header must be set
$headers = "MIME-Version: 1.0\n";
$headers .= "Content-Type: text/html; charset=iso-8859-1\n";
$headers .= "From: $EmailAddress" . "\r\n" .
"Reply-To: $EmailAddress" . "\r\n" .
'X-Mailer: PHP/' . phpversion();
// Mail it
mail($to, $subject, $messagetext, $headers);
//echo $message;
echo '<META HTTP-EQUIV=Refresh CONTENT="0; URL='.$url.'">';
//echo "Success"; // my testing method
?>
Additional options:
To make sure someone fills in all the fields and not send mail unless they do, then you can use the following and make sure you remove the in your <textarea name="Message"... because that is considered as input.
if(!empty($_POST['EmailAddress']) && (!empty($_POST['ContactName']))
&& (!empty($_POST['PhoneNumber']))
&& (!empty($_POST['PracticeName']))
&& (!empty($_POST['Message']))
)
{
mail($to, $subject, $messagetext, $headers);
//echo $message;
// echo '<META HTTP-EQUIV=Refresh CONTENT="0; URL='.$url.'">';
echo "Success";
}
else {
echo "Mail failed. All fields must be filled.";
}
There are more improvements that can be done, for instance Email validation such as FILTER_VALIDATE_EMAIL.
Example:
$email = $_POST['EmailAddress'];
if(empty($_POST['EmailAddress']) || !filter_var($EmailAddress, FILTER_VALIDATE_EMAIL)) {
die("Please enter a valid email");
}
More on FILTER_VALIDATE_EMAIL can be found on the PHP.net Website
http://php.net/manual/en/filter.filters.validate.php

I have fixed your code. Enter the email in $to variable whom you want to send the email.
<?php
$ContactName = $_POST['ContactName'];
$EmailAddress = $_POST['EmailAddress'];
$PhoneNumber = $_POST['PhoneNumber'];
$Message = $_POST['Message'];
$PracticeName = $_POST['PracticeName'];
$to = 'ENTER EMAIL ADDRESS ON WHICH YOU WANT TO SEND EMAIL';
$subject = 'Someone sent you a contact request';
$headers = 'From: '.$EmailAddress.'/r/n';
$headers .= 'MIME-Version: 1.0\r\n';
$headers .= 'Content-Type: text/html; charshet=ISO-8859-1\r\n';
// message
$messagetext = '<html><body>';
$messagetext .= ' <p>Website Form Submit</p>';
$messagetext .= ' <table>';
$messagetext .= ' <tr><td align="right">Name:</td><td>'.$ContactName.'</td></tr>';
$messagetext .= ' <tr><td align="right">Email Address:</td><td align="left">'.$EmailAddress.'</td></tr>';
$messagetext .= ' <tr><td align="right">Phone Number:</td><td align="left">'.$PhoneNumber.'</td></tr>';
$messagetext .= ' <tr><td align="right">Practice Name:</td><td align="left">'.$PracticeName.'</td></tr>';
$messagetext .= ' <tr><td align="right">Message:</td><td align="left">'.$Message.'</textarea></td></tr>';
$messagetext .= ' </table></body></html>';
// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1'."\r\n";
// Mail it
mail($to, $subject, $messagetext, $headers);
//echo $message;
echo '<META HTTP-EQUIV=Refresh CONTENT="0; URL='.$url.'">';

Related

How to redirect to home page after submitting the Contact Form?

i have created contact form but i am getting two problems
Reply-To mail is not going.
After Submitting the form page has to redirect to Home page.
For reference please find the attached image and code
Below is the PHP code
<?php
if(isset($_POST['submit']))
{
$name = $_POST['name']; // Get Name value from HTML Form
$email_id = $_POST['email']; // Get Email Value
$mobile_no = $_POST['Mobile']; // Get Mobile No
$msg = $_POST['message']; // Get Message Value
$to = "somasekhar.n#vitalticks.com"; // You can change here your Email
$subject = "'$name' has been sent a mail"; // This is your subject
// HTML Message Starts here
$message ="
<html>
<body>
<table style='width:600px;'>
<tbody>
<tr>
<td style='width:150px'><strong>Name: </strong></td>
<td style='width:400px'>$name</td>
</tr>
<tr>
<td style='width:150px'><strong>Email ID: </strong></td>
<td style='width:400px'>$email_id</td>
</tr>
<tr>
<td style='width:150px'><strong>Mobile No: </strong></td>
<td style='width:400px'>$mobile_no</td>
</tr>
<tr>
<td style='width:150px'><strong>Message: </strong></td>
<td style='width:400px'>$msg</td>
</tr>
</tbody>
</table>
</body>
</html>
";
// HTML Message Ends here
// Always set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
// More headers
$headers .= "From: New Contact Form <".$_POST["email"].">\r\n"; // Give an email id on which you want get a reply. User will get a mail from this email id
$headers .= 'Cc: somumstr210#gmail.com' . "\r\n"; // If you want add cc
// $headers .= 'Bcc: somasekhar.n#vitalticks.com' . "\r\n"; // If you want add Bcc
$headers .= "Reply-To: ".$_POST["email"]."\r\n";
if(mail($to,$subject,$message,$headers)){
// Message if mail has been sent
echo "<script>
alert('Mail has been sent Successfully.');
</script>";
}
else{
// Message if mail has been not sent
echo "<script>
alert('EMAIL FAILED');
</script>";
}
}
?>
The mail function is deprecated and may not work right! I recommand phpmailer https://github.com/PHPMailer/PHPMailer
How do I make a redirect in PHP?
header("Location: path/to/file");
please check post variables
$variable = $_POST['variable-name'] ?? "default content if $_POST['variable-name'] is undefined";
EDIT: mail() is not deprecated but please use the PHPmailer because it's better

Tell a friend script not sending email php [duplicate]

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 5 years ago.
I am trying to execute "Tell a friend" php script, but that is not sending any emails, not to admin, friends, sender. Other emails on same server working fine.
I don't understand why this is not working, as other emails pages like contact us, registration (which send confimation email) all working on same server, please help me..
Html Code:
<table>
<tr>
<td>
<span>Complete the details below to send this link to a friend:</span>
<?php
$refurl = $_SERVER['HTTP_REFERER'];?>
<span><? print $refurl;?></span>
<form name="tellafriend" action="send_group.php" method="post" onSubmit="return checkfields()">
<table>
<tr>
<td> Your name*:</td>
<td> <input name="name" size="30" maxlength="45"> </td>
</tr>
<tr>
<td>Your email*:</td>
<td><input name="email" size="30" maxlength="45"></td>
</tr>
<tr>
<td colspan="2"><p align="center">Enter your friend's email addresses:</p>
</td>
</tr>
<tr>
<td>Email 1*:</td>
<td><input name="fmail1" class="bordesolid1" size="30" maxlength="50"></td>
</tr>
<tr>
<td>Email 2*:</td>
<td><input name="fmail2" size="30" maxlength="50"></td>
</tr>
<tr>
<td>Email 3*:</td>
<td><input name="fmail3" size="30" maxlength="50"></td>
</tr>
<tr>
<td colspan="2"><p align="center"><span>This message will contain your name & email address.</span>
<br>
<input onClick="validate();" type="button" value="click once to send">
<input type=hidden name=refurl value="<? print $refurl;?>">
</td>
</tr>
</table>
</form>
</td>
</tr>
</table>
PHP Code:
<?php
if(count($_POST)) {
foreach(array('fmail1','fmail2','fmail3','email','name') as $key) $_POST[$key] = strip_tags($_POST[$key]);
if(!is_secure($_POST)) {
die("Peace People! Stop Spamming!");
}
$name = $_POST[name];
$email = $_POST[email];
$fmail1 = $_POST[fmail1];
$fmail2 = $_POST[fmail2];
$fmail3 = $_POST[fmail3];
$refurl = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '';
$to = "arvindsri123#yahoo.com";
$subject = "Recommendation form submission";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: '.$email."\r\n";
$headers .= 'Reply-To: '.$email."\r\n".
'X-Mailer: PHP/' . phpversion();
$message = '<html><body>';
$message.='<p style="margin-top:10px;">'.$name.' has used your recommendation form using an email address of '.$email.' </p>';
$message.='<p style="margin-top:10px;">The people the recommendation has been submitted to are: </p>';
$message.='<p style="margin-top:10px;">'.$fmail1.' </p>';
$message.='<p style="margin-top:10px;">'.$fmail2.' </p>';
$message.='<p style="margin-top:10px;">'.$fmail3.' </p>';
$message.='<p style="margin-top:10px;">The page recommended:</p>';
$message.='<p style="margin-top:10px;">'.$refurl.'</p>';
$message .= '</body></html>';
$sentmail = mail($to, $subject, $message, $headers);
// $thankyoupage = "thankyou.htm";
//echo $sentmail;
if($sentmail) {
$name = $_POST[name];
$email = $_POST[email];
$fmail1 = $_POST[fmail1];
$fmail2 = $_POST[fmail2];
$fmail3 = $_POST[fmail3];
$refurl = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '';
$tsubject = "A web page recommendation from $_POST[name]";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: '.$email."\r\n";
$headers .= 'Reply-To: '.$email."\r\n".
'X-Mailer: PHP/' . phpversion();
$message = '<html><body>';
$message.='<p style="margin-top:10px;">Hi, '.$name.' whose email address is $_POST[email] thought you may be interested in this web page. '.$email.' </p>';
$message.='<p style="margin-top:10px;">'.$refurl.'</p>';
$message .= '</body></html>';
$sentmail = mail($fmail1,$fmail2,$fmail3, $tsubject $message, $headers);
echo '<h4>You have sent emails...</h4>';
//header("Location: $thankyoupage");
exit;
}
function is_secure($ar) {
$reg = "/(Content-Type|Bcc|MIME-Version|Content-Transfer-Encoding)/i";
if(!is_array($ar)) {
return preg_match($reg,$ar);
}
$incoming = array_values_recursive($ar);
foreach($incoming as $k=>$v) if(preg_match($reg,$v)) return false;
return true;
}
function array_values_recursive($array) {
$arrayValues = array();
foreach ($array as $key=>$value) {
if (is_scalar($value) || is_resource($value)) {
$arrayValues[] = $value;
$arrayValues[] = $key;
}
elseif (is_array($value)) {
$arrayValues[] = $key;
$arrayValues = array_merge($arrayValues, array_values_recursive($value));
}
}
return $arrayValues;
}
?>
I am using Bluehost web hosting, and other emails properly working as I said Contact us, Registration confirmation emails.. etc.
Thanks!
If you are running on localhost then it won't send email.. to do so you need to host on server
Try to host on free hosting server.. it will work!

Use PHP mail header to send email to more than one address

I'm new to this site so i'll try and explain myself as clearly as possible!
I'm currently in the process of creating a web form which when submitted sends an email to an account I have made on my server.
All is working fine but was wondering if there was a way i could add multiple accounts for the submitted form to send too.
My HTML:
<table style="width:100%">
<form action="form_complete.php" method="post">
<tr>
<th>
<input type="text" required placeholder="Name" name="name" />
</th>
<th>
<input type="text" required placeholder="Email" name="email" />
</th>
</tr>
<tr>
<th>
<input type="text" placeholder="Contact Number" name="mobile" />
</th>
<th>
<input type="text" required placeholder="Subject" name="subject" />
</th>
</tr>
<tr>
<td colspan="2"><textarea id="ta" name="message" required placeholder="Message"></textarea>
</tr>
</table>
<input id="submit" type="submit" name="submit" value="submit" />
</form>
form_complete.php
if(isset($_POST['submit'])){
$to = "rtozer#tandtcivils.co.uk"; // this is your Email address
$from = $_POST['email']; // this is the sender's Email address
$name = $_POST['name'];
$mobile = $_POST['mobile'];
$subject = $_POST['subject'];
$subject2 = "Copy of your form submission";
$message = "Name: " .$name . "\n\n mobile number: " . $mobile . ".\n\n Message:" . "\n\n" . $_POST['message'];
$message2 = "Here is a copy of your message " . $name . "\n\n" . $_POST['message'];
$headers = "From:" . $from;
$headers2 = "From:" . $to;
mail($to,$subject,$message,$headers);
mail($from,$subject2,$message2,$headers2); // sends a copy of the message to the sender
header ('Location: http://www.tandtcivils.co.uk/form_complete.php');
// You can also use header('Location: thank_you.php'); to redirect to another page.
}
The code here is working correctly when sending an email to rtozer#tandtcivils.co.uk but i want to be able to add; dtozer#tandtcivils.co.uk to recieve a copy of the form submission also.
If you need any further information from me please leave a comment!
Thanks in advance,
Sam
You can have multiple TO addresses. For example:
$to = "rtozer#tandtcivils.co.uk," . $_POST['email'];
Or use CC or BCC headers:
$headers = "From:" . $from;
$headers .= "CC:" . $_POST['email'];
Or:
$headers .= "CC:rtozer#tandtcivils.co.uk," . $_POST['email'];
There are a number of ways to organize your email recipients, depending on whether you want them to be direct recipients, carbon copy recipients, or blind carbon copy recipients. Lots of examples are available in the PHP documentation.
The key benefit here, as opposed to what you attempted in your code, is that you need only send the email once as opposed to sending the same email multiple times. Just arrange multiple recipients on that one email.
Thanks to Reza Mamun try this:
//......
//...Other setting goes here....
// Always set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
// More headers
$headers .= 'From: My Name <myemail#example.com>'. "\r\n";
//Multiple CC can be added, if we need (comma separated);
$headers .= 'Cc: myboss1#example.com, myboss2#example.com' . "\r\n";
//Multiple BCC, same as CC above;
$headers .= 'Bcc: myboss3#example.com, myboss4#example.com' . "\r\n";
mail($to, $subject, $message, $headers);

PHP Form - not receiving email

I'm not understanding why Im not receiving email from the form after filling out the simple form. After clicking the submit button it redirected to the thank you page with no problem, but no email.
HTML
<form class="action" name="form1" method="POST" action="_sendmail2.php" onSubmit="return CheckAll(this);">
<label class="nick-2">Full Name:</label><br>
<input type="text" class="name" name="full_name">
<label class="nick">Email Address:</label><br>
<input type="text" class="email" name="email"><br>
<div class="radio-toolbar">
<input type="radio" id="radio1" name="agent_type" value="Buyer" checked>
<label for="radio1">Buyer</label>
<input type="radio" id="radio2" name="agent_type" value="Seller">
<label for="radio2">Seller</label>
<input type="radio" id="radio3" name="agent_type" value="Investor">
<label for="radio3">Investor</label>
</div><br>
<input type="submit" class="btn" value="SUBMIT" name="Submit">
</form>
PHP (
<?php
$to = "cluneborg#hotmail.com";
$subject = "New Agent Inquries";
$full_name = $_POST['full_name'];
$email = $_POST['email'];
$agent_type = $_POST['agent_type'];
if($_SERVER['REQUEST_METHOD']=="POST") {
$full_name=str_replace ( array("\n"), array("<br>"),trim($_REQUEST['full_name']));
$email=str_replace ( array("\n"), array("<br>"),trim($_REQUEST['email']));
$agent_type=str_replace ( array("\n"), array(" <br>"),trim($_REQUEST['agent_type']));
$contentmsg=stripslashes("<br><b><font style=color:#CC3300>$subject</font></b><br>
<table width=708 border=0 cellpadding=2 cellspacing=1 bgcolor=#CCCCCC>
<tr>
<td width=165 align=right valign=top bgcolor=#FFFFFF><B>Full Name: </b> </td>
<td width=565 align=left valign=top bgcolor=#FFFFFF> $full_name</td>
</tr>
<tr>
<td width=165 align=right valign=top bgcolor=#FFFFFF><B>Email Address: </b> </td>
<td width=565 align=left valign=top bgcolor=#FFFFFF> $email</td>
</tr>
<tr>
<td width=165 align=right valign=top bgcolor=#FFFFFF><B>Type of Agent:</b> </td>
<td width=565 align=left valign=top bgcolor=#FFFFFF> $agent_type</td>
</tr>
</table>
");
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1" . "\r\n";
$headers .= 'To: Eric <eluneborg#gmail.com>' . "\r\n";
$headers .= 'From: Texas Real Estate Agent Website' . "\r\n";
if(mail($to,$subject,$contentmsg,$headers)){
header("Location: http://www.magnixsolutions.com/clients/tas/thanks.html");
}
else
{
echo "Mail was not sent!";
}
}
?>
Sometimes it sends email to my hotmail and most of time it get this (checked on cpanel)
This message was created automatically by mail delivery software.
A message that you sent could not be delivered to one or more of its
recipients. This is a permanent error. The following address(es) failed:
cluneborg#hotmail.com
Domain magnixsolutions.com has exceeded the max defers and failures per hour (5/5 (55%)) allowed. Message discarded.
(Tested) - There were a few issues with your code.
The most important thing, is the # symbol in #mail - This will not execute, it needs to be removed.
Now, this line: (in PHP)
$_REQUEST['type_agent']
should be:
$_REQUEST['agent_type']
as per: (in HTML form)
<input type="radio" id="radio3" name="agent_type" value="Investor">
Then your headers were incorrect, where I added a few \r\n
One of your headers (in PHP)
$headers .= "From: ".$from."";
has been changed to:
$headers .= "From: $full_name <$email>\r\n";
Sidenote: It could be replaced with
$headers .= "From: $fromemail <$email>\r\n";
if you want the name to appear as "New Agent" in mail, instead of the person's name sending the Email.
Using this $fromemail="New Agent"; in conjunction with $from=$fromemail; and $headers .= "From: ".$from."";
would have resulted in mail going to SPAM, being it's not an actual Email address.
Plus, upon testing your original code, it did not come in as proper HTML, but the codes themselves showed up in the Email; that has been corrected.
If you want the Email and the name, you need to use two different variables.
I.e.:
$headers .= 'From: YourName <YourName#domain.com>' . "\r\n";
and in your case:
$headers .= "From: $full_name <$email>\r\n";
Rewrite: (PHP)
<?php ob_start();
// commented out - is not needed for the time being
// $fromemail="New Agent"; // change here if you want
$toemail="email#example.com"; // change here if you want
$sub="Agent Inquiries"; // change here if you want
$success_page_name="thanks.html";
////// do not change in following
if($_SERVER['REQUEST_METHOD']=="POST")
{
$full_name=str_replace ( array("\n"), array("<br>"),trim($_REQUEST['full_name']));
$email=str_replace ( array("\n"), array("<br>"),trim($_REQUEST['email']));
$type_agent=str_replace ( array("\n"), array("<br>"),trim($_REQUEST['agent_type']));
$contentmsg=stripslashes("<br><b><font style=color:#CC3300>$sub</font></b><br>
<table width=708 border=0 cellpadding=2 cellspacing=1 bgcolor=#CCCCCC>
<tr>
<td width=165 align=right valign=top bgcolor=#FFFFFF><B>Full Name: </b> </td>
<td width=565 align=left valign=top bgcolor=#FFFFFF>$full_name</td>
</tr>
<tr>
<td width=165 align=right valign=top bgcolor=#FFFFFF><B>Email Address: </b> </td>
<td width=565 align=left valign=top bgcolor=#FFFFFF>$email</td>
</tr>
<tr>
<td width=165 align=right valign=top bgcolor=#FFFFFF><B>Type of Agent:</b> </td>
<td width=565 align=left valign=top bgcolor=#FFFFFF>$type_agent</td>
</tr>
</table>
");
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1" . "\r\n";
$headers .= "From: $full_name <$email>\r\n";
#mail($toemail,$sub,$contentmsg,$headers);
header("Location:$success_page_name");
}
?>
Footnotes:
Including the # symbol in #mail suppresses errors and does not execute the function, so you will want to remove it..
In my testing, I removed onSubmit="return CheckAll(this); since your full code didn't include that function. Should it fail, then you may need to remove it also.
Remove the # sign from the #mail command and it might give you a helpful error. The # sign there is suppressing errors:
http://www.php.net/manual/en/language.operators.errorcontrol.php
You need to add EOL character \n in headers to separate. Don't know whether this is the solution, but it is at least one problem that needs attention.
$headers = "MIME-Version: 1.0\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\n";
$from=$fromemail;
$headers .= "From: ".$from."\n";
In addition to the error suppression answers/comments, you can also make sure that mail() returns true indicating your server has accepted it and will attempt delivery.
$success = mail($toemail,$sub,$contentmsg,$headers);
var_dump( $success ); // should be true

Process order form with php to send email

I have the following code:
<table class="table table-striped" id="itemsTable">
<thead>
<tr>
<th></th>
<th>Item Code</th>
<th>Description</th>
<th>Qty</th>
<th>Price</th>
<th>Total</th>
</tr>
</thead>
<tbody>
<tr class="item-row">
<td></td>
<td><input type="text" name="itemCode[]" value="" class="input-medium" id="itemCode"
tabindex="1"/>
</td>
<td><input type="text" name="itemDesc[]" value="" class="input-large" id="itemDesc"
readonly="readonly"/></td>
<td><input type="text" name="itemQty[]" value="" class="input-mini" id="itemQty" tabindex="2"/>
</td>
<td>
<div class="input-prepend input-append"><span class="add-on">€</span>
<input
name="itemPrice[]"
class=" input-small"
id="itemPrice"
type="text"></div>
</td>
<td>
<div class="input-prepend input-append"><span class="add-on">€</span><input
name="itemLineTotal[]" class=" input-small" id="itemLineTotal" type="text"
readonly="readonly"></div>
</td>
</tr>
</tbody>
</table>
What is the best way to process the inputs via php to send the order via email nicley formated into a table? This is an order form and I need to to simply be sent to an email once complete
Here is my processing code:
<?php
$to = $_REQUEST['xxx'] ;
$from = $_REQUEST['Email'] ;
$name = $_REQUEST['Name'] ;
$headers = "From: $from";
$subject = "Web Contact Data";
$fields = array();
$fields{"itemCode"} = "Code";
$fields{"itemDesc"} = "Description";
$fields{"itemPrice"} = "Price";
$body = "We have received the following information:\n\n";
foreach($fields as $a => $b){
$body .= sprintf("%20s: %s\n",$b,$_REQUEST[$a]);
}
$headers2 = "From: noreply#example.com";
$subject2 = "Thank you for contacting us";
$autoreply = "Thank you for contacting us. Somebody will get back to you as soon as possible, usualy within 48 hours. If you have any more questions, please consult our website at www.oursite.com";
$send = mail($to, $subject, $body);
if($send){
header( "Location:index.php" );
} else {
print "We encountered an error sending your mail, please try again";
}
?>
This code is not working please help
To process a form this way you need to have a form element somewhere in your markup to process.
<form method="POST" action="yourSecondScript.php">
your first markup here
<input type="submit">
<form>
Then to make the email nice with tables you need to set the email headers to html.
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= "From: " . $_REQUEST['Name'] . ">\r\n";
mail($to, $subject, $body, $headers);
Where is
$to = $_REQUEST['xxx'] ;
coming from? My guess is that you can set it to fixed as you probably don't need a dynamic e-mail to address, so something like:
$to = 'myname#mydomain.com';
But as Michael mentioned in a reaction to your question, we cannot be sure until you tell us which part is not working / what errors you receive and so on..

Categories