Magento 2 - Email attachment is not working in my controlerGetting an issue working with Grunt in Magento 2Magento 2: send email with AttachmentEmail attachmentmagento send email with PDF attachmentMagento 2: How to override newsletter Subscriber modelAdd Attachment In Magento Email ModelMagento2 create downloadable product into an iterationUpdate Product programmatically - Invalid method MagentoEavModelEntityAttribute::isScopeGlobalMagento 2.3 custom email attachment not workingMagento 2.3 email attachment not working while sending custom email

Players of unusual orchestral instruments

Why doesn't the Lars family (and thus Luke) speak Huttese as their first language?

Why does Hellboy file down his horns?

Professor falsely accusing me of cheating in a class he does not teach, two months after end of the class. What precautions should I take?

Hacker Rank : Electronics Shop

does ability to impeach an expert witness on science or scholarship go too far?

What's the point of this scene involving Flash Thompson at the airport?

How can I legally visit the United States Minor Outlying Islands in the Pacific?

Rearranging the formula

Modeling, view and projection transformation using vector and point in homogenous form

What is the English equivalent of 干物女 (dried fish woman)?

What does `[$'rn']` mean?

I quit, and boss offered me 3 month "grace period" where I could still come back

A DVR algebra with weird automorphisms

How can I deal with a player trying to insert real-world mythology into my homebrew setting?

School House Points (Python + SQLite)

Measuring mystery distances

The monorail explodes before I can get on it

Why did my rum cake turn black?

When is pointing out a person's hypocrisy not considered to be a logical fallacy?

To accent or not to accent in Greek

Back to the nineties!

About the number of real roots

Can I intentionally omit previous work experience or pretend it doesn't exist when applying for jobs?



Magento 2 - Email attachment is not working in my controler


Getting an issue working with Grunt in Magento 2Magento 2: send email with AttachmentEmail attachmentmagento send email with PDF attachmentMagento 2: How to override newsletter Subscriber modelAdd Attachment In Magento Email ModelMagento2 create downloadable product into an iterationUpdate Product programmatically - Invalid method MagentoEavModelEntityAttribute::isScopeGlobalMagento 2.3 custom email attachment not workingMagento 2.3 email attachment not working while sending custom email






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








2















Here is my code.



$pdfFile = 'pub/pdf/13_rajeshnagappan2016@gmail.com.pdf';
$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('rajeshnagappan2016@gmail.com', 'Customer Name');
// ->addAttachment(file_get_contents($pdfFile));

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










share|improve this question
























  • which error you are facing into this code?

    – Dhiren Vasoya
    Jul 5 at 14:34











  • No errors showing. When I command attachment line email working.

    – Rajesh Nagappan
    Jul 5 at 14:51











  • Uncaught Error: Call to undefined method MagentoFrameworkMailMessageInterceptor::createAttachment()

    – Rajesh Nagappan
    Jul 5 at 15:23











  • did you referred this link ? webkul.com/blog/attach-pdf-file-magento-2-email

    – Mohit Rane
    Jul 6 at 17:12

















2















Here is my code.



$pdfFile = 'pub/pdf/13_rajeshnagappan2016@gmail.com.pdf';
$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('rajeshnagappan2016@gmail.com', 'Customer Name');
// ->addAttachment(file_get_contents($pdfFile));

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










share|improve this question
























  • which error you are facing into this code?

    – Dhiren Vasoya
    Jul 5 at 14:34











  • No errors showing. When I command attachment line email working.

    – Rajesh Nagappan
    Jul 5 at 14:51











  • Uncaught Error: Call to undefined method MagentoFrameworkMailMessageInterceptor::createAttachment()

    – Rajesh Nagappan
    Jul 5 at 15:23











  • did you referred this link ? webkul.com/blog/attach-pdf-file-magento-2-email

    – Mohit Rane
    Jul 6 at 17:12













2












2








2








Here is my code.



