{"id":486858,"date":"2025-11-04T09:41:58","date_gmt":"2025-11-04T09:41:58","guid":{"rendered":"https:\/\/webkul.com\/blog\/?p=486858"},"modified":"2025-11-04T10:05:14","modified_gmt":"2025-11-04T10:05:14","slug":"how-to-send-email-attachment-magento2","status":"publish","type":"post","link":"https:\/\/webkul.com\/blog\/how-to-send-email-attachment-magento2\/","title":{"rendered":"How to add File Attachments in Email in Magento 2"},"content":{"rendered":"\n<div class=\"wk-index-wrap\"><div class=\"block-wrap\">\n<h3 class=\"wp-block-heading index-title\">Email Attachments issue<\/h3>\n<\/div><\/div>\n\n\n\n<p>While learning how to add file attachments to email, we faced an issue for a client in Magento 2.<\/p>\n\n\n\n<p>The attachments failed to load, and after investigation, we found the problem was caused by updates in the <strong>Zend<\/strong> module.<\/p>\n\n\n\n<p>If you\u2019re looking to send order attachments, you can check<span style=\"margin: 0px;padding: 0px\">\u00a0the<a href=\"https:\/\/store.webkul.com\/magento2-order-attachment.html\" target=\"_blank\">\u00a0<\/a><\/span><a href=\"https:\/\/webkul.com\/blog\/magento2-order-attachment\/\">Magento 2 Order Attachment Extension<\/a>.<\/p>\n\n\n\n<div class=\"wk-index-wrap\"><div class=\"block-wrap\">\n<h4 class=\"wp-block-heading index-title\">Resolution<\/h4>\n<\/div><\/div>\n\n\n\n<p>We created the <strong>SendEmailWithAttachment<\/strong> controller using Magento\u2019s core mail transport builder (<strong>Magento\\Framework\\Mail\\Template\\TransportBuilder<\/strong>) to attach files and send emails efficiently.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\">&lt;?php\n\n\/**\n * Webkul Software.\n *\n * @category  Webkul Software Private Limited\n * @package   Webkul_Email\n * @author    Webkul Software Private Limited\n * @copyright Webkul Software Private Limited (https:\/\/webkul.com)\n * @license   https:\/\/store.webkul.com\/license.html\n *\/\nnamespace Webkul\\Email\\Controller;\n\nuse Magento\\Framework\\Filesystem;\nuse Magento\\Framework\\App\\Filesystem\\DirectoryList;\nuse Magento\\Framework\\Mail\\Template\\TransportBuilder;\nuse Magento\\Store\\Model\\StoreManagerInterface;\nuse Magento\\Framework\\App\\Config\\ScopeConfigInterface;\nuse Magento\\Framework\\Filesystem\\Driver\\File;\nuse Magento\\Framework\\File\\Mime;\nuse Laminas\\Mime\\Mime as LaminasMime;\nuse Laminas\\Mime\\Part;\nuse Laminas\\Mime\\Message as MimeMessage;\n\nclass SendEmailWithAttachment extends \\Magento\\Framework\\App\\Action\\Action\n{\n    \/**\n     * @var \\Magento\\Framework\\Translate\\Inline\\StateInterface\n     *\/\n    protected $_inlineTranslation;\n\n    \/**\n     * @var Filesystem\n     *\/\n    private $filesystem;\n\n    \/**\n     * @var TransportBuilder\n     *\/\n    protected $transportBuilder;\n\n    \/**\n     * @var StoreManagerInterface\n     *\/\n    protected $storeManager;\n\n    \/**\n     * @var ScopeConfigInterface\n     *\/\n    protected $scopeConfig;\n\n    \/**\n     * @var File\n     *\/\n    protected $fileDriver;\n\n    \/**\n     * @var Mime\n     *\/\n    protected $mime;\n\n    \/**\n     * Constructor\n     *\n     * @param \\Magento\\Framework\\App\\Action\\Context $context\n     * @param Filesystem $filesystem\n     * @param TransportBuilder $transportBuilder\n     * @param StoreManagerInterface $storeManager\n     * @param ScopeConfigInterface $scopeConfig\n     * @param File $fileDriver\n     * @param Mime $mime\n     * @param \\Magento\\Framework\\Translate\\Inline\\StateInterface $inlineTranslation\n     *\/\n    public function __construct(\n        \\Magento\\Framework\\App\\Action\\Context $context,\n        Filesystem $filesystem,\n        TransportBuilder $transportBuilder,\n        StoreManagerInterface $storeManager,\n        ScopeConfigInterface $scopeConfig,\n        File $fileDriver,\n        Mime $mime,\n        \\Magento\\Framework\\Translate\\Inline\\StateInterface $inlineTranslation\n    ) {\n        parent::__construct($context);\n        $this-&gt;filesystem = $filesystem;\n        $this-&gt;transportBuilder = $transportBuilder;\n        $this-&gt;storeManager = $storeManager;\n        $this-&gt;scopeConfig = $scopeConfig;\n        $this-&gt;fileDriver = $fileDriver;\n        $this-&gt;mime = $mime;\n        $this-&gt;_inlineTranslation = $inlineTranslation;\n    }<\/pre>\n\n\n\n<p>This class is responsible for sending an email with an attached file. We&#8217;ll start by adding a function that attaches a file to the email, following the example provided in the Laminas documentation.<\/p>\n\n\n\n<div class=\"wk-index-wrap\"><div class=\"block-wrap\">\n<h4 class=\"wp-block-heading index-title\">Create File Attachment<\/h4>\n<\/div><\/div>\n\n\n\n<p>Our method below is similar; it will take a file attachment, set its properties, and return a <strong>Laminas\\Mime\\Part<\/strong> class.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\">    \/**\n    * Add an attachment to the message inside the transport builder.\n    *\n    * @param TransportInterface $transport\n    * @param string $filePath\n    *\n    * @return TransportInterface\n    *\/\n\n    protected function addAttachment($transport, $filePath)\n    {\n        $fileMimeType = $this-&gt;mime-&gt;getMimeType($filePath);\n        $fileContent = $this-&gt;fileDriver-&gt;fileGetContents($filePath);\n        $html = $transport-&gt;getMessage()-&gt;getBody()-&gt;generateMessage();\n        $decodedHtml = quoted_printable_decode($html);\n        $message = $transport-&gt;getMessage();\n        if (!$message) {\n            throw new \\Magento\\Framework\\Exception\\LocalizedException(__(&#039;Email message is null.&#039;));\n        }\n\n        $attachment = new Part($fileContent);\n        $attachment-&gt;disposition = LaminasMime::DISPOSITION_ATTACHMENT;\n        $attachment-&gt;encoding = LaminasMime::MULTIPART_RELATED;\n        $attachment-&gt;filename = basename($filePath);\n        $attachment-&gt;type = $fileMimeType;\n\n        $htmlPart = new Part($decodedHtml);\n        $htmlPart-&gt;type = &#039;text\/html&#039;;\n        $htmlPart-&gt;charset = &#039;utf-8&#039;;\n        $htmlPart-&gt;encoding = LaminasMime::ENCODING_QUOTEDPRINTABLE;\n\n        $mimeMessage = new MimeMessage();\n        $mimeMessage-&gt;setParts(&#091;$htmlPart, $attachment]);\n\n        $message-&gt;setBody($mimeMessage);\n\n        return $transport;\n    }<\/pre>\n\n\n\n<div class=\"wk-index-wrap\"><div class=\"block-wrap\">\n<h4 class=\"wp-block-heading index-title\">Add File Attachment to the TransportBuilder<\/h4>\n<\/div><\/div>\n\n\n\n<p>In this code, we\u2019ll attach a file to the transport interface, similar to adding extra content to an email. <\/p>\n\n\n\n<p>The method will accept <strong>Laminas\\Mime\\Part <\/strong>and <strong>Magento\\Framework\\Mail\\TransportInterface<\/strong> as parameters.<\/p>\n\n\n\n<p>So what we have down here is to break the example in the Laminas&nbsp;<a href=\"https:\/\/docs.laminas.dev\/laminas-mail\/message\/attachments\/\">doc<\/a>&nbsp;into two separate parts.<br>The final step is to create a method that will send an email to the recipient while attaching the file.&nbsp;<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\">     \/**\n     * Function to send email to recipient with attachment\n     *\/\n    public function execute()\n    {\n        $directory = $this-&gt;filesystem &gt;getDirectoryWrite(DirectoryList::PUB);\n        $fileName = &#039;xyz.csv&#039;;\n        $filePath = $directory-&gt;getAbsolutePath($fileName);\n        $fileContent = $this-&gt;fileDriver-&gt;fileGetContents($filePath);\n        $fileMimeType = $this-&gt;mime-&gt;getMimeType($filePath);\n        $storeId = $this-&gt;storeManager-&gt;getStore()-&gt;getId();\n\n        $senderEmail = $this-&gt;scopeConfig-&gt;getValue(&#039;trans_email\/ident_support\/email&#039;,\n        \\Magento\\Store\\Model\\ScopeInterface::SCOPE_STORE);\n        $senderName = $this-&gt;scopeConfig-&gt;getValue(&#039;trans_email\/ident_support\/name&#039;,\n        \\Magento\\Store\\Model\\ScopeInterface::SCOPE_STORE);\n\n        $recipientEmail = &#039;Receiver email id&#039;;\n        $subject = &#039;Email subject&#039;;\n        $recipientName = &#039;Receiver name&#039;;\n        $bodyText = &#039;Body Content&#039;;\n        $bodyText = nl2br(htmlspecialchars_decode($bodyText, ENT_QUOTES));\n        $templateVariables = &#091;\n            &quot;message_email_subject&quot; =&gt; $subject,\n            &quot;message_email_content&quot; =&gt;  $bodyText,\n            &quot;recipient_name&quot; =&gt;  $recipientName,\n        ];\n\n        \/\/ Build email\n        $this-&gt;_inlineTranslation-&gt;suspend();\n        $transport = $this-&gt;transportBuilder-&gt;setTemplateIdentifier(&#039;your_template_name&#039;)\n            -&gt;setTemplateOptions(&#091;\n                &#039;area&#039; =&gt; \\Magento\\Framework\\App\\Area::AREA_FRONTEND,\n                &#039;store&#039; =&gt; $storeId,\n            ])\n            -&gt;setTemplateVars($templateVariables)\n            -&gt;setFrom(&#091;&#039;email&#039; =&gt; $senderEmail, &#039;name&#039; =&gt; $senderName])\n            -&gt;addTo($recipientEmail)\n            -&gt;getTransport();\n        if ($transport &amp;&amp; $transport-&gt;getMessage()) {\n            $transport = $this-&gt;addAttachment($transport, $filePath);\n            $transport-&gt;sendMessage();\n        }\n        $this-&gt;_inlineTranslation-&gt;resume();\n    }<\/pre>\n\n\n\n<p>Here is the full code of the Controller <strong>SendEmailWithAttachment<\/strong> to add File Attachments to emails in Magento 2.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\">&lt;?php\n\n\/**\n * Webkul Software.\n *\n * @category  Webkul Software Private Limited\n * @package   Webkul_Email\n * @author    Webkul Software Private Limited\n * @copyright Webkul Software Private Limited (https:\/\/webkul.com)\n * @license   https:\/\/store.webkul.com\/license.html\n *\/\nnamespace Webkul\\Email\\Controller;\n\nuse Magento\\Framework\\Filesystem;\nuse Magento\\Framework\\App\\Filesystem\\DirectoryList;\nuse Magento\\Framework\\Mail\\Template\\TransportBuilder;\nuse Magento\\Store\\Model\\StoreManagerInterface;\nuse Magento\\Framework\\App\\Config\\ScopeConfigInterface;\nuse Magento\\Framework\\Filesystem\\Driver\\File;\nuse Magento\\Framework\\File\\Mime;\nuse Laminas\\Mime\\Mime as LaminasMime;\nuse Laminas\\Mime\\Part;\nuse Laminas\\Mime\\Message as MimeMessage;\n\nclass SendEmailWithAttachment extends \\Magento\\Framework\\App\\Action\\Action\n{\n    \/**\n     * @var \\Magento\\Framework\\Translate\\Inline\\StateInterface\n     *\/\n    protected $_inlineTranslation;\n\n    \/**\n     * @var Filesystem\n     *\/\n    private $filesystem;\n\n    \/**\n     * @var TransportBuilder\n     *\/\n    protected $transportBuilder;\n\n    \/**\n     * @var StoreManagerInterface\n     *\/\n    protected $storeManager;\n\n    \/**\n     * @var ScopeConfigInterface\n     *\/\n    protected $scopeConfig;\n\n    \/**\n     * @var File\n     *\/\n    protected $fileDriver;\n\n    \/**\n     * @var Mime\n     *\/\n    protected $mime;\n\n    \/**\n     * Constructor\n     *\n     * @param \\Magento\\Framework\\App\\Action\\Context $context\n     * @param Filesystem $filesystem\n     * @param TransportBuilder $transportBuilder\n     * @param StoreManagerInterface $storeManager\n     * @param ScopeConfigInterface $scopeConfig\n     * @param File $fileDriver\n     * @param Mime $mime\n     * @param \\Magento\\Framework\\Translate\\Inline\\StateInterface $inlineTranslation\n     *\/\n    public function __construct(\n        \\Magento\\Framework\\App\\Action\\Context $context,\n        Filesystem $filesystem,\n        TransportBuilder $transportBuilder,\n        StoreManagerInterface $storeManager,\n        ScopeConfigInterface $scopeConfig,\n        File $fileDriver,\n        Mime $mime,\n        \\Magento\\Framework\\Translate\\Inline\\StateInterface $inlineTranslation\n    ) {\n        parent::__construct($context);\n        $this-&gt;filesystem = $filesystem;\n        $this-&gt;transportBuilder = $transportBuilder;\n        $this-&gt;storeManager = $storeManager;\n        $this-&gt;scopeConfig = $scopeConfig;\n        $this-&gt;fileDriver = $fileDriver;\n        $this-&gt;mime = $mime;\n        $this-&gt;_inlineTranslation = $inlineTranslation;\n    }\n\n    \/**\n     * Function to send email to recipient with attachment\n     *\/\n    public function execute()\n    {\n        $varDirectory = $this-&gt;filesystem-&gt;getDirectoryWrite(DirectoryList::PUB);\n        $fileName = &#039;xyz.csv&#039;;\n        $filePath = $varDirectory-&gt;getAbsolutePath($fileName);\n        $fileContent = $this-&gt;fileDriver-&gt;fileGetContents($filePath);\n        $fileMimeType = $this-&gt;mime-&gt;getMimeType($filePath);\n        $storeId = $this-&gt;storeManager-&gt;getStore()-&gt;getId();\n\n        $senderEmail = $this-&gt;scopeConfig-&gt;getValue(&#039;trans_email\/ident_support\/email&#039;,\n        \\Magento\\Store\\Model\\ScopeInterface::SCOPE_STORE);\n        $senderName = $this-&gt;scopeConfig-&gt;getValue(&#039;trans_email\/ident_support\/name&#039;,\n        \\Magento\\Store\\Model\\ScopeInterface::SCOPE_STORE);\n\n        $recipientEmail = &#039;Receiver email id&#039;;\n        $subject = &#039;Email subject&#039;;\n        $recipientName = &#039;Receiver name&#039;;\n        $bodyText = &#039;Body Content&#039;;\n        $bodyText = nl2br(htmlspecialchars_decode($bodyText, ENT_QUOTES));\n        $templateVariables = &#091;\n            &quot;message_email_subject&quot; =&gt; $subject,\n            &quot;message_email_content&quot; =&gt;  $bodyText,\n            &quot;recipient_name&quot; =&gt;  $recipientName,\n        ];\n\n        \/\/ Build emailImage\n        $this-&gt;_inlineTranslation-&gt;suspend();\n        $transport = $this-&gt;transportBuilder-&gt;setTemplateIdentifier(&#039;your_template_name&#039;)\n            -&gt;setTemplateOptions(&#091;\n                &#039;area&#039; =&gt; \\Magento\\Framework\\App\\Area::AREA_FRONTEND,\n                &#039;store&#039; =&gt; $storeId,\n            ])\n            -&gt;setTemplateVars($templateVariables)\n            -&gt;setFrom(&#091;&#039;email&#039; =&gt; $senderEmail, &#039;name&#039; =&gt; $senderName])\n            -&gt;addTo($recipientEmail)\n            -&gt;getTransport();\n        if ($transport &amp;&amp; $transport-&gt;getMessage()) {\n            $transport = $this-&gt;addAttachment($transport, $filePath);\n            $transport-&gt;sendMessage();\n        }\n        $this-&gt;_inlineTranslation-&gt;resume();\n    }\n\n    \/**\n    * Add an attachment to the message inside the transport builder.\n    *\n    * @param TransportInterface $transport\n    * @param array $file\n    *\n    * @return TransportInterface\n    *\/\n\n    protected function addAttachment($transport, $filePath)\n    {\n        $fileMimeType = $this-&gt;mime-&gt;getMimeType($filePath);\n        $fileContent = $this-&gt;fileDriver-&gt;fileGetContents($filePath);\n        $html = $transport-&gt;getMessage()-&gt;getBody()-&gt;generateMessage();\n        $decodedHtml = quoted_printable_decode($html);\n        $message = $transport-&gt;getMessage();\n        if (!$message) {\n            throw new \\Magento\\Framework\\Exception\\LocalizedException(__(&#039;Email message is null.&#039;));\n        }\n\n        $attachment = new Part($fileContent);\n        $attachment-&gt;disposition = LaminasMime::DISPOSITION_ATTACHMENT;\n        $attachment-&gt;encoding = LaminasMime::MULTIPART_RELATED;\n        $attachment-&gt;filename = basename($filePath);\n        $attachment-&gt;type = $fileMimeType;\n\n        $htmlPart = new Part($decodedHtml);\n        $htmlPart-&gt;type = &#039;text\/html&#039;;\n        $htmlPart-&gt;charset = &#039;utf-8&#039;;\n        $htmlPart-&gt;encoding = LaminasMime::ENCODING_QUOTEDPRINTABLE;\n\n        $mimeMessage = new MimeMessage();\n        $mimeMessage-&gt;setParts(&#091;$htmlPart, $attachment]);\n\n        $message-&gt;setBody($mimeMessage);\n\n        return $transport;\n    }\n}<\/pre>\n\n\n\n<p>Below is the email template file <strong>your_template_name<\/strong> needs to be created under view\/frontend\/email\/<strong>your_template_name<\/strong>.xml<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\">&lt;!-- \/**\n * Webkul Software Private Limited\n *\n * @category  Webkul Software Private Limited\n * @package   Webkul_Email\n * @author    Webkul Software Private Limited\n * @copyright Webkul Software Private Limited (https:\/\/webkul.com)\n * @license   https:\/\/store.webkul.com\/license.html\n *\/ --&gt;\n&lt;!--@subject {{var message_email_subject}} @--&gt;\n&lt;!--@vars {\n&quot;var message_email_subject&quot;:&quot;Message Email Subject&quot;\n&quot;var message_email_content|raw&quot;:&quot;Message Email Content&quot;\n&quot;var recipient_name&quot;:&quot;Recipient Name&quot;\n} @--&gt;\n{{template config_path=&quot;design\/email\/header_template&quot;}}\n&lt;p&gt;Dear {{var recipient_name}},&lt;\/p&gt;\n&lt;p&gt;{{var message_email_content|raw}}&lt;\/p&gt;\n{{template config_path=&quot;design\/email\/footer_template&quot;}}<\/pre>\n\n\n\n<p><strong>Here is the output below. Where attachment is added in the mail.<br><\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img decoding=\"async\" width=\"1200\" height=\"613\" src=\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2025\/03\/emailwithattachments-3-1200x613.webp\" alt=\"Email with attachment\" class=\"wp-image-486900\" style=\"object-fit:cover;width:1200px;height:613px\" srcset=\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2025\/03\/emailwithattachments-3-1200x613.webp 1200w, https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2025\/03\/emailwithattachments-3-300x153.webp 300w, https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2025\/03\/emailwithattachments-3-250x128.webp 250w, https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2025\/03\/emailwithattachments-3-768x392.webp 768w, https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2025\/03\/emailwithattachments-3-1536x785.webp 1536w, https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2025\/03\/emailwithattachments-3.webp 1587w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" loading=\"lazy\" \/><\/figure>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>While learning how to add file attachments to email, we faced an issue for a client in Magento 2. The attachments failed to load, and after investigation, we found the problem was caused by updates in the Zend module. If you\u2019re looking to send order attachments, you can check\u00a0the\u00a0Magento 2 Order Attachment Extension. We created <a href=\"https:\/\/webkul.com\/blog\/how-to-send-email-attachment-magento2\/\">[&#8230;]<\/a><\/p>\n","protected":false},"author":444,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9121],"tags":[2070,590],"class_list":["post-486858","post","type-post","status-publish","format-standard","hentry","category-magento-2","tag-magento2","tag-webkul"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>How to add File Attachments in Email in Magento 2 - Webkul Blog<\/title>\n<meta name=\"description\" content=\"In this code, we add file attachments to an email using Magento. The method uses LaminasMimePart and MagentoMailTransportInterface.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/webkul.com\/blog\/how-to-send-email-attachment-magento2\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to add File Attachments in Email in Magento 2 - Webkul Blog\" \/>\n<meta property=\"og:description\" content=\"In this code, we add file attachments to an email using Magento. The method uses LaminasMimePart and MagentoMailTransportInterface.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/webkul.com\/blog\/how-to-send-email-attachment-magento2\/\" \/>\n<meta property=\"og:site_name\" content=\"Webkul Blog\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/webkul\/\" \/>\n<meta property=\"article:published_time\" content=\"2025-11-04T09:41:58+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-11-04T10:05:14+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/webkul.com\/blog\/wp-content\/uploads\/2025\/03\/emailwithattachments-3-1200x613.webp\" \/>\n<meta name=\"author\" content=\"Rajesh Pathak\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@webkul\" \/>\n<meta name=\"twitter:site\" content=\"@webkul\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Rajesh Pathak\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/webkul.com\/blog\/how-to-send-email-attachment-magento2\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/webkul.com\/blog\/how-to-send-email-attachment-magento2\/\"},\"author\":{\"name\":\"Rajesh Pathak\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/d896112f48e09d54b07c2b61352aa96b\"},\"headline\":\"How to add File Attachments in Email in Magento 2\",\"datePublished\":\"2025-11-04T09:41:58+00:00\",\"dateModified\":\"2025-11-04T10:05:14+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/webkul.com\/blog\/how-to-send-email-attachment-magento2\/\"},\"wordCount\":280,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/webkul.com\/blog\/#organization\"},\"image\":{\"@id\":\"https:\/\/webkul.com\/blog\/how-to-send-email-attachment-magento2\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/webkul.com\/blog\/wp-content\/uploads\/2025\/03\/emailwithattachments-3-1200x613.webp\",\"keywords\":[\"Magento2\",\"webkul\"],\"articleSection\":[\"Magento 2\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/webkul.com\/blog\/how-to-send-email-attachment-magento2\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/webkul.com\/blog\/how-to-send-email-attachment-magento2\/\",\"url\":\"https:\/\/webkul.com\/blog\/how-to-send-email-attachment-magento2\/\",\"name\":\"How to add File Attachments in Email in Magento 2 - Webkul Blog\",\"isPartOf\":{\"@id\":\"https:\/\/webkul.com\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/webkul.com\/blog\/how-to-send-email-attachment-magento2\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/webkul.com\/blog\/how-to-send-email-attachment-magento2\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/webkul.com\/blog\/wp-content\/uploads\/2025\/03\/emailwithattachments-3-1200x613.webp\",\"datePublished\":\"2025-11-04T09:41:58+00:00\",\"dateModified\":\"2025-11-04T10:05:14+00:00\",\"description\":\"In this code, we add file attachments to an email using Magento. The method uses Laminas\\\\Mime\\\\Part and Magento\\\\Mail\\\\TransportInterface.\",\"breadcrumb\":{\"@id\":\"https:\/\/webkul.com\/blog\/how-to-send-email-attachment-magento2\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/webkul.com\/blog\/how-to-send-email-attachment-magento2\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/webkul.com\/blog\/how-to-send-email-attachment-magento2\/#primaryimage\",\"url\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2025\/03\/emailwithattachments-3.webp\",\"contentUrl\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2025\/03\/emailwithattachments-3.webp\",\"width\":1587,\"height\":811},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/webkul.com\/blog\/how-to-send-email-attachment-magento2\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/webkul.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to add File Attachments in Email in Magento 2\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/webkul.com\/blog\/#website\",\"url\":\"https:\/\/webkul.com\/blog\/\",\"name\":\"Webkul Blog\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/webkul.com\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/webkul.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/webkul.com\/blog\/#organization\",\"name\":\"WebKul Software Private Limited\",\"url\":\"https:\/\/webkul.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png\",\"contentUrl\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png\",\"width\":380,\"height\":380,\"caption\":\"WebKul Software Private Limited\"},\"image\":{\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/webkul\/\",\"https:\/\/x.com\/webkul\",\"https:\/\/www.instagram.com\/webkul\/\",\"https:\/\/www.linkedin.com\/company\/webkul\",\"https:\/\/www.youtube.com\/user\/webkul\/\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/d896112f48e09d54b07c2b61352aa96b\",\"name\":\"Rajesh Pathak\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/75e8d1cd06e1cb22acade1a768946b2ef8a0157cf66440e8739aa42c1d51e049?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/75e8d1cd06e1cb22acade1a768946b2ef8a0157cf66440e8739aa42c1d51e049?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g\",\"caption\":\"Rajesh Pathak\"},\"url\":\"https:\/\/webkul.com\/blog\/author\/rajesh-pathak672\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to add File Attachments in Email in Magento 2 - Webkul Blog","description":"In this code, we add file attachments to an email using Magento. The method uses LaminasMimePart and MagentoMailTransportInterface.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/webkul.com\/blog\/how-to-send-email-attachment-magento2\/","og_locale":"en_US","og_type":"article","og_title":"How to add File Attachments in Email in Magento 2 - Webkul Blog","og_description":"In this code, we add file attachments to an email using Magento. The method uses LaminasMimePart and MagentoMailTransportInterface.","og_url":"https:\/\/webkul.com\/blog\/how-to-send-email-attachment-magento2\/","og_site_name":"Webkul Blog","article_publisher":"https:\/\/www.facebook.com\/webkul\/","article_published_time":"2025-11-04T09:41:58+00:00","article_modified_time":"2025-11-04T10:05:14+00:00","og_image":[{"url":"https:\/\/webkul.com\/blog\/wp-content\/uploads\/2025\/03\/emailwithattachments-3-1200x613.webp","type":"","width":"","height":""}],"author":"Rajesh Pathak","twitter_card":"summary_large_image","twitter_creator":"@webkul","twitter_site":"@webkul","twitter_misc":{"Written by":"Rajesh Pathak","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/webkul.com\/blog\/how-to-send-email-attachment-magento2\/#article","isPartOf":{"@id":"https:\/\/webkul.com\/blog\/how-to-send-email-attachment-magento2\/"},"author":{"name":"Rajesh Pathak","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/d896112f48e09d54b07c2b61352aa96b"},"headline":"How to add File Attachments in Email in Magento 2","datePublished":"2025-11-04T09:41:58+00:00","dateModified":"2025-11-04T10:05:14+00:00","mainEntityOfPage":{"@id":"https:\/\/webkul.com\/blog\/how-to-send-email-attachment-magento2\/"},"wordCount":280,"commentCount":0,"publisher":{"@id":"https:\/\/webkul.com\/blog\/#organization"},"image":{"@id":"https:\/\/webkul.com\/blog\/how-to-send-email-attachment-magento2\/#primaryimage"},"thumbnailUrl":"https:\/\/webkul.com\/blog\/wp-content\/uploads\/2025\/03\/emailwithattachments-3-1200x613.webp","keywords":["Magento2","webkul"],"articleSection":["Magento 2"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/webkul.com\/blog\/how-to-send-email-attachment-magento2\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/webkul.com\/blog\/how-to-send-email-attachment-magento2\/","url":"https:\/\/webkul.com\/blog\/how-to-send-email-attachment-magento2\/","name":"How to add File Attachments in Email in Magento 2 - Webkul Blog","isPartOf":{"@id":"https:\/\/webkul.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/webkul.com\/blog\/how-to-send-email-attachment-magento2\/#primaryimage"},"image":{"@id":"https:\/\/webkul.com\/blog\/how-to-send-email-attachment-magento2\/#primaryimage"},"thumbnailUrl":"https:\/\/webkul.com\/blog\/wp-content\/uploads\/2025\/03\/emailwithattachments-3-1200x613.webp","datePublished":"2025-11-04T09:41:58+00:00","dateModified":"2025-11-04T10:05:14+00:00","description":"In this code, we add file attachments to an email using Magento. The method uses Laminas\\Mime\\Part and Magento\\Mail\\TransportInterface.","breadcrumb":{"@id":"https:\/\/webkul.com\/blog\/how-to-send-email-attachment-magento2\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/webkul.com\/blog\/how-to-send-email-attachment-magento2\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/webkul.com\/blog\/how-to-send-email-attachment-magento2\/#primaryimage","url":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2025\/03\/emailwithattachments-3.webp","contentUrl":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2025\/03\/emailwithattachments-3.webp","width":1587,"height":811},{"@type":"BreadcrumbList","@id":"https:\/\/webkul.com\/blog\/how-to-send-email-attachment-magento2\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/webkul.com\/blog\/"},{"@type":"ListItem","position":2,"name":"How to add File Attachments in Email in Magento 2"}]},{"@type":"WebSite","@id":"https:\/\/webkul.com\/blog\/#website","url":"https:\/\/webkul.com\/blog\/","name":"Webkul Blog","description":"","publisher":{"@id":"https:\/\/webkul.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/webkul.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/webkul.com\/blog\/#organization","name":"WebKul Software Private Limited","url":"https:\/\/webkul.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png","contentUrl":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png","width":380,"height":380,"caption":"WebKul Software Private Limited"},"image":{"@id":"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/webkul\/","https:\/\/x.com\/webkul","https:\/\/www.instagram.com\/webkul\/","https:\/\/www.linkedin.com\/company\/webkul","https:\/\/www.youtube.com\/user\/webkul\/"]},{"@type":"Person","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/d896112f48e09d54b07c2b61352aa96b","name":"Rajesh Pathak","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/75e8d1cd06e1cb22acade1a768946b2ef8a0157cf66440e8739aa42c1d51e049?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/75e8d1cd06e1cb22acade1a768946b2ef8a0157cf66440e8739aa42c1d51e049?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g","caption":"Rajesh Pathak"},"url":"https:\/\/webkul.com\/blog\/author\/rajesh-pathak672\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/486858","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/users\/444"}],"replies":[{"embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/comments?post=486858"}],"version-history":[{"count":26,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/486858\/revisions"}],"predecessor-version":[{"id":512122,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/486858\/revisions\/512122"}],"wp:attachment":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/media?parent=486858"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/categories?post=486858"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/tags?post=486858"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}