PHP code example of shockwavemk / magento2-module-mail

1. Go to this page and download the library: Download shockwavemk/magento2-module-mail library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

shockwavemk / magento2-module-mail example snippets



    /**
         * @param \Magento\Sales\Model\Order\Email\Sender                          $emailSender
         * @param \Magento\Sales\Model\ResourceModel\EntityAbstract                $entityResource
         * @param \Magento\Sales\Model\ResourceModel\Collection\AbstractCollection $entityCollection
         * @param \Magento\Framework\App\Config\ScopeConfigInterface               $globalConfig
         * @param Config                                                           $documentConfig
         * @param ObjectManagerInterface                                           $objectManager
         */
        public function __construct(
            \Magento\Sales\Model\Order\Email\Sender $emailSender,
            \Magento\Sales\Model\ResourceModel\EntityAbstract $entityResource,
            \Magento\Sales\Model\ResourceModel\Collection\AbstractCollection $entityCollection,
            \Magento\Framework\App\Config\ScopeConfigInterface $globalConfig,
            ObjectManagerInterface $objectManager
        )
        {
            parent::__construct($emailSender, $entityResource, $entityCollection, $globalConfig);

            $this->objectManager = $objectManager;
            $this->documentConfig = $documentConfig;

            // Dynamic typed build a collection for attachments for later usage

            // Should be extended from \Magento\Framework\Data\Collection
            $this->attachmentCollection = $this->objectManager->get(
                'Shockwavemk\Mail\Base\Model\Mail\AttachmentCollectionInterface'
            );
        }



    /**
     * Handles asynchronous email sending
     *
     * @return void
     * @throws \RuntimeException
     * @throws \Exception
     */
    public function sendEmails()
    {
        /** @var \Magento\Sales\Model\AbstractModel $item */
        foreach ($this->entityCollection->getItems() as $item) {

        // add this code

            // Create a new attachment

            if (!is_null($attachmentFilePath)) {
                /** @var \Shockwavemk\Mail\Base\Model\Mail\AttachmentInterface|\Magento\Framework\DataObject $attachment */
                $attachment = $this->objectManager
                    ->create('Shockwavemk\Mail\Base\Model\Mail\AttachmentInterface');

                // Do not transfer binary data to mail entity at this point: The mailer can handle file reading on its own
                $attachment->setFilePath($attachmentFilePath);

                // Add attachment to attachment collection
                // Let the mailer later decide how to handle them
                $this->attachmentCollection->addItem($attachment);
            }

        }

    }