$pdfFile = 'pub/pdf/13_rajeshnagappan2016@gmail.com.pdf';
$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('rajeshnagappan2016@gmail.com', 'Customer Name');
// ->addAttachment(file_get_contents($pdfFile));

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










share|improve this question
















Here is my code.



$pdfFile = 'pub/pdf/13_rajeshnagappan2016@gmail.com.pdf';
$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('rajeshnagappan2016@gmail.com', 'Customer Name');
// ->addAttachment(file_get_contents($pdfFile));

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







magento2 email controllers attachment






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jul 5 at 15:28









MSA

1,3663 silver badges17 bronze badges




1,3663 silver badges17 bronze badges










asked Jul 5 at 14:33









Rajesh NagappanRajesh Nagappan

913 silver badges13 bronze badges




913 silver badges13 bronze badges












  • which error you are facing into this code?

    – Dhiren Vasoya
    Jul 5 at 14:34











  • No errors showing. When I command attachment line email working.

    – Rajesh Nagappan
    Jul 5 at 14:51











  • Uncaught Error: Call to undefined method MagentoFrameworkMailMessageInterceptor::createAttachment()

    – Rajesh Nagappan
    Jul 5 at 15:23











  • did you referred this link ? webkul.com/blog/attach-pdf-file-magento-2-email

    – Mohit Rane
    Jul 6 at 17:12

















  • which error you are facing into this code?

    – Dhiren Vasoya
    Jul 5 at 14:34











  • No errors showing. When I command attachment line email working.

    – Rajesh Nagappan
    Jul 5 at 14:51











  • Uncaught Error: Call to undefined method MagentoFrameworkMailMessageInterceptor::createAttachment()

    – Rajesh Nagappan
    Jul 5 at 15:23











  • did you referred this link ? webkul.com/blog/attach-pdf-file-magento-2-email

    – Mohit Rane
    Jul 6 at 17:12
















which error you are facing into this code?

– Dhiren Vasoya
Jul 5 at 14:34





which error you are facing into this code?

– Dhiren Vasoya
Jul 5 at 14:34













No errors showing. When I command attachment line email working.

– Rajesh Nagappan
Jul 5 at 14:51





No errors showing. When I command attachment line email working.

– Rajesh Nagappan
Jul 5 at 14:51













Uncaught Error: Call to undefined method MagentoFrameworkMailMessageInterceptor::createAttachment()

– Rajesh Nagappan
Jul 5 at 15:23





Uncaught Error: Call to undefined method MagentoFrameworkMailMessageInterceptor::createAttachment()

– Rajesh Nagappan
Jul 5 at 15:23













did you referred this link ? webkul.com/blog/attach-pdf-file-magento-2-email

– Mohit Rane
Jul 6 at 17:12





did you referred this link ? webkul.com/blog/attach-pdf-file-magento-2-email

– Mohit Rane
Jul 6 at 17:12










2 Answers
2






active

oldest

votes


















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






