Categories: How ToMagento 2

How to Send Magento 2 Shipping Mail to Customers Using Order ID Programmatically

E-commerce is made up of several rich elements, but Email is best effective medium among all to get in touch with customers. Magento 2 comes pre-loaded with a wide range of email templates for each and every activity happened within the store environment. Magento E-commerce store is designed to send all order activity emails to their customers until products get delivered to customers but shipment plays the important role because customers are always willing to know their order shipment status until they get their products hands on.

Being a Magento developer, we always need to play with code & passed through certain requirements. Recently we need to generate shipment mail from Magento 2 environment programmatically for multiple orders. So, we decided to create a script that will help to trigger shipping email just by using order ID. Within a short period of time, we have created this script that we would like to share with you.

To use this script, you need to create a new PHP file using following code and name it as Shipment_mail.php.
Now, copy the script file to root directory of your server and simply hit the URL.

use Magento\Framework\App\Bootstrap;
require __DIR__ . '/app/bootstrap.php';
 
$bootstrap = Bootstrap::create(BP, $_SERVER);
 
$obj = $bootstrap->getObjectManager();
 
$state = $obj->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');
 
 $convertOrder = $objectManager->create('Magento\Sales\Model\Convert\Order');
 $shipment = $convertOrder->toShipment($order);
 
 foreach ($order->getAllItems() AS $orderItem) {
     
     if (! $orderItem->getQtyToShip() || $orderItem->getIsVirtual()) {
          continue;
     }
     $qtyShipped = $orderItem->getQtyToShip();
     
     $shipmentItem = $convertOrder->itemToShipmentItem($orderItem)->setQty($qtyShipped);
        $shipment->addItem($shipmentItem);
   }
      $shipment->register();
      $shipment->getOrder()->setIsInProcess(true);
         try {
             $shipment->save();
                $shipment->getOrder()->save();
                    $objectManager->create('Magento\Shipping\Model\ShipmentNotifier')
                    ->notify($shipment);
 echo "mail success";
                 $shipment->save();
 
         }

This script will automatically send email to customers tied to the account, but you can even use this code to generate shipping more than one time as per your need using the loop.
Hit below stars if this code has helped you or comment down below if you are looking for help regarding this code.
Happy Mailing!

Click to rate this post!
[Total: 8 Average: 3.4]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate 🎖️ Certified Magento Developer👨‍💻. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.🏏

Recent Posts

How to Integrate ChatGPT with Laravel Application?

In this guide, we'll explore how to integrate ChatGPT, an AI-powered chatbot, with a Laravel…

2 days ago

What are Net Sales? How to Calculate Your Net Sales?

In the world of business, understanding financial metrics is crucial for making informed decisions and…

4 days ago

Magento 2 Extensions Digest April 2024 (New Release & Updates)

Welcome to the MageComp Monthly Digest, where we bring you the latest updates, releases, and…

4 days ago

The ABCs of Geofencing: Definition, Features and Uses

In this era, businesses are always on the lookout for ways to engage with their…

5 days ago

How to Delete Product Variant in a Shopify Remix App using GraphQL Mutations?

Managing a Shopify store efficiently involves keeping your product catalog organized. This includes removing outdated…

6 days ago

6 Innovative Tools Revolutionizing E-Commerce Operations

E-commerce has transformed the way consumers shop for products and services and interact with businesses.…

1 week ago