4

I put that function on the transpotbuilder file in core file , but it has still error:

Fatal error: Class 'Magento\Framework\Mail\Template\Zend_Mime' not found in /var/www/html/vendor/magento/framework/Mail/Template

 public function addAttachment(
    $body,
    $mimeType    = Zend_Mime::TYPE_OCTETSTREAM,
    $disposition = Zend_Mime::DISPOSITION_ATTACHMENT,
    $encoding    = Zend_Mime::ENCODING_BASE64,
    $filename    = null
) {
    $this->message->createAttachment($body, $mimeType, $disposition, $encoding, $filename);
    return $this;
}

I added this function in core file and try to send attachment email and I got the Above error.

5 Answers 5

3

You need to add prefix slash() before zend_mime.

Clear var/generation folder and check,

 public function addAttachment(
    $body,
    $mimeType    = \Zend_Mime::TYPE_OCTETSTREAM,
    $disposition = \Zend_Mime::DISPOSITION_ATTACHMENT,
    $encoding    = \Zend_Mime::ENCODING_BASE64,
    $filename    = null
) {
    $this->message->createAttachment($body, $mimeType, $disposition, $encoding, $filename);
    return $this;
}

There are not any class related to

'Magento\Framework\Mail\Template\Zend_Mime.php

When you use prefix \ before the class they will route to Mime.php file from path vendor/magento/zendframework1/library/Zend/Mime.php

0
1
    class TransportBuilder extends \Magento\Framework\Mail\Template\TransportBuilder
{

    const TYPE_OCTETSTREAM         = 'application/octet-stream';
    const DISPOSITION_ATTACHMENT   = 'attachment';
    const ENCODING_BASE64          = 'base64';

    public function addAttachment(
        $body,
        $mimeType    = self::TYPE_OCTETSTREAM,
        $disposition = self::DISPOSITION_ATTACHMENT,
        $encoding    = self::ENCODING_BASE64,
        $filename    = null
    ) {
        $this->message->createAttachment($body, $mimeType, $disposition, $encoding, $filename);
        return $this;
    }
}

Use like this then you can resolve this issue.

Reference: vendor/magento/zendframework1/library/Zend/Mime.php

1

Try

composer require zendframework/zend-mime
0
0

For Magento 2.4.7 use

public function addAttachment(
    $fileContent,
    $filename    = null,
    $mimeType    = \Laminas\Mime\Mime::TYPE_OCTETSTREAM,
    $disposition = \Laminas\Mime\Mime::DISPOSITION_ATTACHMENT,
    $encoding    = \Laminas\Mime\Mime::ENCODING_BASE64
) {
    if (!$filename) {
        $filename="attachment";
    }
    
    $this->message->setBodyAttachment($fileContent, $mimeType, $disposition,$encoding,$filename);
    return $this;
}
0

Replace this Class

use Zend\Mime\Mime;
use Zend\Mime\PartFactory;

With this Class

use Laminas\Mime\Mime;
use Laminas\Mime\PartFactory;

So your final code would be:

 public function addAttachment(
    $body,
    $mimeType    = Laminas_Mime::TYPE_OCTETSTREAM,
    $disposition = Laminas_Mime::DISPOSITION_ATTACHMENT,
    $encoding    = Laminas_Mime::ENCODING_BASE64,
    $filename    = null
) {
    $this->message->createAttachment($body, $mimeType, $disposition, $encoding, $filename);
    return $this;
}

This works for me as I have extended the \Magento\Framework\Mail\Template\TransportBuilder class.

Your Answer

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

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