• Announcements

    • boen_robot

      Guidelines and Netiquette   03/28/2017

      Posting Problems:   Having problems posting your topic? Read through this: To join, you agree to our terms and conditions and fill out and submit a registration form. An activation email will be sent to your email adress, so you'll need to verify your account. After that the account has to be validated by one of the moderators. This will mean that it can take up to a day to be activated. A couple of things to remember to ensure approval: Don't use an email address in one of those $2 four character .com domains eg. xyds.com. These will be deleted and the domain added to the banned list. Don't use an email address that is within a domain with a bad reputation for spam. A Google search is run on every email address and email domain. Don't sign up with an email address that doesn't exist, doesn't work or requires the sender to answer a quiz before their email can get to you. Put your country and or state and city in the signup form. Blank forms will go to the botton of the "to do" list. And make sure that your email address and your country match, saying you're from Alabama and using a .ru email address is not going to get you activated. After a membership is activated the first few posts will be monitored. Posting spam or unapproved topics described in the agreement results in an immediate ban. The email provider and the IP addresses associated with the account will be banned and all posts will be deleted. These strict measures have been deemed necessary to hinder spam. Sorry for any inconvenience this causes, but it's not liable to change. If, after reading this, you still can't post and don't understand why, contact one of the Moderators listed here.   Topic Guidelines   Including the following information can expedite an accurate response from board members: Must be a Specific Problem or Question related to web design and development Include Code in Question (wrap with   for small blocks of code and for longer blocks   ) Include Code Author Include Extra Notes/Modifications/Attempts Include web link to page/file when possible Content Guidelines   You may not post, upload, link to, or email any Content that contains, promotes, gives instruction about, or provides prohibited Content. Prohibited Content includes any Content that breaks any local, state, county, national or international law. Prohibited Content also includes: No direct or indirect advertising or websites, forums, products, services No hijacking of posts (do not post your question in someone elses) Content that infringes upon any rights [ex. MP3s and ROMs] (including, but not limited to, copyrights and trademarks) Abusive, threatening, defamatory, racist, or obscene Viruses or any other harmful computer software False Information or libel Spam, chain letters, or Pyramid schemes Gambling or Illicit drugs Terrorism Hacking or cheating for internet/online games Warez, Roms, CD-Keys, Cracks, Passwords, or Serial Numbers Pornography, nudity, or sexual material of any kind Excessive profanity Invasive of privacy or impersonation of any person/entity Hacking materials or information Posting Tips   There are more BBcodes than there are buttons for on the reply menu. To get the full list, click "BB Code Help" underneath the clickable smilie face menu. Use   for small snippets of code Use   for lengthy snippets of code Use   if your snippet is HTML (optional) Use   if your snippet is SQL (optional) Rules of Conduct   Be nice. There's no need for calling someone stupid if they ask an 'easy' question. Keep your avatars and signatures absolutely child friendly. We have a younger audience on this forum. Keep your language appropriate for the same reason above. Do not PM moderators for help on the forum. Post on the topic, or create a new one.   Spam:   Recently, as you have all without doubt noticed, we have had lots of spam and advertisement on the forum. Therefore, we'd like to alert you as to what to do when you have found any of the aforementioned annoying messages: it. Immediately. Give a clear reason, please, if the advertising is not evident. DO NOT POST! Report, let the post stay as is, and we will get to it, meanwhile if you continue to post as normal in the other threads, it won't be on the top so long. Refrain from PMing the member. This won't help at all, as they are most likely spambots anyway. Thank you.       Images in signatures:   After thinking of users on dial-up, we have decided to enforce the following rules regarding signatures. Please pay heed to them. Respecting these rules is respecting the members on this forum with dial-up. Signature rules: No animated images AT ALL. No matter the amount of animation. Maximum image widthxheight: 300x150 Maximum image (file) size: 15kb Use calm colors. Do not use highly contrasting images in your signature, as this can get really annoying when seeing several posts from one member in the same thread. The same prohibited content goes for images as for posts. Lastly, use common sense. No lengthy signatures please. Save us some scrolling. Thank you.       Links in signatures:   Please understand that w3schools.com only exists because of voluntary work and is barely supported by the advertising littered throughout the tutorials and the forum. So, please, stop advertising other sites. DO not post links that drive traffic away from the w3schools domain - especially to a site that offers similar if not identical information. Please help support the site by keeping individuals on it. Thank you. Here are some guidelines as to what you can put in your signature: w3schools links --> allowed w3.org links --> allowed browser links --> allowed html editor links --> allowed personal sites --> allowed tutorial sites competing with w3schools --> NOT allowed sites completely irrelevant to webprogramming and this forum --> NOT allowed   Thanks for understanding, and for taking the time to read this. ~W3Schools Modstaff~
