1

I've got a cake order form for my website, which works fine, and once everything is processed a pdf copy of it is sent to the customer and another to us so the order can be processed. I've now had a request to also have an XML file of the form values created and sent back to us as well but I'm not sure how to go about this. Everything I've seen speaks about actually creating an XML file first in my documents and then writing to it and attaching that, is this necessary? Or can it be like with my pdf which gets generated by my php script and then gets attached to the email without having to actually save the file to my documents first.

This is my order form:

<form method="POST" action="cakePayment.php" name="cakeForm" id="cakeForm" enctype="multipart/form-data" onSubmit="if(!confirm('Is the form filled out correctly?')){return false;}">


<p>What date is this required for?</p>
<p>Please note, we need 4 days clear notice(Monday-Friday)</p>
<input id="datefield" name="date" type='date' required onkeydown="return false" min='2019-05-10'></input>

<div class="contact" align="center">
<p>Please tell us who you are</p>
<table border="0" cellpadding="0" id="table1">

<tr>
<td align="right">Name</font></td>
<td><input type="text" name="name" id="name" required size="15" tabindex="1"></td>
</tr>
<tr>
<td align="right">Email</font> 
(Your confirmation will be sent here): </td>
<td><input type="text" name="email" id="email" required size="20" tabindex="1"></td>
</tr>
<tr>
<td align="right">Phone number:</td>
<td><input type="text" name="number" id="number" required size="15" tabindex="1"></td>
</tr>
<tr>
<td align="right">Address:</td>
<td><input type="text" name="address" id="address" required size="15" tabindex="1"></td>
</tr>
<tr>
<td align="right">Town:</td>
<td><input type="text" name="town" id="town" required size="15" tabindex="1"></td>
</tr>
<tr>
<td align="right">Postcode:</td>
<td><input type="text" name="postcode" id="postcode" required size="15" tabindex="1"></td>
</tr>
<tr>
<td align="right">County:</td>
<td><input type="text" name="county" id="county" required size="15" tabindex="1"></td>
</tr>
</table>
</div>
<p>And tell us what you would like:</p>




<div class="select-style">
<label>Cake Type</label>
<select id="cake_type" class="cake_type" name="cake_type" required>
<option value="0" selected="selected">Select One</option>
<option value="5|Round Golf Course" >Round Golf Course</option>
<option value="10|Oblong Football Pitch" >Oblong Football Pitch</option>
<option value="15|Round Chocolate Sweetie" >Round Chocolate Sweetie</option>
<option value="20|Round White Chocolate Sweetie" >Round White Chocolate Sweetie</option>
<option value="25|Round Unicorn Cake" >Round Unicorn Cake</option>
<option value="30|Round Drip Cake" >Round Drip Cake</option>
<option value="35|Football Cake" >Football Cake</option>
<option value="40|Yum Yum Cake"> Yum Yum Cake</option>
<option value="45|Oblong Carrot Cake" >Oblong Carrot Cake</option>
<option value="50|Round AFC Cake" >Round AFC Cake</option>
<option value="55|Square 8 inch Cake" >Square 8" Cake</option>
<option value="60|Oblong Cake" >Oblong Cake</option>
</select>
</div>
<br />
<br />



<div class="select-style">
<label>Collection Point</label> 
<select id="collection" required name="collection">
<option value="None" selected="selected">Select One</option>
<option value="Alford" >Alford</option>
<option value="Auld Toon" >Auld Toon</option>
<option value="Banff" >Banff</option>
<option value="Emmas">Emmas</option>
<option value="Insch" >Insch</option>
<option value="Kemnay" >Kemnay</option>
<option value="Market Place" >Market Place</option>
<option value="Mastrick"> Mastrick</option>
<option value="Meldrum Bakery" >Meldrum Bakery</option>
<option value="North Street" >North Street</option>
<option value="Rousay" >Rousay</option>
<option value="Seafield Street" >Seafield Street </option>
<option value="St Machar" >St Machar </option>
<option value="St Swithin" >St Swithin Street </option>
<option value="Stonehaven" >Stonehaven</option>
<option value="Torry" >Torry</option>
<option value="Keystore Old Aberdeen" >Keystore Old Aberdeen</option>
<option value="Keystore Old Meldrum" >Keystore Old Meldrum </option>
<option value="Highclere" >Highclere</option>
</select>
</div>

<br />
<br />

<div class= "colours">
<p class="icing">Icing Colour</p>
<input type="text" id="icing" name="icing" required></td>


<p>Trim Colour</p>
<input type="text" id="trim" name="trim" required></td>

</div>

<div class="select-style">
<label>Filling</label>
<select id="filling" name="filling">
<option value="" selected="selected">Select One</option>
<option value="jam_cream" >Jam & Cream</option>
<option value="jam" >Jam</option>
<option value="cream" >Cream</option>
<option value="chocolate">Chocolate Cream</option>

</select>
</div>
<br />
<br />

<p>Would you like a photo on your cake?(£7 extra)</p>
<div>
<label class="checkbox-inline"></label>
    <input type="radio" required name="photo[]" class="photo" id="yesPhoto" value="7">Yes
