PHP code example of weprovide / magento2-module-mailattachment

1. Go to this page and download the library: Download weprovide/magento2-module-mailattachment 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/ */

    

weprovide / magento2-module-mailattachment example snippets



namespace YourNameSpace\YourModule\Controller\Email;

use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Magento\Framework\App\Area;
use Magento\Framework\Controller\ResultFactory;
use Magento\Store\Model\Store;
use WeProvide\MailAttachment\Mail\Template\TransportBuilder;
use Zend_Mime;

class Index extends Action
{
    protected $transportBuilder;

    public function __construct(
        Context $context,
        TransportBuilder $transportBuilder
    ) {
        $this->transportBuilder  = $transportBuilder;
        parent::__construct($context);
    }

    /**
     * Execute view action
     * @return \Magento\Framework\Controller\ResultInterface
     */
    public function execute()
    {
        // Any buffer
        $content = '';

        $transport = $this->transportBuilder->setTemplateIdentifier('example_identifier')
                                            ->setTemplateOptions([
                                                'area'  => Area::AREA_FRONTEND,
                                                'store' => Store::DEFAULT_STORE_ID,
                                            ])
                                            ->setFrom([
                                                'name'  => 'Example name',
                                                'email' => '[email protected]',
                                            ])
                                            ->addTo('[email protected]')
                                            ->addAttachment($content, 'document.pdf', 'application/pdf');
        
        $transport = $transport->getTransport();
        $transport->sendMessage();

        return $this->resultFactory->create(ResultFactory::TYPE_PAGE);
    }
}