I am trying to send the emails with the attached files but I am not succeeding.

For the moment this is my code:

namespace PHPCuong\CustomerAccount\Helper;
use Magento\Framework\App\Helper\Context;
use Magento\Framework\Mail\Template\TransportBuilder;
use Magento\Framework\App\Helper\AbstractHelper;
use Magento\Store\Model\StoreManagerInterface;
class SendEmail extends AbstractHelper
    protected $transportBuilder;
    protected $storeManager;
    public function __construct(
        Context $context,
        TransportBuilder $transportBuilder,
        StoreManagerInterface $storeManager
        $this->transportBuilder = $transportBuilder;
        $this->storeManager = $storeManager;
    public function sendEmailTransport(){
        return $this->transportBuilder;


And this is the code for sending an email:

                        $helperLogEmail = $this->helper('\PHPCuong\CustomerAccount\Helper\SendEmail');

                        $templateId = 'customer_logo_ord'; // template id
                        $fromEmail = '[email protected]';  // sender Email id
                        $fromName = 'test'; // sender Name
                        $toEmail = "[email protected]";

                        $templateVars = [
                            'product_name' => '',
                            'order_id' => $orderInc
                        $storeId = $_order->getStoreId();
                        $from = ['email' => $fromEmail, 'name' => $fromName];
                        $storeScope = \Magento\Store\Model\ScopeInterface::SCOPE_STORE;
                        $templateOptions = [
                            'area' => \Magento\Framework\App\Area::AREA_FRONTEND,
                            'store' => $storeId
                        $transport = $helperLogEmail->sendEmailTransport()->setTemplateIdentifier($templateId, $storeScope)
                        //$helperLogEmail->sendEmailTransport()->addAttachment($fileName, 'image/jpeg');

Now, how can i do for create and send attachments in my email with transport builder?

Thanks for any help


1 Answer 1


You can use addAttachment to attach files while sending emails.

->addAttachment($filePath, $fileName);

Make sure to give filepath as Relative Paths like /var/www/html/yoursite/yourfile.xyz

  • addAttachment is a function that already exists in transport builder or do I have to create it myself? Thanks
    – Jackom
    Dec 13, 2021 at 14:11
  • 2
    that is already exits. for now add it after ->addCc($fromEmail,'') this one without ; Dec 14, 2021 at 7:08
  • I tried man but return addAttachment() is not defined! How can i do? thanks!
    – Jackom
    Dec 16, 2021 at 18:35
  • where you have added addAttachment() function? can you update your question ? Dec 17, 2021 at 7:34

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.