Sign in to follow this  
Followers 0
BeckyKing

Php form to send XML

30 posts in this topic

I want to create a PHP form that sends the details from the form as a XML attachment. What is the easiest way to do this?

Share this post


Link to post
Share on other sites

Thank you. I am very new to PHP and XML and still abit confused. Any chance anyone can give me abit of code to work with? I have the PHP form just need the bit where I add the XML attachment. I know there were a lot of examples in those links but as I said still confused. Thank you in advance

Share this post


Link to post
Share on other sites

We're really here to help people learn how to program, rather than write code for people. There should be some examples you can find online though:https://www.google.com/search?client=opera&q=php+create+xml+from+form+data&sourceid=opera&ie=UTF-8&oe=UTF-8If you have specific questions about how things work, we can help fill in the blanks.

Share this post


Link to post
Share on other sites

How do I now attach the xml doc? Thats if I have done the creating of the xml doc correct?

 

 

Form:

<form role="form" method="POST" action="contact-form-submission.php">
<div class="row">
<div class="form-group col-lg-4">
<label for="input1">Name</label>
<input type="text" name="contact_name" class="form-control" id="input1">
</div>
<div class="form-group col-lg-4">
<label for="input2">Email Address</label>
<input type="email" name="contact_email" class="form-control" id="input2">
</div>
<div class="form-group col-lg-4">
<label for="input3">Phone Number</label>
<input type="phone" name="contact_phone" class="form-control" id="input3">
</div>
<div class="clearfix"></div>
<div class="form-group col-lg-12">
<label for="input4">Message</label>
<textarea name="contact_message" class="form-control" rows="6" id="input4"></textarea>
</div>
<div class="form-group col-lg-12">
<input type="hidden" name="save" value="contact">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</div>
</form>
PHP:
<?php
/* Create XML Document */
$xmlDoc = new DOMDocument('1.0');
/* Build Maximizer XML file */
$xmlRoot = $xmlDoc->createElement('AllData');
$xmlDoc->appendChild($xmlRoot);
$xmlIndividual = $xmlDoc->createElement('Individual');
$xmlname = $xmlDoc->createElement('contact_name', $input_name);
$xmlIndividual->appendChild($xmlname);
$xmlemail_address = $xmlDoc->createElement('contact_email', $input_email_address);
$xmlIndividual->appendChild($xmlemail_address);
$xmlphone = $xmlDoc->createElement('contact_phone', $input_phone);
$xmlIndividual->appendChild($xmlphone);
$xmlmessage = $xmlDoc->createElement('contact_message', $input_message);
$xmlIndividual->appendChild($xmlmessage);
$content = chunk_split(base64_encode($xmlDoc->saveXML()));
/* No need to create an actual file, just use the content of the created XML document */
$content = $xmlDoc->saveXML();
// check for form submission - if it doesn't exist then send back to contact form
if (!isset($_POST['save']) || $_POST['save'] != 'contact') {
header('Location: contact.php'); exit;
}
// get the posted data
$name = $_POST['contact_name'];
$email_address = $_POST['contact_email'];
$phone = $_POST['contact_phone'];
$message = $_POST['contact_message'];
// check that a name was entered
if (empty($name))
$error = 'You must enter your name.';
// check that an email address was entered
elseif (empty($email_address))
$error = 'You must enter your email address.';
// check for a valid email address
elseif (!preg_match('/^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,3})$/', $email_address))
$error = 'You must enter a valid email address.';
// check that a phone number was entered
if (empty($phone))
$error = 'You must enter your phone number.';
// check that a message was entered
elseif (empty($message))
$error = 'You must enter a message.';
// check if an error was found - if there was, send the user back to the form
if (isset($error)) {
header('Location: contact.php?e='.urlencode($error)); exit;
}
$headers = "From: $email_addressrn";
$headers .= "Reply-To: $email_addressrn";
// write the email content
$email_content = "Name: $namen";
$email_content .= "Email Address: $email_addressn";
$email_content .= "Phone Number: $phonen";
$email_content .= "Message:nn$message";
// send the email
//ENTER YOUR INFORMATION BELOW FOR THE FORM TO WORK!
mail ('becky.king99@gmail.com', 'Westover', $email_content, $headers);
// send the user back to the form
header('Location: contact.html?s='.urlencode('Thank you for your message.')); exit;
?>

