2

Here is my code.

$pdfFile = 'pub/pdf/[email protected]';
    $store = $this->_storeManager->getStore()->getId();
    $transport = $this->_transportBuilder->setTemplateIdentifier('enquiry_test_template')
        ->setTemplateOptions(['area' => 'frontend', 'store' => $store])
        ->setTemplateVars(
            [
                'store' => $this->_storeManager->getStore(),
            ]
        )
        ->setFrom('general')
        ->addTo('[email protected]', 'Customer Name');
        // ->addAttachment(file_get_contents($pdfFile));

        try {
            $transport = $this->_transportBuilder->getTransport();
            $transport->sendMessage();
            $this->_inlineTranslation->resume();
        } catch (\Exception $e) {
            echo $e->getMessage(); die;
        }
4

2 Answers 2

2

Ensure that you are using the right area code

email_templates.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Email:etc/email_templates.xsd">
    <template id="..." label="This is email template" file="general_template.html" type="html" module="..." area="frontend"/>
</config>

template options

$templateOptions = [
        'area' => Area::AREA_FRONTEND,
               ];

and in that case your controller class should be in the frontend area too

1
  • {"0":"Warning: Declaration of Offer\\Enquiry\\Model\\Mail\\TransportBuilder\\Interceptor::addAttachment($pdfString) should be compatible with Offer\\Enquiry\\Model\\Mail\\TransportBuilder::addAttachment($pdfString, $pdfFile) in E:\\Rajesh\\xampp7.2\\htdocs\\magento\\generated\\code\\Offer\\Enquiry\\Model\\Mail\\TransportBuilder\\Interceptor.php on line 7", Jul 5, 2019 at 16:08
1

Finally I got solution...

etc/di.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="Magento\Framework\Mail\Message" type="Offer\Enquiry\Model\Message" /></config>

Model/Mail/Message.php

<?php 
namespace Offer\Enquiry\Model;

class Message extends \Magento\Framework\Mail\Message {

protected $_parts = [];

public function setBodyText($content) {
    $textPart = new \Zend\Mime\Part();
    $textPart->setContent($content)
        ->setType(\Zend\Mime\Mime::TYPE_TEXT)
        ->setCharset('utf-8')
    ;
    $this->_parts[] = $textPart;
    $this->setPartsToBody();
    return $this;
}

public function setBodyHtml($content) {
    $htmlPart = new \Zend\Mime\Part();
    $htmlPart->setContent($content)
        ->setType(\Zend\Mime\Mime::TYPE_HTML)
        ->setCharset('utf-8')
    ;
    $this->_parts[] = $htmlPart;
    $this->setPartsToBody();
    return $this;
}

public function createAttachment($content, $fileType, $disposition, $encoding, $fileName) {
    if($content === null) throw new \Exception("Param 'content' can not be null");
    if($fileType === null) $fileType = 'application/pdf';
    if($disposition === null) $disposition = \Zend\Mime\Mime::DISPOSITION_ATTACHMENT;
    if($encoding === null) $encoding = \Zend\Mime\Mime::ENCODING_BASE64;
    if($fileName === null) throw new \Exception("Param 'filename' can not be null");
    $attachmentPart = new \Zend\Mime\Part();
    $attachmentPart
        ->setContent($content)
        ->setType($fileType)
        ->setDisposition($disposition)
        ->setEncoding($encoding)
        ->setFileName($fileName)
    ;
    $this->_parts[] = $attachmentPart;
    $this->setPartsToBody();
    return $this;
}

public function setPartsToBody() {
    $mimeMessage = new \Zend\Mime\Message();
    $mimeMessage->setParts($this->_parts);
    $this->setBody($mimeMessage);
    return $this;
}
}

Model/Mail/TransportBuilder.php

<?php
namespace Offer\Enquiry\Model\Mail;
class TransportBuilder extends \Magento\Framework\Mail\Template\TransportBuilder
{
/**
* @param Api\AttachmentInterface $attachment
*/
public function addAttachment($pdfString)
{
    $this->message->createAttachment(
        $pdfString,
        'application/pdf',
        \Zend_Mime::DISPOSITION_ATTACHMENT,
        \Zend_Mime::ENCODING_BASE64,
        'attatched.pdf'
    );
    return $this;
}
        public function clearHeader($headerName)
{
    if (isset($this->_headers[$headerName])){
        unset($this->_headers[$headerName]);
    }
    return $this;
}
}

Controller/Index.php

$pdfFile = 'pub/'.$fileName;
   $this->inlineTranslation->suspend();
   $transport = $this->_transportBuilder->setTemplateIdentifier('enquiry_test_template')->setTemplateOptions(
       [
           'area' => Area::AREA_FRONTEND,
           'store' => $this->_storeManager->getStore()->getId(),
       ]
   )->setTemplateVars(
       [
           'message' => 'this is test with send mail',
       ]
   )->setFrom(
       [
           'email' => '[email protected]',
           'name' => 'Test'
       ]
   )->addTo(
      '[email protected]', 'Receiver'
   )->addAttachment(file_get_contents($pdfFile))->getTransport();
   $transport->sendMessage();
   $this->inlineTranslation->resume();
   $this->sendMail();

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.