</label>
<label class="checkbox-inline">
<input type="radio" name="photo[]" class="photo" id="noPhoto" value="0"/>No
</div>


</label>
<br /><br />
<div class="7 box" style="display: none;">
<input type="file" name="image" id="image" onchange="readURL(this);" accept="image/*"/>
 <img id="cakePhoto" src="#" />

 </div>

 <div class="0 box" style="display: none;">
 <p>You do not want a photo on your cake</p>
 </div>

 <p>Wording</p>

 <textarea name="cakeWording" id="cakeWording" rows="10" cols="30"></textarea>

 <div class= "price">
 <p>Price</p>


</div>
 <br /><br />

 <div class = "total">
 <p>£</p>
 <input type="number" name="total"  id="totalPrice" value="0" readonly="readonly" size="8">


</div>


 <input type='submit' id='submit' name="submit" onClick="checkValue()" class="submit" value='Submit' />

</form>

This is my current script for emailing:

?php
// Starting session
require_once('includes/config.php');
require('mc_table.php');
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'C:\PHPMailer\PHPMailer-master\src\Exception.php';
require 'C:\PHPMailer\PHPMailer-master\src\PHPMailer.php';
require 'C:\PHPMailer\PHPMailer-master\src\SMTP.php';



$date = $_SESSION['date'];
$name = $_SESSION['name'];
$email = $_SESSION['email'];
$number = $_SESSION['number'];
$address = $_SESSION['address'];
$town =  $_SESSION['town'];
$postcode = $_SESSION['postcode'];
$county = $_SESSION['county'];
$cake_type = $_SESSION['cake_type'];
$collection = $_SESSION['collection'];
$icing = $_SESSION['icingColour'];
$trim = $_SESSION['trimColour'];
$filling = $_SESSION['filling'];
$photo = $_SESSION['photo'];
$cakePhoto = $_SESSION['cakePhoto'];
$wording = $_SESSION['wording'];
$price = $_SESSION['price'];


$mail = new PHPMailer(TRUE);


try {

   $mail->isHTML(true);
    $mail->setFrom('**********', 'Cake Order Form');
   $mail->addAddress('*********', 'Lewis');
   $mail->isSMTP();
   $mail->Host = 'smtp.gmail.com';
   $mail->SMTPAuth = TRUE;
   $mail->SMTPSecure = 'tls';
  $mail->Username = '*******';
   $mail->Password = '*********';
   $mail->Port = 587;
  $pdf=new PDF_MC_Table();
    $pdf->AddPage();
    $pdf->Image('images/logo.png',10,6,30);
    $pdf->SetFont('Arial','B',15);
    $pdf->Cell(80);
    $pdf->Cell(30,10,'JG Ross Cake Order','C');
     $pdf->Ln(20);
   $pdf->SetFont('Arial','B',16);

    $pdf->Cell(40,10, 'Date Required:');
$pdf->Cell(50); 
$pdf->Cell(40,10, $date);

$pdf->Ln();

$pdf->Cell(40,10, 'Name:');
$pdf->Cell(50); 
$pdf->Cell(40,10, $name);

$pdf->Ln();

 $pdf->Cell(40,10, 'Email:');
$pdf->Cell(50); 
$pdf->Cell(40,10, $email);

$pdf->Ln();

 $pdf->Cell(40,10, 'Number:');
$pdf->Cell(50); 
$pdf->Cell(40,10, $number);

$pdf->Ln();

 $pdf->Cell(40,10, 'Address:');
$pdf->Cell(50); 
$pdf->Cell(40,10, $address);

$pdf->Ln();

 $pdf->Cell(40,10, 'Town:');
$pdf->Cell(50); 
$pdf->Cell(40,10, $town);

$pdf->Ln();

 $pdf->Cell(40,10, 'Postcode:');
$pdf->Cell(50); 
$pdf->Cell(40,10, $postcode);

$pdf->Ln();

 $pdf->Cell(40,10, 'County:');
$pdf->Cell(50); 
$pdf->Cell(40,10, $cake_type);

$pdf->Ln();

 $pdf->Cell(40,10, 'Collecting From:');
$pdf->Cell(50); 
$pdf->Cell(40,10, $collection);

$pdf->Ln();

 $pdf->Cell(40,10, 'Icing Colour:');
$pdf->Cell(50); 
$pdf->Cell(40,10, $icing);

$pdf->Ln();

 $pdf->Cell(40,10, 'Trim Colour:');
$pdf->Cell(50); 
$pdf->Cell(40,10, $trim);

$pdf->Ln();

 $pdf->Cell(40,10, 'Filling:');
$pdf->Cell(50); 
$pdf->Cell(40,10, $filling);

$pdf->Ln();

 $pdf->Cell(40,10, 'Do you want a photo on your cake?');
$pdf->Cell(70); 
$pdf->Cell(40,10, $cakePhoto);

$pdf->Ln();

 $pdf->Cell(40,10, 'Wording:');
$pdf->Cell(50); 
$pdf->Cell(40,10, $wording);

$pdf->Ln();

 $pdf->Cell(40,10, 'Price:');
$pdf->Cell(50); 
$pdf->Cell(40,10, $price);

$pdf->Ln();


     $pdfdoc = $pdf->Output('', 'S'); 
       if ($mail->addReplyTo($email)) {
        $mail->Subject = 'Cake Order';

         //keeps it simple
        $mail->isHTML(true);
        // a simple message body
    $mail->Body = "A new cake order has been submitted";


    $mail->addStringAttachment($pdfdoc, 'createCake.pdf');
if (isset($_SESSION['photo'])) { 
  $mail->addAttachment($_SESSION['photo']);
}




        //Send the message, check for errors
        if (!$mail->send()) {
            //The reason for failing to send will be in $mail->ErrorInfo

            $msg = 'Sorry, something went wrong. Please try again later.';
        } else {
           // $msg = 'Message sent! Thanks for contacting us.';
            header("Location: createCake.php");
        }
    } else {
        $msg = 'Invalid email address, message ignored.';
    }



            $mail->ClearAddresses();
            $mail->AddAddress($email);
            $mail->Body = "Here is your confirmed cake order";
            $mail->addStringAttachment($pdfdoc, 'createCake.pdf');
            if (isset($_SESSION['photo'])) { 
            $mail->addAttachment($_SESSION['photo']);
        }
            $mail->send();



   /* Enable SMTP debug output. */
   $mail->SMTPDebug = 4;

  // $mail->send();


}