Share this post


Link to post
Share on other sites

Using PHP's mail function to send with attachments means that you have to build the entire message and headers yourself. There are other PHP packages to send mail that will handle that for you, such as PHPMailer. With PHPMailer, for example, it would be as easy as this to add your attachment:$mail->addStringAttachment($xml, 'filename.xml', 'base64', 'application/xml', 'attachment');If you want to stick with PHP's regular mail function, then there's an example function here to do that:http://stackoverflow.com/questions/4586557/php-send-email-with-attachmentThat function expects a filename though, and you aren't saving a file, you're just generating a string of XML. The function can be modified like this:

function mail_attachment($to, $subject, $message, $from, $data, $filename) {  $content = chunk_split(base64_encode($data));   $uid = md5(uniqid(time()));  $from = str_replace(array("r", "n"), '', $from); // to prevent email injection  $header = "From: ".$from."rn"      ."MIME-Version: 1.0rn"      ."Content-Type: multipart/mixed; boundary="".$uid.""rnrn"      ."This is a multi-part message in MIME format.rn"       ."--".$uid."rn"      ."Content-type:text/plain; charset=iso-8859-1rn"      ."Content-Transfer-Encoding: 7bitrnrn"      .$message."rnrn"      ."--".$uid."rn"      ."Content-Type: application/octet-stream; name="".$filename.""rn"      ."Content-Transfer-Encoding: base64rn"      ."Content-Disposition: attachment; filename="".$filename.""rnrn"      .$content."rnrn"      ."--".$uid."--";   return mail($to, $subject, "", $header);}
So, you pass that function the to address, subject, email text, from address, the file data, and the filename you want to use, and you use that function instead of PHP's mail function (it calls mail at the end).Your code has these lines:
$content = chunk_split(base64_encode($xmlDoc->saveXML()));  /* No need to create an actual file, just use the content of the created XML document */$content = $xmlDoc->saveXML();
Both of those are setting the $content variable with your XML data. There's no need to do that twice, and since that function expects normal un-encoded data, just remove the first line.At the end of your code, you have lines where you set the From and Reply-To headers, you can delete those lines. Then you have a section where you build the $email_content variable with the email text, and after that you call the mail function and send it everything. Keep the part where you build $email_content, but remove the line where you call the mail function. Instead of that line, add this line to use the above function:
mail_attachment('becky.king99@gmail.com', 'Westover', $email_content, $email_address, $content, 'attachment.xml');
In that line, $content is the XML string that you generated, and you can change the last value if you want to change the filename of the attachment. Make sure you also include that function definition somewhere in your code.

Share this post


Link to post
Share on other sites

Thank you for your help I have a fully working form that attaches the xml document.

 

My problem now is that the information is not being transferred onto the xml document? so when i open the xml document attachment there is no information?

Share this post


Link to post
Share on other sites

In the code you posted above you're using variables like $input_name, $input_email_address, etc. Make sure those variables have the values that you want before you write them to the XML file.

Share this post


Link to post
Share on other sites

You have lines like this:$xmlname = $xmlDoc->createElement('contact_name', $input_name);If $input_name isn't set to anything, then the XML file isn't going to contain anything for that element. Get those values from the form and set those variables before trying to write them into the XML document.

Share this post


Link to post
Share on other sites

This didn't seem to work?

<?php
/* Create XML Document */
$xmlDoc = new DOMDocument('1.0');
/* Build Maximizer XML file */
$xmlRoot = $xmlDoc->createElement('AllData');
$xmlDoc->appendChild($xmlRoot);
$xmlIndividual = $xmlDoc->createElement('Individual');
$xmlname = $xmlDoc->createElement('contact_name', $name);
$xmlIndividual->appendChild($xmlname);
$name = $_POST["contact_name"];
$xmlemail_address = $xmlDoc->createElement('contact_email', $email_address);
$xmlIndividual->appendChild($xmlemail_address);
$email_address = $_POST["contact_email"];
$xmlphone = $xmlDoc->createElement('contact_phone', $phone_number);
$xmlIndividual->appendChild($xmlphone);
$phone_number = $_POST["contact_phone"];
$xmlmessage = $xmlDoc->createElement('contact_message', $message);
$xmlIndividual->appendChild($xmlmessage);
$message = $_POST["contact_message"];
/* No need to create an actual file, just use the content of the created XML document */
$content = $xmlDoc->saveXML();
// check for form submission - if it doesn't exist then send back to contact form
if (!isset($_POST['save']) || $_POST['save'] != 'contact') {
header('Location: contact.php'); exit;
}
// get the posted data
$name = $_POST['contact_name'];
$email_address = $_POST['contact_email'];
$phone = $_POST['contact_phone'];
$message = $_POST['contact_message'];
// check that a name was entered
if (empty($name))
$error = 'You must enter your name.';
// check that an email address was entered
elseif (empty($email_address))
$error = 'You must enter your email address.';
// check for a valid email address
elseif (!preg_match('/^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,3})$/', $email_address))
$error = 'You must enter a valid email address.';
// check that a phone number was entered
if (empty($phone))
$error = 'You must enter your phone number.';
// check that a message was entered
elseif (empty($message))
$error = 'You must enter a message.';
// check if an error was found - if there was, send the user back to the form
if (isset($error)) {
header('Location: contact.php?e='.urlencode($error)); exit;
}
// write the email content
$email_content = "Name: $namen";
$email_content .= "Email Address: $email_addressn";
$email_content .= "Phone Number: $phonen";
$email_content .= "Message:nn$message";
mail_attachment('becky.king99@gmail.com', 'Westover', $email_content, $email_address, $content, 'test2.xml');
// send the user back to the form
header('Location: contact.html?s='.urlencode('Thank you for your message.')); exit;
function mail_attachment($to, $subject, $message, $from, $data, $test2) {
$content = chunk_split(base64_encode($data));
$uid = md5(uniqid(time()));
$from = str_replace(array("r", "n"), '', $from); // to prevent email injection
$header = "From: ".$from."rn"
."MIME-Version: 1.0rn"
."Content-Type: multipart/mixed; boundary="".$uid.""rnrn"
."This is a multi-part message in MIME format.rn"
."--".$uid."rn"
."Content-type:text/plain; charset=iso-8859-1rn"
."Content-Transfer-Encoding: 7bitrnrn"
.$message."rnrn"
."--".$uid."rn"
."Content-Type: application/octet-stream; name="".$test2.""rn"
."Content-Transfer-Encoding: base64rn"
."Content-Disposition: attachment; filename="".$test2.""rnrn"
.$content."rnrn"
."--".$uid."--";
return mail($to, $subject, "", $header);
}
?>
Form:
<form role="form" method="POST" action="contact-form-submission.php">
<div class="row">
<div class="form-group col-lg-4">
<label for="input1">Name</label>
<input type="text" name="contact_name" class="form-control" id="input1">
</div>
<div class="form-group col-lg-4">
<label for="input2">Email Address</label>
<input type="email" name="contact_email" class="form-control" id="input2">
</div>
<div class="form-group col-lg-4">
<label for="input3">Phone Number</label>
<input type="phone" name="contact_phone" class="form-control" id="input3">
</div>
<div class="clearfix"></div>
<div class="form-group col-lg-12">
<label for="input4">Message</label>
<textarea name="contact_message" class="form-control" rows="6" id="input4"></textarea>
</div>
<div class="form-group col-lg-12">
<input type="hidden" name="save" value="contact">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</div>
</form>

Share this post


Link to post
Share on other sites

You're getting the values from the form after building the XML document, move the code that gets the values to the top.

Share this post


Link to post
Share on other sites

I have moved the code to the top but it still will not create the data for the XML document?

Share this post


Link to post
Share on other sites

If you moved this:$name = $_POST["contact_name"];before this:$xmlname = $xmlDoc->createElement('contact_name', $name);And the XML document has a contact_name element that is empty, then it sounds like that field wasn't filled in on the form (or else it wouldn't be empty). Try printing out the XML document (the $content variable) after creating it to see what it actually creates.

