使用 html 表单值填充 xml 文件并在 phpmailer 中作为附件发送

Populate xml file with html form values and send as attachment in phpmailer

我的网站有一个蛋糕订单表,工作正常,所有处理完毕后,将一份 pdf 副本发送给客户,另一份发送给我们,以便处理订单。我现在收到了一个请求,要求我也创建一个包含表单值的 XML 文件,并将其发回给我们,但我不确定该怎么做。我所看到的一切都在谈论实际上首先在我的文档中创建一个 XML 文件,然后写入并附加它,这是必要的吗?或者它可以像我的 pdf 一样,它由我的 php 脚本生成,然后附加到电子邮件,而不必先将文件实际保存到我的文档中。

这是我的订单:

<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>

这是我当前的电子邮件脚本:

?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();

 } 

那么我需要添加什么才能用 $name 和 $number 等表单值填充 xml 文件?不确定如何开始着手进行,所以任何形式的指导都会很棒

更新:我通过以下操作创建了 xml:

/* 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();

我尝试通过执行以下操作将其附加到电子邮件中:

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

但我收到错误:致命错误:未捕获错误:调用未定义的方法 DOMDocument::asXML()

附件哪里出错了?

使用 DOMDocumentSimpleXMLElement 在 PHP 中创建 XML。然后像您当前使用的那样使用 addStringAttachment 作为您的 XML.

例如。如果 $xml 是生成的 XML 对象,您可以将其字符串版本附加为:

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

有关如何在此处创建 XML 的更多信息:How to generate XML file dynamically using PHP?