catch (Exception $e)
{
  echo $e->errorMessage();
}
catch (\Exception $e)
{
   echo $e->getMessage();

 } 

So what would I need to add in order to populate an xml file with form values such as $name and $number? Not sure how to even start going about it so any sort of guidance would be great

Update: I've got the xml created by doing the following:

/* create a dom document with encoding utf8 */
    $doc = new DOMDocument('1.0', 'UTF-8');
    $doc->formatOutput = true;

    /* create the root element of the xml tree */
    $xmlRoot = $doc->createElement("xml");
    /* append it to the document created */
    $xmlRoot = $doc->appendChild($xmlRoot);
    $root = $doc->createElement('OrderDetails');
    $root = $doc->appendChild($root);

    $ele1 = $doc->createElement('DateRequired');
    $ele1->nodeValue=$date;
    $root->appendChild($ele1);

    $ele2 = $doc->createElement('Name');
    $ele2->nodeValue=$name;
    $root->appendChild($ele2);

    $ele3 = $doc->createElement('Email');
    $ele3->nodeValue=$email;
    $root->appendChild($ele3);

    $ele4 = $doc->createElement('Number');
    $ele4->nodeValue=$number;
    $root->appendChild($ele4);

    $ele5 = $doc->createElement('Address');
    $ele5->nodeValue=$address;
    $root->appendChild($ele5);

    $ele6 = $doc->createElement('Town');
    $ele6->nodeValue=$town;
    $root->appendChild($ele6);

    $ele7 = $doc->createElement('Postcode');
    $ele7->nodeValue=$postcode;
    $root->appendChild($ele7);

    $ele8 = $doc->createElement('County');
    $ele8->nodeValue=$county;
    $root->appendChild($ele8);

    $ele9 = $doc->createElement('CakeType');
    $ele9->nodeValue=$cake_type;
    $root->appendChild($ele9);

    $ele10 = $doc->createElement('Collecting');
    $ele10->nodeValue=$collection;
    $root->appendChild($ele10);

    $ele11 = $doc->createElement('Icing');
    $ele11->nodeValue=$icing;
    $root->appendChild($ele11);

    $ele12 = $doc->createElement('Trim');
    $ele12->nodeValue=$trim;
    $root->appendChild($ele12);

    $ele13 = $doc->createElement('Filling');
    $ele13->nodeValue=$filling;
    $root->appendChild($ele13);

    $ele14 = $doc->createElement('Photo');
    $ele14->nodeValue=$cakePhoto;
    $root->appendChild($ele14);

    $ele15 = $doc->createElement('Wording');
    $ele15->nodeValue=$wording;
    $root->appendChild($ele15);

    $ele16 = $doc->createElement('Price');
    $ele16->nodeValue=$price;
    $root->appendChild($ele16);

    $doc->saveXML();

And I've tried to attach it to the email by doing the following:

 $mail->addStringAttachment($doc->asXML(), "xml.xml");

but I am getting the error: Fatal error: Uncaught Error: Call to undefined method DOMDocument::asXML()

where am I going wrong with the attachment?

Lewis Ross
  • 153
  • 2
  • 13

1 Answers1

1

Use DOMDocument or SimpleXMLElement to create the XML in PHP. Then use the addStringAttachment like you are using currently, for your XML.

Eg. if $xml is the XML object generated, you can attach its string version as:

$mail->addStringAttachment($xml->asXML(), "xml.xml");

More info on how to create XML here: How to generate XML file dynamically using PHP?

Anurag Srivastava
  • 14,077
  • 4
  • 33
  • 43