Share this post


Link to post
Share on other sites

I have moved the code to where you said but my XML is still not being generated.

 

Do i need to write anything in the XML document or write the XML code in the PHP?

 

<?php
/* Create XML Document */
$xmlDoc = new DOMDocument('1.0');
/* Build Maximizer XML file */
$xmlRoot = $xmlDoc->createElement('AllData');
$xmlDoc->appendChild($xmlRoot);
$xmlIndividual = $xmlDoc->createElement('Individual');
$contact_name = $_POST["contact_name"];
$contact_email = $_POST["contact_email"];
$contact_phone = $_POST["contact_phone"];
$contact_message = $_POST["contact_message"];
$xmlname = $xmlDoc->createElement('contact_name', $contact_name);
$xmlIndividual->appendChild($xmlname);
$xmlemail_address = $xmlDoc->createElement('contact_email', $contact_email);
$xmlIndividual->appendChild($xmlemail_address);
$xmlphone = $xmlDoc->createElement('contact_phone', $contact_phone);
$xmlIndividual->appendChild($xmlphone);
$xmlmessage = $xmlDoc->createElement('contact_message', $contact_message);
$xmlIndividual->appendChild($xmlmessage);
/* No need to create an actual file, just use the content of the created XML document */
$content = $xmlDoc->saveXML();
// check for form submission - if it doesn't exist then send back to contact form
if (!isset($_POST['save']) || $_POST['save'] != 'contact') {
header('Location: contact.php'); exit;
}
// get the posted data
$name = $_POST['contact_name'];
$email_address = $_POST['contact_email'];
$phone = $_POST['contact_phone'];
$message = $_POST['contact_message'];
// check that a name was entered
if (empty($name))
$error = 'You must enter your name.';
// check that an email address was entered
elseif (empty($email_address))
$error = 'You must enter your email address.';
// check for a valid email address
elseif (!preg_match('/^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,3})$/', $email_address))
$error = 'You must enter a valid email address.';
// check that a phone number was entered
if (empty($phone))
$error = 'You must enter your phone number.';
// check that a message was entered
elseif (empty($message))
$error = 'You must enter a message.';
// check if an error was found - if there was, send the user back to the form
if (isset($error)) {
header('Location: contact.php?e='.urlencode($error)); exit;
}
// write the email content
$email_content = "Name: $namen";
$email_content .= "Email Address: $email_addressn";
$email_content .= "Phone Number: $phonen";
$email_content .= "Message:nn$message";
mail_attachment('becky.king99@gmail.com', 'Westover', $email_content, $email_address, $content, 'test2.xml');
// send the user back to the form
header('Location: contact.html?s='.urlencode('Thank you for your message.')); exit;
function mail_attachment($to, $subject, $message, $from, $data, $test2) {
$content = chunk_split(base64_encode($data));
$uid = md5(uniqid(time()));
$from = str_replace(array("r", "n"), '', $from); // to prevent email injection
$header = "From: ".$from."rn"
."MIME-Version: 1.0rn"
."Content-Type: multipart/mixed; boundary="".$uid.""rnrn"
."This is a multi-part message in MIME format.rn"
."--".$uid."rn"
."Content-type:text/plain; charset=iso-8859-1rn"
."Content-Transfer-Encoding: 7bitrnrn"
.$message."rnrn"
."--".$uid."rn"
."Content-Type: application/octet-stream; name="".$test2.""rn"
."Content-Transfer-Encoding: base64rn"
."Content-Disposition: attachment; filename="".$test2.""rnrn"
.$content."rnrn"
."--".$uid."--";
return mail($to, $subject, "", $header);
}
?>

