Setup & Send Email Using Mailgun in 10 Min with 2023 PHP Examples

Send Email using Mailgun in PHP Code Example We recommend using the Mailgun PHP SDK to send emails in PHP. You can quickly install it through composer. Once you include Mailgun in your file, you can create a new Mailgun instance and pass the sendMessage to send a new email. Require the composer mailgun/mailgun-php package. Include the composer autoload file in your application Create a new Mailgun instance inside of your file. Pass in your mailgun api key into the constructor for Mailgun. Set up your email content. Use the Mailgun sendMessage function to send the email. # First, instantiate the SDK with your API credentials and define your domain. $mgClient = new Mailgun("key-example"); $domain = "example.com"; # Now, compose and send your message. $mgClient->sendMessage($domain, array('from' => 'Sender <sender@gmail.com>', 'to' => 'Receiver <receiver.com>', 'subject' => 'The Printer Caught Fire', 'text' => 'We have a problem.')); In this PHP article, we…

read more

15+ Array of Objects PHP Code Examples | Easy 2023 Tutorial

How to work with an array of objects in PHP code Can you have arrays of objects in PHP Yes, you can definitely create, store, filter, merge and work with an array of objects in PHP. You can decode JSON objects as arrays as well as create array collections of PHP arrays. PHP offers many solutions to work with an array of objects. This article will break down 2 common scenarios when working with arrays of objects in PHP. Visitors come to us many times to ask questions about working with PHP classes and objects as well as JSON objects. Both situations are common but do have unique requirements. Let's first get into PHP classes and objects. Then we will follow it up with Working with PHP Classes and Objects PHP was originally designed as a purely procedural language. Starting in PHP 4 and greatly extending into PHP 5, it…

read more

Convert JSON to XML Correctly with PHP Code Examples (2023)

JSON to XML in PHP
Converting JSON to XML in PHP Code Example <?php $JSON_arr = json_decode($JSON, true); $xml = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?><root></root>'); function arraytoXML($json_arr, &$xml) { foreach($json_arr as $key => $value) { if(is_int($key)) { $key = 'Element'.$key; //To avoid numeric tags like <0></0> } if(is_array($value)) { $label = $xml->addChild($key); arrayToXml($value, $label); //Adds nested elements. } else { $xml->addChild($key, $value); } } } arraytoXML($JSON_arr, $xml); print_r($xml->asXML('output.xml')); ?> Can't understand what's going on? Don't worry, just stay tuned as this article will explain every bit in depth.  Background: JSON, XML, SimpleXMLElement Class JSON vs XML JSON and XML have some similarities and differences in structure and function. Both are popular as data exchange formats in a client-server setup. That's why applications that deal with both formats often need interconversion functionality between JSON and XML. Follow the link to learn more about JSON vs XML. This article focuses on PHP JSON to XML conversion.  PHP SimpleXMLElement…

read more

Echo PHP Array: 10 Code Examples to Print Keys & Values (2023)

How to echo an array in PHP
How to echo an array in PHP There are multiple ways to echo arrays in PHP. Use a foreach loop, implode, print_r, or var_dump functions to display an array's keys & values. I recommend using either var_dump or print_r if you are debugging whereas loops are useful when you need to access the data. This article explains how to echo an array in PHP and includes a section about how we can print out a PHP array. Stick to the article and you will have better insights by the end of this article. Let’s start without any further ado. Echo PHP Arrays with Value PHP Code Examples <?php $fruit = ["apple" => 2, "banana" => 3, "orange" => 7]; // Print an array using var_dump: var_dump($fruit); // Print an array using print_r: print_r($fruit); // Echo array keys using a foreach loop. echo "fruits: "; $i = 1; foreach ($fruit as…

read more