Magento – Magento 2.3 email attachment not working while sending custom email

attachmentcustomemailmagento2

https://extait.com/blog/how-to-send-email-with-attachment-in-magento-2-3/

I have tried above link, and below is the code: Now I am receiving emails without attachment.
Full code as below:
Custom\Module\Mail\Template\TransportBuilder.php


<?php
namespace Custom\Module\Mail\Template;

class TransportBuilder extends \Magento\Framework\Mail\Template\TransportBuilder
{
   /**
     * @var \Extait\Attachment\Mail\Message
     */
    protected $message;
    /**
     * Add an attachment to the message.
     *
     * @param string $content
     * @param string $fileName
     * @param string $fileType
     * @return $this
     */
    public function addAttachment($fileName, $fileType)
    {
        $this->message->setBodyAttachment($fileName, $fileType);
        return $this;
    }
    /**
     * After all parts are set, add them to message body.
     *
     * @return $this
     * @throws \Magento\Framework\Exception\LocalizedException
     */
    protected function prepareMessage()
    {
        parent::prepareMessage();
        $this->message->setPartsToBody();
        return $this;
    }
}



File: Vendor\Module\Mail\Message.php

namespace Vendor\Module\Mail;
use Zend\Mime\Mime;
use Zend\Mime\PartFactory;
use Zend\Mail\MessageFactory as MailMessageFactory;
use Zend\Mime\MessageFactory as MimeMessageFactory;
class Message implements \Magento\Framework\Mail\MailMessageInterface
{
    /**
     * @var \Zend\Mime\PartFactory
     */
    protected $partFactory;
    /**
     * @var \Zend\Mime\MessageFactory
     */
    protected $mimeMessageFactory;
    /**
     * @var \Zend\Mail\Message
     */
    private $zendMessage;
    /**
     * @var \Zend\Mime\Part[]
     */
    protected $parts = [];
    /**
     * Message constructor.
     *
     * @param \Zend\Mime\PartFactory $partFactory
     * @param \Zend\Mime\MessageFactory $mimeMessageFactory
     * @param string $charset
     */
    public function __construct(PartFactory $partFactory, MimeMessageFactory $mimeMessageFactory, $charset = 'utf-8')
    {
        $this->partFactory = $partFactory;
        $this->mimeMessageFactory = $mimeMessageFactory;
        $this->zendMessage = MailMessageFactory::getInstance();
        $this->zendMessage->setEncoding($charset);
    }
    /**
     * Add the HTML mime part to the message.
     *
     * @param string $content
     * @return $this
     */
    public function setBodyText($content)
    {
        $textPart = $this->partFactory->create();
        $textPart->setContent($content)
            ->setType(Mime::TYPE_TEXT)
            ->setCharset($this->zendMessage->getEncoding());
        $this->parts[] = $textPart;
        return $this;
    }


   /**
     * Add the text mime part to the message.
     *
     * @param string $content
     * @return $this
     */
    public function setBodyHtml($content)
    {
        $htmlPart = $this->partFactory->create();
        $htmlPart->setContent($content)
            ->setType(Mime::TYPE_HTML)
            ->setCharset($this->zendMessage->getEncoding());
        $this->parts[] = $htmlPart;
        return $this;
    }

    /**
     * Add the attachment mime part to the message.
     *
     * @param string $content
     * @param string $fileName
     * @param string $fileType
     * @return $this
     */


    public function setBodyAttachment($file, $name)
    {
        $attachmentPart = $this->partFactory->create();
        $attachmentPart->setType($file)
            ->setFileName($name);
        $this->parts[] = $attachmentPart;

        return $this;
    }


    /**
     * Set parts to Zend message body.
     *
     * @return $this
     */
    public function setPartsToBody()
    {
        $mimeMessage = $this->mimeMessageFactory->create();
        $mimeMessage->setParts($this->parts);
        $this->zendMessage->setBody($mimeMessage);
        return $this;
    }


