0

I am generating order report in csv and saving that file in Var folder. Now i want to send that file on email without using any email template. How to i can send that file on email ? Kindly check below code and make it correct.

Thanks

<?php
namespace Jco\PreOrderReport\Cron;

class ExportOrders
{

/**
 * @var \Magento\Sales\Api\OrderRepositoryInterface
 */
private $orderRepository;

/**
 * @var \Magento\Framework\Api\SearchCriteriaBuilder
 */
private $searchCriteriaBuilder;

/**
 * @var \Magento\Framework\Locale\ResolverInterface
 */
private $localeResolver;

/**
 * @var \Magento\Framework\Stdlib\DateTime\TimezoneInterface
 */
private $localeDate;

/**
 * Filter builder
 *
 * @var \Magento\Framework\Api\FilterBuilder
 */
private $filterBuilder;

/**
 * Directory List
 *
 * @var \Magento\Framework\App\Filesystem\DirectoryList
 */
private $directoryList;



protected $transportBuilder;

/**
 * File CSV
 *
 * @var \Magento\Framework\File\Csv
 */
private $csvProcessor;

public function __construct(
    \Magento\Sales\Api\OrderRepositoryInterface $orderRepository,
    \Magento\Framework\Api\SearchCriteriaBuilder $searchCriteria,
    \Magento\Framework\Locale\ResolverInterface $localeResolver,
    \Magento\Framework\Stdlib\DateTime\TimezoneInterface $timezone,
    \Magento\Framework\Api\FilterBuilder $filterBuilder,
    \Magento\Framework\App\Filesystem\DirectoryList $directoryList,
    \Magento\Framework\Mail\Template\TransportBuilder $transportBuilder,
    \Magento\Framework\File\Csv $csvProcessor

) {
    $this->orderRepository = $orderRepository;
    $this->searchCriteriaBuilder = $searchCriteria;
    $this->localeResolver = $localeResolver;
    $this->localeDate = $timezone;
    $this->filterBuilder = $filterBuilder;
    $this->directoryList = $directoryList;
    $this->csvProcessor = $csvProcessor;
    $this->transportBuilder = $transportBuilder;
}


/**
 * Export orders to csv
 *
 * @return void
 */
public function exportOrdersToCsv() {

    $lastDayOrders = $this->getLastDayOrders();
    //$exportFieldsArray = [];
    $exportFieldsArray[] = [
        'increment_id'          => __('Order ID'),
        'created_at'            => __('Date Ordered'),
        'shipping_fullname'     => __('Customer'),
        'currency_code'         => __('Currency'),
        'grand_total'           => __('Sale Price'),
        'order_status'          => __('Order Status'),
        'remaining_days'        => __('Remaining Days'),
        'expected_delivery'     => __('Due Date'),
        'status_color'  => __('Status Color'),
        
    ];
        
    foreach($lastDayOrders as $order){
            
            $created_at     =  $order->getCreatedAt();          
            $today          = date("Y-m-d"); // current date
            $from           = strtotime('+60 day', strtotime($order->getCreatedAt()));
            $from           = date('Y-m-d', $from); // +60 Days Time format
            
            $now        = strtotime($today);
            $your_date  = strtotime($from); 
            $datediff   = $your_date - $now;
            $numberDays = round($datediff / (60 * 60 * 24));
            
        
        if (($order->getStatusLabel() == 'Processing Pre-Order') && ($numberDays <= 15)){
            
            $shippingAddress = $order->getShippingAddress();
            $billingAddress = $order->getBillingAddress();
            $order_status   = $order->getStatusLabel();
            
            $exportFieldsArray[] = [
                'increment_id'          => $order->getIncrementId(),
                'created_at'            =>  $order->getCreatedAt(),         
                'shipping_fullname'     => $shippingAddress->getFirstname()." ".$shippingAddress->getLastname(),
                'currency_code'         => $order->getOrderCurrencyCode(),            
                'grand_total'           => $order->getGrandTotal(),
                'order_status'          => $order->getStatusLabel(),
                'remaining_days'        => $numberDays,
                'expected_delivery'     => $from,
                'status_color'  => 'Red'
                
            ];
        }
        
    }

    $fileDirectoryPath = $this->directoryList->getPath(\Magento\Framework\App\Filesystem\DirectoryList::VAR_DIR);
    
    $fileName = 'PreOrderReport.csv';
    $filePath =  $fileDirectoryPath . '/' . $fileName;
    
    
    $this->csvProcessor
        ->setEnclosure('"')
        ->setDelimiter(',')     
        ->saveData($filePath, $exportFieldsArray);
        
    
    // Sending csv file to email
    $transport = $this->transportBuilder->setTemplateIdentifier('mail_template')
        ->setTemplateOptions(['area' => 'frontend', 'store' => '1'])
        ->setFrom('[email protected]')
        ->addTo('[email protected]')
        ->addAttachment($filePath, $fileName)
        ->getTransport();
    $transport->sendMessage();
    
}
}
2

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.