Share this post


Link to post
Share on other sites

The variable $content contains the XML content. Print that out to see what it's generating.

Share this post


Link to post
Share on other sites

I know its probably simple but I keep getting errors when I try to print out the variable $content?

 

What is the best code to use and where abouts do I put it?

Share this post


Link to post
Share on other sites

Just use "echo $content;", and put it anywhere after the $content variable is created. You'll need to view the HTML source of the page to see the actual content without the browser trying to render it. You'll get an error about headers already being sent unless you comment out the header lines so that it doesn't try to redirect you.

Share this post


Link to post
Share on other sites

You're not doing anything with the $xmlindividual node. You create that node, and add all of the child nodes to it, but nothing else. After adding the child nodes to it you should append that node to the root node, which will add it to the XML document.

Share this post


Link to post
Share on other sites

Your code uses appendChild in several places. Every XML node has an appendChild method. You need to use the root node's appendChild method to append the individual node.

Share this post


Link to post
Share on other sites

I have used $xmlroot->appendChild($xmlindividual); and tried $xmlroot->appendChild($xmlfirstname); But keep getting and error 'Fatal error: Call to a member function appendChild() on a non-object in /websites/123reg/LinuxPackage22/ki/ng/_c/king-creations.co.uk/public_html/beckyking/contact-form-submission.php on line 23'

 