    /**
     * {@inheritdoc}
     */
    public function setBody($body)
    {
        return $this;
    }
    /**
     * {@inheritdoc}
     */
    public function setSubject($subject)
    {
        $this->zendMessage->setSubject($subject);
        return $this;
    }
    /**
     * {@inheritdoc}
     */
    public function getSubject()
    {
        return $this->zendMessage->getSubject();
    }
    /**
     * {@inheritdoc}
     */
    public function getBody()
    {
        return $this->zendMessage->getBody();
    }
    /**
     * {@inheritdoc}
     */
    public function setFrom($fromAddress)
    {
        $this->setFromAddress($fromAddress, null);
        return $this;
    }
    /**
     * {@inheritdoc}
     */
    public function setFromAddress($fromAddress, $fromName = null)
    {
        $this->zendMessage->setFrom($fromAddress, $fromName);
        return $this;
    }
    /**
     * {@inheritdoc}
     */
    public function addTo($toAddress)
    {
        $this->zendMessage->addTo($toAddress);
        return $this;
    }
    /**
     * {@inheritdoc}
     */
    public function addCc($ccAddress)
    {
        $this->zendMessage->addCc($ccAddress);
        return $this;
    }
    /**
     * {@inheritdoc}
     */
    public function addBcc($bccAddress)
    {
        $this->zendMessage->addBcc($bccAddress);
        return $this;
    }
    /**
     * {@inheritdoc}
     */
    public function setReplyTo($replyToAddress)
    {
        $this->zendMessage->setReplyTo($replyToAddress);
        return $this;
    }
    /**
     * {@inheritdoc}
     */
    public function getRawMessage()
    {
        return $this->zendMessage->toString();
    }
    /**
     * @inheritDoc
     */
    public function setMessageType($type)
    {
        return $this;
    }
==========================================
File: Vendor\Module\Controller\Index\Sendemail.php 

$yourFolderName = 'careers-resumes/';

        // "my_custom_file" is the HTML input file name
        $yourInputFileName = 'careers-resume';

        try{
            $postValues = $this->getRequest()->getPost();

            $file = $this->getRequest()->getFiles($yourInputFileName);
            $fileName = ($file && array_key_exists('name', $file)) ? $file['name'] : null;

            if ($file && $fileName) {

                $target = $this->mediaDirectory->getAbsolutePath($yourFolderName);        

                /** @var $uploader \Magento\MediaStorage\Model\File\Uploader */
                $uploader = $this->fileUploader->create(['fileId' => $yourInputFileName]);

                // set allowed file extensions
                $uploader->setAllowedExtensions(['doc', 'docx', 'pdf']);

                // allow folder creation
                $uploader->setAllowCreateFolders(true);

                // rename file name if already exists 
                $uploader->setAllowRenameFiles(true);


                $result = $uploader->save($target);

                $filePath = $result['path'].$result['file'];
                $fileName = $result['name'];


                if ($result['file']) {


                    $storeScope = \Magento\Store\Model\ScopeInterface::SCOPE_STORE;

                     $templateOptions = [
                      'area' => \Magento\Framework\App\Area::AREA_FRONTEND,
                      'store' => 1
                    ];

                    $store = $this->_storeManager->getStore()->getId();
                    $transport = $this->_transportBuilder->setTemplateIdentifier(5)
                    ->setTemplateOptions(['area' => 'frontend', 'store' => $store])
                    ->setTemplateVars(
                        [
                            'store' => $this->_storeManager->getStore(),
                        ]
                    )
                    ->setFrom('general')
                    // you can config general email address in Store -> Configuration -> General -> Store Email Addresses
                    ->addTo('customer@email.com', 'Customer Name')
                    ->addAttachment($filePath, $fileName)
                    ->getTransport();
                    $transport->sendMessage();



                    $this->_redirect('careers');
                    $this->messageManager->addSuccess(__('File has been successfully uploaded & mail sent.')); 
                }

            }
        } catch (\Exception $e) {
            $this->messageManager->addError($e->getMessage());
        }

I am receiving email without attachment.

Best Answer

Your overwrite looks good. Problem is where you send email. You maybe missing following part:

$transport->setTemplateVars([])

OR

$template->setVars([]);

[Update]

Replace addAttachment method by following code:

/**
 * @param $body
 * @param $mimeType
 * @param $disposition
 * @param $encoding
 * @param null $filename
 * @return $this
 */
public function addAttachment(
    $body,
    $mimeType    = \Zend_Mime::TYPE_OCTETSTREAM,
    $disposition = \Zend_Mime::DISPOSITION_ATTACHMENT,
    $encoding    = \Zend_Mime::ENCODING_BASE64,
    $filename    = null
) {

    if($disposition == null) {
        $disposition = \Zend_Mime::DISPOSITION_ATTACHMENT;
    }

    if($encoding == null) {
        $encoding = \Zend_Mime::ENCODING_BASE64;
    }

    $this->message->createAttachment($body, $mimeType, $disposition, $encoding, $filename);
    return $this;
}

And call following way:

$this->transportBuilder->addAttachment(file_get_contents($_FILES['attach']['tmp_name']), $_FILES['attach']['type'], null, null, $_FILES['attach']['name']);
Related Topic