share|improve this answer

























  • {"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",

    – Rajesh Nagappan
    Jul 5 at 16:08


















0














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="MagentoFrameworkMailMessage" type="OfferEnquiryModelMessage" /></config>


Model/Mail/Message.php



<?php 
namespace OfferEnquiryModel;

class Message extends MagentoFrameworkMailMessage

protected $_parts = [];

public function setBodyText($content)
$textPart = new ZendMimePart();
$textPart->setContent($content)
->setType(ZendMimeMime::TYPE_TEXT)
->setCharset('utf-8')
;
$this->_parts[] = $textPart;
$this->setPartsToBody();
return $this;


public function setBodyHtml($content)
$htmlPart = new ZendMimePart();
$htmlPart->setContent($content)
->setType(ZendMimeMime::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 = ZendMimeMime::DISPOSITION_ATTACHMENT;
if($encoding === null) $encoding = ZendMimeMime::ENCODING_BASE64;
if($fileName === null) throw new Exception("Param 'filename' can not be null");
$attachmentPart = new ZendMimePart();
$attachmentPart
->setContent($content)
->setType($fileType)
->setDisposition($disposition)
->setEncoding($encoding)
->setFileName($fileName)
;
$this->_parts[] = $attachmentPart;
$this->setPartsToBody();
return $this;


public function setPartsToBody()
$mimeMessage = new ZendMimeMessage();
$mimeMessage->setParts($this->_parts);
$this->setBody($mimeMessage);
return $this;




Model/Mail/TransportBuilder.php



<?php
namespace OfferEnquiryModelMail;
class TransportBuilder extends MagentoFrameworkMailTemplateTransportBuilder

/**
* @param ApiAttachmentInterface $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' => 'rajeshnagappan2017@gmail.com',
'name' => 'Test'
]
)->addTo(
'rajeshnagappan2016@gmail.com', 'Receiver'
)->addAttachment(file_get_contents($pdfFile))->getTransport();
$transport->sendMessage();
$this->inlineTranslation->resume();
$this->sendMail();





share|improve this answer

























    Your Answer








    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "479"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f280992%2fmagento-2-email-attachment-is-not-working-in-my-controler%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    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






    share|improve this answer

























    • {"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",

      – Rajesh Nagappan
      Jul 5 at 16:08















    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






    share|improve this answer

























    • {"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",

      – Rajesh Nagappan
      Jul 5 at 16:08













    2












    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






    share|improve this answer















    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







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Jul 5 at 17:12









    MSA

    1,3663 silver badges17 bronze badges




    1,3663 silver badges17 bronze badges










    answered Jul 5 at 15:13









    GattekaGatteka

    211 bronze badge




    211 bronze badge












    • {"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",

      – Rajesh Nagappan
      Jul 5 at 16:08

















    • {"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",

      – Rajesh Nagappan
      Jul 5 at 16:08
















    {"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",

    – Rajesh Nagappan
    Jul 5 at 16:08





    {"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",

    – Rajesh Nagappan
    Jul 5 at 16:08













    0














    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="MagentoFrameworkMailMessage" type="OfferEnquiryModelMessage" /></config>


    Model/Mail/Message.php



    <?php 
    namespace OfferEnquiryModel;

    class Message extends MagentoFrameworkMailMessage

    protected $_parts = [];

    public function setBodyText($content)
    $textPart = new ZendMimePart();
    $textPart->setContent($content)
    ->setType(ZendMimeMime::TYPE_TEXT)
    ->setCharset('utf-8')
    ;
    $this->_parts[] = $textPart;
    $this->setPartsToBody();
    return $this;


    public function setBodyHtml($content)
    $htmlPart = new ZendMimePart();
    $htmlPart->setContent($content)
    ->setType(ZendMimeMime::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 = ZendMimeMime::DISPOSITION_ATTACHMENT;
    if($encoding === null) $encoding = ZendMimeMime::ENCODING_BASE64;
    if($fileName === null) throw new Exception("Param 'filename' can not be null");
    $attachmentPart = new ZendMimePart();
    $attachmentPart
    ->setContent($content)
    ->setType($fileType)
    ->setDisposition($disposition)
    ->setEncoding($encoding)
    ->setFileName($fileName)
    ;
    $this->_parts[] = $attachmentPart;
    $this->setPartsToBody();
    return $this;


    public function setPartsToBody()
    $mimeMessage = new ZendMimeMessage();
    $mimeMessage->setParts($this->_parts);
    $this->setBody($mimeMessage);
    return $this;




    Model/Mail/TransportBuilder.php



    <?php
    namespace OfferEnquiryModelMail;
    class TransportBuilder extends MagentoFrameworkMailTemplateTransportBuilder

    /**
    * @param ApiAttachmentInterface $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' => 'rajeshnagappan2017@gmail.com',
    'name' => 'Test'
    ]
    )->addTo(
    'rajeshnagappan2016@gmail.com', 'Receiver'
    )->addAttachment(file_get_contents($pdfFile))->getTransport();
    $transport->sendMessage();
    $this->inlineTranslation->resume();
    $this->sendMail();





    share|improve this answer



























      0














      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="MagentoFrameworkMailMessage" type="OfferEnquiryModelMessage" /></config>


      Model/Mail/Message.php



      <?php 
      namespace OfferEnquiryModel;

      class Message extends MagentoFrameworkMailMessage

      protected $_parts = [];

      public function setBodyText($content)
      $textPart = new ZendMimePart();
      $textPart->setContent($content)
      ->setType(ZendMimeMime::TYPE_TEXT)
      ->setCharset('utf-8')
      ;
      $this->_parts[] = $textPart;
      $this->setPartsToBody();
      return $this;


      public function setBodyHtml($content)
      $htmlPart = new ZendMimePart();
      $htmlPart->setContent($content)
      ->setType(ZendMimeMime::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 = ZendMimeMime::DISPOSITION_ATTACHMENT;
      if($encoding === null) $encoding = ZendMimeMime::ENCODING_BASE64;
      if($fileName === null) throw new Exception("Param 'filename' can not be null");
      $attachmentPart = new ZendMimePart();
      $attachmentPart
      ->setContent($content)
      ->setType($fileType)
      ->setDisposition($disposition)
      ->setEncoding($encoding)
      ->setFileName($fileName)
      ;
      $this->_parts[] = $attachmentPart;
      $this->setPartsToBody();
      return $this;


      public function setPartsToBody()
      $mimeMessage = new ZendMimeMessage();
      $mimeMessage->setParts($this->_parts);
      $this->setBody($mimeMessage);
      return $this;




      Model/Mail/TransportBuilder.php



      <?php
      namespace OfferEnquiryModelMail;
      class TransportBuilder extends MagentoFrameworkMailTemplateTransportBuilder

      /**
      * @param ApiAttachmentInterface $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' => 'rajeshnagappan2017@gmail.com',
      'name' => 'Test'
      ]
      )->addTo(
      'rajeshnagappan2016@gmail.com', 'Receiver'
      )->addAttachment(file_get_contents($pdfFile))->getTransport();
      $transport->sendMessage();
      $this->inlineTranslation->resume();
      $this->sendMail();





      share|improve this answer

























        0












        0








        0







        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="MagentoFrameworkMailMessage" type="OfferEnquiryModelMessage" /></config>


        Model/Mail/Message.php



        <?php 
        namespace OfferEnquiryModel;

        class Message extends MagentoFrameworkMailMessage

        protected $_parts = [];

        public function setBodyText($content)
        $textPart = new ZendMimePart();
        $textPart->setContent($content)
        ->setType(ZendMimeMime::TYPE_TEXT)
        ->setCharset('utf-8')
        ;
        $this->_parts[] = $textPart;
        $this->setPartsToBody();
        return $this;


        public function setBodyHtml($content)
        $htmlPart = new ZendMimePart();
        $htmlPart->setContent($content)
        ->setType(ZendMimeMime::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 = ZendMimeMime::DISPOSITION_ATTACHMENT;
        if($encoding === null) $encoding = ZendMimeMime::ENCODING_BASE64;
        if($fileName === null) throw new Exception("Param 'filename' can not be null");
        $attachmentPart = new ZendMimePart();
        $attachmentPart
        ->setContent($content)
        ->setType($fileType)
        ->setDisposition($disposition)
        ->setEncoding($encoding)
        ->setFileName($fileName)
        ;
        $this->_parts[] = $attachmentPart;
        $this->setPartsToBody();
        return $this;


        public function setPartsToBody()
        $mimeMessage = new ZendMimeMessage();
        $mimeMessage->setParts($this->_parts);
        $this->setBody($mimeMessage);
        return $this;




        Model/Mail/TransportBuilder.php



        <?php
        namespace OfferEnquiryModelMail;
        class TransportBuilder extends MagentoFrameworkMailTemplateTransportBuilder

        /**
        * @param ApiAttachmentInterface $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' => 'rajeshnagappan2017@gmail.com',
        'name' => 'Test'
        ]
        )->addTo(
        'rajeshnagappan2016@gmail.com', 'Receiver'
        )->addAttachment(file_get_contents($pdfFile))->getTransport();
        $transport->sendMessage();
        $this->inlineTranslation->resume();
        $this->sendMail();





        share|improve this answer













        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="MagentoFrameworkMailMessage" type="OfferEnquiryModelMessage" /></config>


        Model/Mail/Message.php



        <?php 
        namespace OfferEnquiryModel;

        class Message extends MagentoFrameworkMailMessage

        protected $_parts = [];

        public function setBodyText($content)
        $textPart = new ZendMimePart();
        $textPart->setContent($content)
        ->setType(ZendMimeMime::TYPE_TEXT)
        ->setCharset('utf-8')
        ;
        $this->_parts[] = $textPart;
        $this->setPartsToBody();
        return $this;


        public function setBodyHtml($content)
        $htmlPart = new ZendMimePart();
        $htmlPart->setContent($content)
        ->setType(ZendMimeMime::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 = ZendMimeMime::DISPOSITION_ATTACHMENT;
        if($encoding === null) $encoding = ZendMimeMime::ENCODING_BASE64;
        if($fileName === null) throw new Exception("Param 'filename' can not be null");
        $attachmentPart = new ZendMimePart();
        $attachmentPart
        ->setContent($content)
        ->setType($fileType)
        ->setDisposition($disposition)
        ->setEncoding($encoding)
        ->setFileName($fileName)
        ;
        $this->_parts[] = $attachmentPart;
        $this->setPartsToBody();
        return $this;


        public function setPartsToBody()
        $mimeMessage = new ZendMimeMessage();
        $mimeMessage->setParts($this->_parts);
        $this->setBody($mimeMessage);
        return $this;




        Model/Mail/TransportBuilder.php



        <?php
        namespace OfferEnquiryModelMail;
        class TransportBuilder extends MagentoFrameworkMailTemplateTransportBuilder

        /**
        * @param ApiAttachmentInterface $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' => 'rajeshnagappan2017@gmail.com',
        'name' => 'Test'
        ]
        )->addTo(
        'rajeshnagappan2016@gmail.com', 'Receiver'
        )->addAttachment(file_get_contents($pdfFile))->getTransport();
        $transport->sendMessage();
        $this->inlineTranslation->resume();
        $this->sendMail();






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jul 7 at 8:40









        Rajesh NagappanRajesh Nagappan

        913 silver badges13 bronze badges




        913 silver badges13 bronze badges



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Magento Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f280992%2fmagento-2-email-attachment-is-not-working-in-my-controler%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Get product attribute by attribute group code in magento 2get product attribute by product attribute group in magento 2Magento 2 Log Bundle Product Data in List Page?How to get all product attribute of a attribute group of Default attribute set?Magento 2.1 Create a filter in the product grid by new attributeMagento 2 : Get Product Attribute values By GroupMagento 2 How to get all existing values for one attributeMagento 2 get custom attribute of a single product inside a pluginMagento 2.3 How to get all the Multi Source Inventory (MSI) locations collection in custom module?Magento2: how to develop rest API to get new productsGet product attribute by attribute group code ( [attribute_group_code] ) in magento 2

            Category:9 (number) SubcategoriesMedia in category "9 (number)"Navigation menuUpload mediaGND ID: 4485639-8Library of Congress authority ID: sh85091979ReasonatorScholiaStatistics

            Magento 2.3: How do i solve this, Not registered handle, on custom form?How can i rewrite TierPrice Block in Magento2magento 2 captcha not rendering if I override layout xmlmain.CRITICAL: Plugin class doesn't existMagento 2 : Problem while adding custom button order view page?Magento 2.2.5: Overriding Admin Controller sales/orderMagento 2.2.5: Add, Update and Delete existing products Custom OptionsMagento 2.3 : File Upload issue in UI Component FormMagento2 Not registered handleHow to configured Form Builder Js in my custom magento 2.3.0 module?Magento 2.3. How to create image upload field in an admin form