0

I got the below error while using addAttachement function in my cron file.

[2019-09-13 06:31:20] main.CRITICAL: Error when running a cron job {"exception":"[object] (RuntimeException(code: 0): Error when running a cron job at C:\
ew\\xampp\\htdocs\\Project\\vendor\\magento\\module-cron\\Observer\\ProcessCronQueueObserver.php:327, Error(code: 0): Call to undefined method Magento\\Framework\\Mail\\Template\\TransportBuilder\\Interceptor::addAttachment() at C:\
ew\\xampp\\htdocs\\Project\\app\\code\\KiwiCommerce\\AdminActivity\\Cron\\Test.php:62)"} []

I have shared my current code below please check and if possible to help me.

<?php
/**
 * KiwiCommerce
 *
 * Do not edit or add to this file if you wish to upgrade to newer versions in the future.
 * If you wish to customize this module for your needs.
 * Please contact us https://kiwicommerce.co.uk/contacts.
 *
 * @category   KiwiCommerce
 * @package    KiwiCommerce_AdminActivity
 * @copyright  Copyright (C) 2018 Kiwi Commerce Ltd (https://kiwicommerce.co.uk/)
 * @license    https://kiwicommerce.co.uk/magento2-extension-license/
 */
namespace KiwiCommerce\AdminActivity\Cron;

use KiwiCommerce\AdminActivity\Mail\Template\TransportBuilder;
use Magento\Framework\App\Area;
use Magento\Framework\App\State;
use Magento\Framework\Exception\MailException;

/**
 * Class ClearLog
 * @package KiwiCommerce\AdminActivity\Cron
 */
class Test extends \Magento\Framework\App\Action\Action
{

    const TEST_TEMPLATE_IDENTIFIER = 'modulename_test_template';
    const TEST_FROM_EMAIL = 'general';
    const TEST_TO_EMAIL = 'akhil.mokariya@fghgh.com';
    const TEST_FILE_CONTENT = 'Test file content.';
    const TEST_FILE_NAME = 'test';
    const TEST_FILE_TYPE = 'txt';
    /**
     * @var \Magento\Framework\Mail\Template\TransportBuilder
     */
    protected $_transportBuilder;

    protected $state;

    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        \Magento\Framework\Mail\Template\TransportBuilder $transportBuilder, 
        State $state
    )
    {
        $this->_transportBuilder = $transportBuilder;
        $this->state = $state;
        parent::__construct($context);
    }

    public function execute()
    {
        try {
            // $this->state->setAreaCode(Area::AREA_FRONTEND);

            $this->_transportBuilder
                ->setTemplateIdentifier(self::TEST_TEMPLATE_IDENTIFIER)
                ->setTemplateOptions(['area' => Area::AREA_FRONTEND, 'store' => 1])
                ->setTemplateVars([])
                ->addAttachment(self::TEST_FILE_CONTENT, self::TEST_FILE_NAME, self::TEST_FILE_TYPE)
                ->setFrom(self::TEST_FROM_EMAIL)
                ->addTo(self::TEST_TO_EMAIL)
                ->getTransport()
                ->sendMessage();
            print_r(__('Everything is fine, email has been sent.'));
        } catch (MailException $me) {
            print_r(__('MailException: %1', $me->getMessage()));
        } catch (\Exception $e) {
            print_r(__('Exception: %1', $e->getMessage()));
        }
        return null;
    }
}
0

addAttachment(), is not available anymore on 2.3 you should use Zend\Mime .

You can follow this links for more helpful info and solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.