Code:

<?php

/* Create XML Document */
$xmlDoc = new DOMDocument('1.0');
/* Build Maximizer XML file */
$xmlRoot = $xmlDoc->createElement('AllData');
$xmlDoc->appendChild($xmlRoot);
$xmlIndividual = $xmlDoc->createElement('Individual');
$firstname = $_POST["firstname"];
$surname = $_POST["surname"];
$email = $_POST["email"];
$phone = $_POST["phone"];
$brand = $_POST["brand"];
$vehmodel = $_POST["vehmodel"];
$vehcolor = $_POST["vehcolor"];
$contact_message = $_POST["contact_message"];
$xmlfirstname = $xmlDoc->createElement('firstname', $firstname);
$xmlIndividual->appendChild($xmlfirstname);
$xmlroot->appendChild($xmlIndividual); "<<<<--- line 23 "
$xmlsurname = $xmlDoc->createElement('surname', $surname);
$xmlIndividual->appendChild($xmlsurname);
$xmlroot->appendChild($xmlIndividual);
$xmlemail = $xmlDoc->createElement('email', $email);
$xmlIndividual->appendChild($xmlemail);
$xmlroot->appendChild($xmlIndividual);
$xmlphone = $xmlDoc->createElement('phone', $phone);
$xmlIndividual->appendChild($xmlphone);
$xmlroot->appendChild($xmlIndividual);
$xmlbrand = $xmlDoc->createElement('brand', $brand);
$xmlIndividual->appendChild($xmlbrand);
$xmlroot->appendChild($xmlIndividual);
$xmlvehmodel = $xmlDoc->createElement('vehmodel', $vehmodel);
$xmlIndividual->appendChild($xmlvehmodel);
$xmlroot->appendChild($xmlIndividual);
$xmlvehcolor = $xmlDoc->createElement('vehcolor', $vehcolor);
$xmlIndividual->appendChild($xmlvehcolor);
$xmlroot->appendChild($xmlIndividual);
$xmlmessage = $xmlDoc->createElement('contact_message', $contact_message);
$xmlIndividual->appendChild($xmlmessage);
$xmlroot->appendChild($xmlIndividual);
/* No need to create an actual file, just use the content of the created XML document */
$content = $xmlDoc->saveXML();
// check for form submission - if it doesn't exist then send back to contact form
if (!isset($_POST['save']) || $_POST['save'] != 'contact') {
header('Location: contact.php'); exit;
}
// get the posted data
$firstname = $_POST['firstname'];
$surname = $_POST['surname'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$brand = $_POST['brand'];
$vehmodel = $_POST['vehmodel'];
$vehcolor = $_POST['vehcolor'];
$message = $_POST['contact_message'];
// check that a name was entered
if (empty($firstname))
$error = 'You must enter your name.';
// check that an surname was entered
elseif (empty($surname))
$error = 'You must enter your surname.';
// check that a phone number was entered
if (empty($phone))
$error = 'You must enter your phone number.';
// check that an email address was entered
elseif (empty($email))
$error = 'You must enter an email address.';
// check for a valid email address
elseif (!preg_match('/^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,3})$/', $email))
$error = 'You must enter a valid email address.';
// check that a phone number was entered
if (empty($phone))
$error = 'You must enter your phone number.';
// check that a Franchise was entered
if (empty($brand))
$error = 'You must enter a Franchise.';
// check that a message was entered
elseif (empty($message))
$error = 'You must enter a message.';
// check if an error was found - if there was, send the user back to the form
if (isset($error)) {
header('Location: contact.php?e='.urlencode($error)); exit;
}
// write the email content
$email_content = "First Name: $firstnamen";
$email_content .= "Surname: $surnamen";
$email_content .= "Email: $emailn";
$email_content .= "Phone Number: $phonen";
$email_content .= "Franchise: $brandn";
$email_content .= "Model: $vehmodeln";
$email_content .= "Colour: $vehcolorn";
$email_content .= "Message:nn$message";
mail_attachment('becky.king99@gmail.com', 'Westover', $email_content, $email, $content, 'test2.xml');
// send the user back to the form
header('Location: contact.html?s='.urlencode('Thank you for your message.')); exit;
function mail_attachment($to, $subject, $message, $from, $data, $test2) {
$content = chunk_split(base64_encode($data));
$uid = md5(uniqid(time()));
$from = str_replace(array("r", "n"), '', $from); // to prevent email injection
$header = "From: ".$from."rn"
."MIME-Version: 1.0rn"
."Content-Type: multipart/mixed; boundary="".$uid.""rnrn"
."This is a multi-part message in MIME format.rn"
."--".$uid."rn"
."Content-type:text/plain; charset=iso-8859-1rn"
."Content-Transfer-Encoding: 7bitrnrn"
.$message."rnrn"
."--".$uid."rn"
."Content-Type: application/octet-stream; name="".$test2.""rn"
."Content-Transfer-Encoding: base64rn"
."Content-Disposition: attachment; filename="".$test2.""rnrn"
.$content."rnrn"
."--".$uid."--";
return mail($to, $subject, "", $header);
}
?>
Edited by BeckyKing

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0