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;
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
add a comment |
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
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
add a comment |
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
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
magento2 email controllers attachment
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
add a comment |
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
add a comment |
2 Answers
2
active
oldest
votes
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
{"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
add a comment |
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();
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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
{"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
add a comment |
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
{"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
add a comment |
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
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
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
add a comment |
{"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
add a comment |
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();
add a comment |
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();
add a comment |
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();
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();
answered Jul 7 at 8:40
Rajesh NagappanRajesh Nagappan
913 silver badges13 bronze badges
913 silver badges13 bronze badges
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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