How to send the simple mail with CSV attachment file in Magento 2.
Please help me to solve out this Question.
Thanks in Advanced.
-
did you tried the solution posted by me?Mohit Kumar Arora– Mohit Kumar Arora2019年09月11日 09:49:54 +00:00Commented Sep 11, 2019 at 9:49
-
@MohitKumarArora, to be honest, I don't know how to send an email with CSV File and also I am confused with your code in where should I place the receiver email.AKM– AKM2019年09月11日 10:03:35 +00:00Commented Sep 11, 2019 at 10:03
2 Answers 2
First of all, Magento (at the time of writing, I am using Magento version 2.2.7) does not provide the functionality to add an attachment in the email.
To add an attachment programmatically in the email, first, you need to override \Magento\Framework\Mail\Template\TransportBuilder class.
Step 1: app/code/[Namespace]/[Module]/etc/di.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="\Magento\Framework\Mail\Template\TransportBuilder" type="\[Namespace]\[Module]\Mail\Template\TransportBuilder" />
</config>
Step 2: app/code/[Namespace]/[Module]/Mail/Template/TransportBuilder.php
<?php
namespace Stream\EmailPrintableCsv\Mail\Template;
class TransportBuilder extends \Magento\Framework\Mail\Template\TransportBuilder {
public function addAttachment(
$filePath, $filename = null, $mimeType = \Zend_Mime::TYPE_OCTETSTREAM, $disposition = \Zend_Mime::DISPOSITION_ATTACHMENT, $encoding = \Zend_Mime::ENCODING_BASE64
) {
if (file_exists($filePath)) {
$body = file_get_contents($filePath);
$this->message->createAttachment($body, $mimeType, $disposition, $encoding, $filename);
}
return $this;
}
}
Now, you can call addAttachment() function while sending an email.
Note: addAttachment function requires 2 parameters: filePath and fileName.
-
But first, I don't know how to send the email in my cron class.AKM– AKM2019年09月11日 10:24:50 +00:00Commented Sep 11, 2019 at 10:24
-
Please check my another question which is connected to this question, please help me if you have any idea(i.e. magento.stackexchange.com/questions/288206/… )AKM– AKM2019年09月12日 05:27:34 +00:00Commented Sep 12, 2019 at 5:27
-
The link is redirecting to the same question.Mohit Kumar Arora– Mohit Kumar Arora2019年09月12日 06:07:09 +00:00Commented Sep 12, 2019 at 6:07
-
magento.stackexchange.com/questions/289415/…AKM– AKM2019年09月12日 06:10:04 +00:00Commented Sep 12, 2019 at 6:10
I wrote an extension to allow you to attach a file to the contact form
Take a look at the controller
You might find it helpful