What are the best practices for sending structured data, like a shopping cart, via email in PHP?
When sending structured data like a shopping cart via email in PHP, it's best to serialize the data into a format like JSON or XML before including it in the email. This ensures that the data remains intact and can be easily reconstructed when the email is received. Additionally, it's important to properly format the email content to make it readable and user-friendly for the recipient.
// Sample shopping cart data
$shoppingCart = [
'items' => [
['name' => 'Product 1', 'price' => 10.99],
['name' => 'Product 2', 'price' => 19.99]
],
'total' => 30.98
];
// Serialize the shopping cart data as JSON
$serializedCart = json_encode($shoppingCart);
// Prepare email content
$to = 'recipient@example.com';
$subject = 'Your Shopping Cart';
$message = 'Here is your shopping cart:' . PHP_EOL . PHP_EOL;
$message .= $serializedCart;
// Send email
mail($to, $subject, $message);