I have a simple form which sending an email with some fields and also it saves the fields in .csv file.
Here is my code (HTML, jQuery, PHP)
The Form:
<form id="request-size">
<input name="email" id="email" type="email" class="cmesgmail" placeholder="email">
<input type="hidden" name="size" id="size" value="52">
<input type="hidden" name="sku" id="sku" value="315">
<input type="hidden" name="pridpr" id="pridpr" value="849">
<input class="request-size-btn btn" value="SEND">
</form>
The jQuery:
jQuery('.request-size-btn').live('click', function() {
jQuery.ajax({
type: "POST",
url: "https://example.com/sendmail.php",
data: jQuery('form#request-size').serializeArray(),
success: function( data ) {
jQuery('.cmesg').empty();
jQuery('.cmesg').append(data);
}
});
});
});
The PHP Sendmail:
$EmailFrom = $_REQUEST['email'];
$SKU = $_REQUEST['sku'];
$SIZE = $_REQUEST['size'];
$ID = $_REQUEST['pridpr'];
$EmailTo = "info#example.com";
$Subject = "Contact for Size";
$DATENOW = date('d/m/Y h:i:s a', time());
$Body = "";
$Body .= "<b>Email:</b> ";
$Body .= $EmailFrom . "\r\n";
$Body .= "<br>";
$Body .= "<b>Product SKU:</b> ";
$Body .= $SKU . "\n";
$Body .= "<br>";
$Body .= "<b>Product ID:</b> ";
$Body .= $ID . "\n";
$Body .= "<br>";
$Body .= "<b>Product Size:</b> ";
$Body .= $SIZE;
$Body .= "<br>";
$Body .= "<b>Date:</b> ";
$Body .= $DATENOW;
$headers = "MIME-Version: 1.0" . "\n";
$headers .= "Content-type: text/html; charset=UTF-8" . "\n";
$headers .= "From: ". $EmailFrom ."" . "\n";
$success = mail($EmailTo, $Subject, $Body, $headers);
if ($success){
echo '<div class="alert alert-success">Done!</div>';
$file = fopen('request-size.csv', 'a');
$data = array(
array($EmailFrom, $SKU, $ID, $SIZE, $DATENOW)
);
foreach ($data as $row)
{
fputcsv($file, $row);
}
fclose($file);
}
else{
echo '<div class="alert alert-danger">Error!</div>';
}
I've got all the fields in my csv file and email, except the "email" field. It's always empty. I have also try to change the name of email field (email to emailreq) on both sides (html, php), but didn't work.
Any suggestions? Thank you!
If updating jQuery is an option for you, do that. Version 1.6.2 and up should be working (possibly also before, but i'm not sure).
Otherwise change the
<input name="email" id="email" type="email" class="cmesgmail" placeholder="email">
to
<input name="email" id="email" type="text" class="cmesgmail" placeholder="email">
Older versions of jQuery did not include support for the HTML5 input types.
Related
I have made a contact form with the following fields: Name, Email, Message. It all worked fine - messages were sent to my email - until I added the attachments option to the form.
I've tried validating the attachment fields by searching up tutorials, but nothing seems to work. I guess I'm just not sure how to implement it to my already existing code.. Any help here?
Here is the form:
<?php include 'contact-form.php'; ?>
<form id="contact" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data">
<h3>Contact Us</h3>
<fieldset>
<input placeholder="Nimi" type="text" tabindex="1" name="thename" value="<?= $thename ?>" autofocus>
<div class="error"><span><?= $name_error ?></span></div>
</fieldset>
<fieldset>
<input placeholder="Email" type="text" tabindex="2" name="email" value="<?= $email ?>">
<div class="error"><span><?= $email_error ?></span></div>
</fieldset>
<fieldset>
<textarea placeholder="Sisesta sõnum siia.." type="text" tabindex="3" name="message"></textarea>
<div class="error"><span><?= $message_error ?></span></div>
</fieldset>
<fieldset>
<label for="attachment1">File:</label> <input type="file" id="attachment1" name="attachment[]" size="35">
<label for="attachment2">File:</label> <input type="file" id="attachment2" name="attachment[]" size="35">
<div class="error"><span><?= $attachment_error ?></span></div>
</fieldset>
<fieldset>
<button name="submit" type="submit" id="contact-submit" data-submit="...Saatmine">Saada</button>
</fieldset>
<div class="success"><?= $success; ?></div>
<div class="error"><?= $error; ?></div>
</form>
Here is PHP validation code contact-form.php:
<?php
$name_error = $email_error = $message_error = $attachment_error = "";
$thename = $email = $message = $success = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["thename"])) {
$name_error = "Palun sisesta nimi";
} else {
$thename = test_input($_POST["thename"]);
// check if name only contains letters, whitespace and hyphen
if (!preg_match("/^[a-zA-Z -]*$/",$thename)) {
$name_error = "Sisestada saab ainult tähti, tühikuid ja sidekriipse";
}
}
if (empty($_POST["email"])) {
$email_error = "Palun sisesta email";
} else {
$email = test_input($_POST["email"]);
// email validation
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$email_error = "Sisesta email korrektselt";
}
}
if (empty($_POST["message"])) {
$message_error = "Palun sisesta sõnum";
} else {
$message = test_input($_POST["message"]);
}
if (empty($_FILES["attachment"])) {
$attachment_error = "Palun sisesta enda eluloo fail";
}
if ($name_error == '' and $email_error == '' and $message_error == '' ){
$message_body = '';
unset($_POST['submit']);
foreach ($_POST as $key => $value){
$message_body .= "$key: $value\n";
}
$to = 'myemail#gmail.com';
$subject = 'Eesti Elulood';
$message = "Sulle saadeti kiri Rannu koguduse kodulehelt.\n\nSaatja nimi: $thename\n\nSaatja email: $email\n\nSõnum: $message";
// create email headers
$headers = 'From: '.$email."\r\n".
'Reply-To: '.$email."\r\n" .
'X-Mailer: PHP/' . phpversion();
if (isset($_FILES['attachment']['name'])) {
$semi_rand = md5(uniqid(time()));
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
$headers = "From: " . '=?UTF-8?B?' . base64_encode($thename) . '?=' . "
<$email>" . PHP_EOL;
$headers .= "Reply-To: " . '=?UTF-8?B?' . base64_encode($thename) . '?=' .
" <$email>" . PHP_EOL;
$headers .= "Return-Path: $email" . PHP_EOL;
$headers .= "MIME-Version: 1.0" . PHP_EOL;
$headers .= "Content-Type: multipart/mixed;" . PHP_EOL;
$headers .= " Boundary=\"{$mime_boundary}\"";
$datamsg = "This is a multi-part message in MIME format." . PHP_EOL .
PHP_EOL;
$datamsg .= "--{$mime_boundary}" . PHP_EOL;
$datamsg .= "Content-Type: text/plain; Charset=\"UTF-8\"" . PHP_EOL;
$datamsg .= "Content-Transfer-Encoding: 8bit" . PHP_EOL . PHP_EOL;
$datamsg .= $message . PHP_EOL . PHP_EOL;
for ($index = 0; $index < count($_FILES['attachment']['name']); $index++)
{
if ($_FILES['attachment']['name'][$index] != "") {
$file_name = $_FILES['attachment']['name'][$index];
$data_file =
chunk_split(base64_encode(file_get_contents($_FILES['attachment']
['tmp_name'][$index])));
$datamsg .= "--{$mime_boundary}" . PHP_EOL;
$datamsg .= "Content-Type: application/octet-stream; Name=\"
{$file_name}\"" . PHP_EOL;
$datamsg .= "Content-Disposition: attachment; Filename=\"{$file_name}\"" . PHP_EOL;
$datamsg .= "Content-Transfer-Encoding: base64" . PHP_EOL . PHP_EOL .
$data_file . PHP_EOL . PHP_EOL;
}
}
$datamsg .= "--{$mime_boundary}--";
}
if (#mail($to, '=?UTF-8?B?' . base64_encode($subject) . '?=',
$datamsg, $headers, "-f$email")){
$success = "Thankyou, message sent!.";
} else {
$error = "Sorry but the email could not be sent. Please try again!";
}
}
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
After hitting the submit button, It just takes me to the index.php page..
Any help is appreciated!
1) You have no mail attachments code into your php code except for the html markup, so you cannot send your mail attachments.
2) You have to encode the attachments using chunk_split(base64_encode()) and then you have to import them into your message part using the correct way.
3) You forgot to enter the correct headers, that's the other reason why you can't send your mails.
4) You have to consider that if you use GMail there may be a limit to the type of file you can send and so read this: https://support.google.com/mail/answer/6590?hl=en
5) I suggest you to use the long php tag instead the short tag:
Instead of writing <?= $_SERVER['PHP_SELF']; ?>, write <?php echo $_SERVER['PHP_SELF']; ?>
6) You have a serious error into your php and this is the reason why pressing submit you are in the home instead of your contact form:
<?= $SERVER['PHP_SELF']; ?> is wrong!
<?= $_SERVER['PHP_SELF']; ?> is correct!
See you point 5)
Here is an example of correct html markup for attachments:
<label for="attachment1">File:</label> <input type="file" id="attachment1" name="attachment[]" size="35">
<label for="attachment2">File:</label> <input type="file" id="attachment2" name="attachment[]" size="35">
<label for="attachment3">File:</label> <input type="file" id="attachment3" name="attachment[]" size="35">
Here is an example of correct php mail code for attachments:
if (isset($_FILES['attachment']['name'])) {
$semi_rand = md5(uniqid(time()));
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
$headers = "From: " . '=?UTF-8?B?' . base64_encode($sender_name) . '?=' . " <$from_email>" . PHP_EOL;
$headers .= "Reply-To: " . '=?UTF-8?B?' . base64_encode($sender_name) . '?=' . " <$from_email>" . PHP_EOL;
$headers .= "Return-Path: $from_email" . PHP_EOL;
$headers .= "MIME-Version: 1.0" . PHP_EOL;
$headers .= "Content-Type: multipart/mixed;" . PHP_EOL;
$headers .= " Boundary=\"{$mime_boundary}\"";
$datamsg = "This is a multi-part message in MIME format." . PHP_EOL . PHP_EOL;
$datamsg .= "--{$mime_boundary}" . PHP_EOL;
$datamsg .= "Content-Type: text/plain; Charset=\"UTF-8\"" . PHP_EOL;
$datamsg .= "Content-Transfer-Encoding: 8bit" . PHP_EOL . PHP_EOL;
$datamsg .= $message . PHP_EOL . PHP_EOL;
for ($index = 0; $index < count($_FILES['attachment']['name']); $index++) {
if ($_FILES['attachment']['name'][$index] != "") {
$file_name = $_FILES['attachment']['name'][$index];
$data_file = chunk_split(base64_encode(file_get_contents($_FILES['attachment']['tmp_name'][$index])));
$datamsg .= "--{$mime_boundary}" . PHP_EOL;
$datamsg .= "Content-Type: application/octet-stream; Name=\"{$file_name}\"" . PHP_EOL;
$datamsg .= "Content-Disposition: attachment; Filename=\"{$file_name}\"" . PHP_EOL;
$datamsg .= "Content-Transfer-Encoding: base64" . PHP_EOL . PHP_EOL . $data_file . PHP_EOL . PHP_EOL;
}
}
$datamsg .= "--{$mime_boundary}--";
}
if (#mail($recipient_email, '=?UTF-8?B?' . base64_encode($subject) . '?=', $datamsg, $headers, "-f$from_email")) {
exit("Files Sent Successfully");
} else {
exit("Sorry but the email could not be sent. Please go back and try again!");
}
Where $sender_name is the name of sender, $from_email is the email of sender, $recipient_email is the recipient of your email.
You can take an example from my code and integrate it into your project, I wrote only the essential parts concerning the sending of attachments.
I hope this helps.
I'm trying to hide the form and show the success alert after the mail is mailed.
But everything I do, is ignored by the code, I put header before the mail function, and the mail still sends, it should redirect before the mail sends, tried to break the code before the mail function, and the break functions works.
at list the mail doesn't send.
contact.php:
<div id="innerWrapper">
<h1 class="pageheading"><?=translate('contact_us') ?></h1>
<form class="form-horizontal" action="/ajax/contact.php" method="POST" id="contactForm">
<?php $email = $db->get_row("SELECT email,full_name FROM users WHERE userID = 1");?>
<label><?=translate('your_name') ?>:</label>
<input type="text" name="name" <?php if($usersObject->ID()) {echo 'value = "'. $email->full_name. '"';} else {echo 'placeholder = "First / Last"';}?> class="required input-xlarge"/>
<br/><br/>
<label><?=translate('email') ?>:</label>
<input type="email" name="email" <?php if($usersObject->ID()) {echo 'value = "'. $email->email . '"';} else {echo 'placeholder = "email#example.com"';}?> class="required input-xlarge"/>
<br/><br/>
<label>Subject:</label>
<input type="text" name="<?=translate('subject') ?>" placeholder="Hi" class="required input-xlarge"/>
<br/><br/>
<label><?=translate('message') ?>:</label>
<textarea name="message_to_us" class="input-xlarge required" rows="5" placeholder=""></textarea>
<br/><br/>
<input class="btn btn-large btn-warning" value="<?=translate('contact_btn') ?>" type="submit" name="sbc"/>
</form>
</div>
ajax/contact.php :
if(isset($_POST['sbc'])) {
foreach($_POST as $K=>$V) $_POST[$K] = trim(strip_tags($V));
extract($_POST);
foreach($_POST as $k=>$v) {
if(trim($v) == "")
{
print '<div class="alert alert-error">All fields are required</div>';
exit;
}
}
$body = 'New contact form message on ' . $_SERVER['SERVER_NAME'];
$body .= '<br/>';
$body .= 'Subject : ' . $Subject;
$body .= '<br/>';
$body .= 'Name : '.$name .' / Email : ' . $email;
$body .= '<br/>';
$body .= $message_to_us;
$headers = "From: contact#" . str_replace("www.", "", $_SERVER['SERVER_NAME']) . "\r\n";
$headers .= "Reply-To: ". $email . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=utf-8\r\n";
mail(PAYPAL_EMAIL, str_replace("www.", '', $_SERVER["SERVER_NAME"]), $body, $headers);
?>
<script type="text/javascript">
$("#sbc").click(function() {
$("#contactForm").hide('slow');
});
</script>
<?php
print '<div class="alert alert-success">'.translate('thankyou_contact').'</div>';
}else{
print 'Emtpy request';
}
edit:
$headers .= "Content-Type: text/html; charset=utf-8\r\n";
header('Location: join');
mail(PAYPAL_EMAIL, str_replace("www.", '', $_SERVER["SERVER_NAME"]), $body, $headers);
tried to redirect before the mail just to check, but it just ignores the header and still mail the mail.
edit2 :
<script type="text/javascript">
$(document).ready (function() {
$("#sbc").click(function() {
$("#contactForm").hide('slow');
});
});
</script>
not working.
add id= sbc to
<input class="btn btn-large btn-warning" value="<?=translate('contact_btn') ?>" type="submit" name="sbc" id="sbc"/>
I´m trying to validate a simple PHP form with HTML code but it doesn´t work properly.
Here you are HTML code:
<form action="expresscontactform.php" method="post" name="form1" id="form1">
<label>Name </label><input id="Name" name="Name" type="text" value="<?php echo $_POST['Name']; ?>">
<span class="error"> <?php echo $errors[1]; ?> </span>
<label>Email </label><input id="Email" name="Email" type="text" value="<?php echo $_POST['Email']; ?>">
<span class="error"> <?php echo $errors[4]; ?> </span>
<label>Phone </label><input id="Phone" name="Phone" type="text" value="<?php echo $_POST['Phone']; ?>">
<span class="error"> <?php echo $errors[2]; ?> </span>
<label>Country of origin</label><input id="Country" name="Country" type="text" value="<?php echo $_POST['Country']; ?>">
<span class="error"> <?php echo $errors[3]; ?> </span>
<label>Message </label><textarea id="message" cols="5" rows="5" name="Message"></textarea>
<input value="Send" class="send_request_new" type="submit">
</form>
And this is PHP code (on the server):
<?php
if(isset($_POST['send_request_new'])){
$errors = array();
if($_POST['Name'] == ''){
$errors[1] = '<span class="error">Please type your name</span>';
}else if($_POST['Phone'] == ''){
$errors[2] = '<span class="error">Please type your phone number</span>';
}else if($_POST['Country'] == ''){
$errors[3] = '<span class="error">Please type your country</span>';
}else{
$EmailFrom = Trim(stripslashes($_POST['Email']));
$EmailTo = 'webmaster#theacademy.co,' . $EmailFrom;
$Subject = "Online Application Form";
$name = Trim(stripslashes($_POST['Name']));
$phone = $_POST['Phone'];
$country = $_POST['Country'];
$message = $_POST['Message'];
$header = 'From: ' . $EmailFrom . " \r\n";
$header .= "X-Mailer: PHP/" . phpversion() . " \r\n";
$header .= "Mime-Version: 1.0 \r\n";
$header .= "Content-Type: text/plain";
// prepare email body text
$Body .= "Contact form";
$Body .= "\n";
$Body .= "\n";
$Body .= "This is an automatically generated e-mail, to inform you that we received your request. We will contact you as soon as possible.";
$Body .= "\n";
$Body .= "\n";
$Body .= "Kind regards";
$Body .= "\n";
$Body .= "\n";
$Body .= "**********************************************";
$Body .= "\n";
$Body .= "\n";
$Body .= "The Academy";
$Body .= "\n";
$Body .= "\n";
$Body .= "::::::::::::::::::::::::::";
$Body .= "\n";
$Body .= "Your Request:";
$Body .= "\n";
$Body .= "::::::::::::::::::::::::::";
$Body .= "\n";
$Body .= "\n";
$Body .= "Name: ";
$Body .= $name;
$Body .= "\n";
$Body .= "\n";
$Body .= "Phone: ";
$Body .= $phone;
$Body .= "\n";
$Body .= "\n";
$Body .= "Country: ";
$Body .= $country;
$Body .= "\n";
$Body .= "\n";
$Body .= "Email: ";
$Body .= $EmailFrom;
$Body .= "\n";
$Body .= "\n";
$Body .= "Message: ";
$Body .= $message;
$Body .= "\n";
$Body .= "\n";
$Body .= "Sent on " . date('d/m/Y', time());
$Body .= "\n";
$Body .= "\n";
$Body .= "Last visited page: ";
$Body .= $_SERVER['HTTP_REFERER'];
if(mail($EmailTo, $Subject, $Body, $header)){
$result = '<div class="result_ok">Email sent successfully</div>';
// If successfully we reset all the fields
$_POST['nombre'] = '';
$_POST['email'] = '';
$_POST['asunto'] = '';
$_POST['mensaje'] = '';
header("refresh:3;url=http://www.myexample.co/");
}else{
$result = '<div class="result_fail">Error!!</div>';
}
}
}
?>
I think there is a mistake in the line below:
<span class="error"> <?php echo $errors[1]; ?> </span>
But I don´t know where exactly.
One more question, how can I set "The Academy" as email sender?
I don´t know if this is the best way to validate a form, if someone show me other way I will be thankful to learn.
I appreciate a lot if anyone can help me, please.
Thanks in advance.
Firstly, your code's execution is dependant on this conditional statement if(isset($_POST['send_request_new'])) where it's looking for a "named" element called send_request_new therefore would never execute.
Your present (unnamed) submit button
<input value="Send" class="send_request_new" type="submit">
this should be changed to:
<input value="Send" class="send_request_new" type="submit" name="send_request_new">
you have a class named that way, instead of a name.
In order to get it to work, you would need to place your entire HTML/PHP inside the same page and use action=""
I noticed you don't have one for your Email and the message, only for the name/phone number and country.
Plus, to use a personalized method for the sender's name:
Base yourself on the following:
$Name = "The Academy";
$email = "email#example.com";
$header = "From: ". $Name . " <" . $email . ">\r\n";
You can use some of the filters on PHP.net to validate and protect against XSS injection:
-http://php.net/manual/en/filter.filters.validate.php
One of which being FILTER_VALIDATE_EMAIL
This php contact form I'm using returns the message that my message has be sent but no email is received by the specified email address.
Here's the php:
<?php
$to = 'blahbahblah#gmail.com';
if($to) {
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$html = "";
$html .= "Name: " . htmlentities($name, ENT_QUOTES, "UTF-8") . "<br>\n";
$html .= "Email: " . htmlentities($email, ENT_QUOTES, "UTF-8") . "<br>\n";
$html .= "Message: " . htmlentities($message, ENT_QUOTES, "UTF-8") . "<br>\n";
$headers = "MIME-Version: 1.0\r\nContent-type: text/html; charset=utf-8\r\n";
$headers .= "From: " . $name . "<". $email .">\r\n";
$headers .= "Reply-To: " . $email . "\r\n";
$html = utf8_decode($html);
mail($to, $subject, $html, $headers);
if ($html)
echo 'ok';
else
echo 'error';
} else {
echo "error";
}
?>
And here's the html associated with it:
<form method="post" action="contact.php">
<p>
<input type="text" name="name" id="name" placeholder="Name" />
</p>
<p>
<input type="text" name="email" id="email" placeholder="Email" />
</p>
<p>
<input type="text" name="subject" id="subject" placeholder="Subject" />
</p>
<div class="textarea-wrapper">
<textarea name="message" id="message" cols="45" rows="10" placeholder="Message"></textarea>
</div>
<button id="submit">Send</button>
</form>
I realize issues like these are frequent, but I've unable to figure it out. Any insight is greatly appreciated.
"<br>\n"
use "\r\n" instead and try again
You need to change your SMTP settings per your conversation with the support rep. These are set in your PHP.INI
The From address should belong to the domain from where you are running the script. If your script is running on your-website.com then the From address should be like xyz#website-name.com
$headers = "From: xyz#website-name.com";
mail($to,$subj,$body,$headers);
To check the contact form that I have used, visit: http://manageproac.com/support/
I have created a form that pulls a variable from a url using the following code:
<?php $id = $_GET['id']; echo $id; ?>
Using this is link as an example (http://www.mydomain.co.uk/customerform.php?id=12) it would display 12 on the webpage.
What I would like to know, is it possible to stop the web page from loading if there is no varible in the URL e.g ?id=12 ?
Either in php, javascript?
So If you visited the following link, the web page would display:
http://www.mydomain.co.uk/customerform.php?id=12
and if you visited this link (without the variable), the page would not display:
http://www.mydomain.co.uk/customerform.php
Thank you for any help.
Update below...
I have added Krishnanunni's code and it successfully prevents the web page from displaying if the is no vartiable in the url. But adding this code prevents the form submitting...
Is there a way of keeping the url variable when the form is submitted?
Can anyone help? Thanks
<body id="main_body" >
<div id="form_container">
<?
/* only load page is variable is presnt in the URL */
if (!isset($_GET['id']) || empty($_GET['id']))
{
die("Bad Request");
}
?>
<?
/* Mailer with Attachments */
$action = $_REQUEST['action'];
global $action;
function showForm() {
?>
<form id="contact" class="appnitro" name="idform" enctype="multipart/form-data" method="post" action="<?=$_SERVER['PHP_SELF']?>">
<input type="hidden" name="action" value="send" />
<input type="hidden" name="MAX_FILE_SIZE" value="10000000"/>
<input name="to_email" type="hidden" id="to_email" value="myemail#domain.co.uk"/>
<div>
<input id="invoice" name="invoice" class="element text medium" type="hidden" maxlength="255" value="<?php $id = $_GET['id']; echo $id; ?>"/>
</div>
<div>
<input id="subject" name="subject" class="element text medium" type="hidden" maxlength="255" value="Enquiry Form - (Invoice: <?php $id = $_GET['id']; echo $id; ?>)"/>
</div>
<label class="description" for="from_name">Name </label>
<div>
<input id="from_name" name="from_name" class="element text medium" type="text" maxlength="255" value=""/>
</div>
<label class="description" for="position">Position </label>
<div>
<input id="position" name="position" class="element text medium" type="text" maxlength="255" value=""/>
</div>
<label class="description" for="uemail"> Email </label>
<div>
<input id="email" name="email" class="element text medium" type="text" maxlength="255" value=""/>
</div>
<label class="description" for="phone"> Phone </label>
<div>
<input id="phone" name="phone" class="element text medium" type="text" maxlength="255" value=""/>
</div>
<input name="Reset" type="reset" class="contactform_small" value="Reset" />
<input name="Submit2" type="submit" class="contactform_small" id="Submit" value="Submit" />
</form>
<?
}
function sendMail() {
if (!isset ($_POST['to_email'])) { //Oops, forgot your email addy!
die ("<p>Oops! You forgot to fill out the email address! Click on the back arrow to go back</p>");
}
else {
$to_name = "MY BUSINESS";
$from_name = stripslashes($_POST['from_name']);
$subject = Trim(stripslashes($_POST['subject']));
$to_email = $_POST['to_email'];
$EmailFrom = Trim(stripslashes($_POST['email']));
// get posted data into local variables
$position = Trim(stripslashes($_POST['position']));
$position2 = Trim(stripslashes($_POST['position2']));
$phone = Trim(stripslashes($_POST['phone']));
$invoice = Trim(stripslashes($_POST['invoice']));
//Let's start our headers
$headers = "From: $EmailFrom<" . $_POST['email'] . ">\n";
$headers .= "Reply-To: <" . $_POST['email'] . ">\n";
$headers .= "MIME-Version: 1.0\n";
$headers .= "Content-Type: multipart/related; type=\"multipart/alternative\"; boundary=\"----=MIME_BOUNDRY_main_message\"\n";
$headers .= "X-Sender: $from_name<" . $_POST['email'] . ">\n";
$headers .= "X-Mailer: PHP4\n";
$headers .= "X-Priority: 3\n"; //1 = Urgent, 3 = Normal
$headers .= "Return-Path: <" . $_POST['email'] . ">\n";
$headers .= "This is a multi-part message in MIME format.\n";
$headers .= "------=MIME_BOUNDRY_main_message \n";
$headers .= "Content-Type: multipart/alternative; boundary=\"----=MIME_BOUNDRY_message_parts\"\n";
$message = "------=MIME_BOUNDRY_message_parts\n";
$message .= "Content-Type: text/plain; charset=\"iso-8859-1\"\n";
$message .= "Content-Transfer-Encoding: quoted-printable\n";
$message .= "\n";
/* Add our message, in this case it's plain text. You could also add HTML by changing the Content-Type to text/html */
$message .= "Invoice: ";
$message .= $invoice;
$message .= "\n";
$message .= "\n";
$message .= "My Business Enquiry Form";
$message .= "\n";
$message .= "\n";
$message .= "Contact Details: ";
$message .= "\n";
$message .= "\n";
$message .= "Name: ";
$message .= $from_name;
$message .= "\n";
$message .= "Position: ";
$message .= $position;
$message .= "\n";
$message .= "Email: ";
$message .= $EmailFrom;
$message .= "\n";
$message .= "Phone: ";
$message .= $phone;
$message .= "\n";
$message .= "\n";
// send the message
mail("$to_name<$to_email>", $subject, $message, $headers);
print "Form sent successfully";
}
}
?>
<?
switch ($action) {
case "send":
sendMail();
showForm();
break;
default:
showForm();
}
?>
<div id="footer">
</div>
</div>
</body>
Start your php page with this code
if (!isset($_GET['id']) || empty($_GET['id']))
{
die("Bad Request");
}
This should solve your problem