开发者

SimpleXMLElement Help with putting XML in PHP Mail

开发者 https://www.devze.com 2023-02-21 20:23 出处:网络
I\'m having problems putting the contents of my XML webhook into a PHP mail script.... For some reason I can\'t access the variables... I must be doing something wrong...

I'm having problems putting the contents of my XML webhook into a PHP mail script.... For some reason I can't access the variables... I must be doing something wrong...

Can anyone help?

    // Get XML data and read it into a string for use with SimpleXML
$xmlData = fopen('php://input' , 'rb'); 
while (!feof($xmlData)) { $xmlString .= fread($xmlData, 4096); }
fclose($xmlData);
$xml = new SimpleXMLElement($xmlString);

// Extract the name, email, country
$user_email         = trim($xml->email);
$user_first_name    = trim($xml->{'billing-address'}->{'first-name'});
$user_last_name     = trim($xml->{'billing-address'}->{'last-name'});
$user_address1      = trim($xml->{'billing-address'}->address1);
$user_address2      = trim($xml->{'billing-address'}->address2);
$user_phone         = trim($xml->{'billing-address'}->phone);
$user_province      = trim($xml->{'billing-开发者_如何学Pythonaddress'}->province);
$user_zip           = trim($xml->{'billing-address'}->zip);
$user_city          = trim($xml->{'billing-address'}->city);
$user_country       = trim($xml->{'billing-address'}->country);
$date               = trim($xml->{'order'}->{'created-at'});

foreach ($xml->{'line-items'}->{'line-item'} as $lineItem) {
  array_push($productTitles, trim($lineItem->title));
}

// multiple recipients
$to  = 'firstemail@hotmail.com'; 

// subject
$subject = 'myemail Purchase - '.$user_first_name.' '.$user_last_name.'';

$message = '
<html>
<head>
  <title>myemail Purchase</title>
</head>
<body>
  <p>Here are the details</p>
  <table><tr>';

$message .= '<td>'.$user_first_name.' '.$user_last_name.'</td>';  
$message .= '<td>';  

$message .= '</td>';  

$message .= '<td>'.$date.'</td>';  

$message .= '</tr></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";

// Additional headers
$headers .= 'To: Charles <myemail@hotmail.com>' . "\r\n";
$headers .= 'From: myemail.com <myemail.com>' . "\r\n";

// Mail it
mail($to, $subject, $message, $headers);

Here is the XML I'm getting...

<?xml version="1.0" encoding="UTF-8"?>
<order>
  <buyer-accepts-marketing type="boolean">false</buyer-accepts-marketing>
  <cart-token nil="true"></cart-token>
  <closed-at type="datetime" nil="true"></closed-at>
  <created-at type="datetime">2005-07-31T15:57:11Z</created-at>
  <currency>USD</currency>
  <email>bob@customer.com</email>
  <financial-status>paid</financial-status>
  <fulfillment-status nil="true"></fulfillment-status>
  <gateway>bogus</gateway>
  <id type="integer">516163746</id>
  <note nil="true"></note>
  <number type="integer">1</number>
  <shop-id type="integer">820947126</shop-id>
  <subtotal-price type="integer">10.00</subtotal-price>
  <taxes-included type="boolean">false</taxes-included>
  <total-discounts type="integer">0.00</total-discounts>
  <total-line-items-price type="integer">10.00</total-line-items-price>
  <total-price type="integer">11.50</total-price>
  <total-tax type="integer">1.50</total-tax>
  <total-weight type="integer">0</total-weight>
  <updated-at type="datetime">2005-08-01T15:57:11Z</updated-at>
  <name>#1001</name>
  <billing-address>
    <address1>123 Amoebobacterieae St</address1>
    <address2></address2>
    <city>Ottawa</city>
    <company></company>
    <country>Canada</country>
    <first-name>Bob</first-name>
    <id type="integer">943494288</id>
    <last-name>Bobsen</last-name>
    <phone>(555)555-5555</phone>
    <province>Ontario</province>
    <shop-id type="integer">820947126</shop-id>
    <zip>K2P0V6</zip>
    <name>Bob Bobsen</name>
  </billing-address>
  <shipping-address>
    <address1>123 Amoebobacterieae St</address1>
    <address2></address2>
    <city>Ottawa</city>
    <company></company>
    <country>Canada</country>
    <first-name>Bob</first-name>
    <id type="integer">943494288</id>
    <last-name>Bobsen</last-name>
    <phone>(555)555-5555</phone>
    <province>Ontario</province>
    <shop-id type="integer">820947126</shop-id>
    <zip>K2P0V6</zip>
    <name>Bob Bobsen</name>
  </shipping-address>
  <line-items type="array">
    <line-item>
      <fulfillment-service>manual</fulfillment-service>
      <grams type="integer">1500</grams>
      <id type="integer">642703538</id>
      <price type="integer">10.00</price>
      <quantity type="integer">1</quantity>
      <sku>1</sku>
      <title>Draft</title>
      <variant-id type="integer">47052976</variant-id>
      <vendor nil="true"></vendor>
      <name>Draft - 151cm</name>
    </line-item>
  </line-items>
</order>


All the xml nodes look like they're being accessed correctly and their values assigned to PHP variables. Your only issue should be $productTitles being uninitialized before the array_push(), which will throw an E_WARNING if error_reporting allows for it.

Add these on top to see PHP complaining:

error_reporting(E_ALL) ;
ini_set('display_errors', 1) ;

Then add this line before the foreach() to fix it:

$productTitles = array() ;

However I don't see it being included in the email text. Is something else not being showed in the email?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号