Issue in sending attachment in Emails Magento2.3
-
13-04-2021 - |
문제
I am trying to send attachments in the email.
The following code is working fine but multiple attachments are sending even if send single attachment also.
I have used below code.
$attribute = $this->eavConfig->getAttribute('catalog_product', $attributeCode);//it is a brand attrbute with drop down
$options = $attribute->getSource()->getAllOptions();
foreach($options as $option) {
$supplierCode = trim($option['label']);
if(isset($supplierCode) && $supplierCode != ''){
$supplierEmail = $this->supplierEmail($supplierCode); // this will give the email id
if(isset($supplierEmail) && $supplierEmail != ''){
$this->sendEmailtoSuppliers($supplierCode,$supplierEmail,$fileDate);
}
}
}
public function sendEmailtoSuppliers($supplierCode,$supplierEmail,$fileDate){
try
{
$fileInfo = $this->getFileInfo($supplierCode,$fileDate);
$fileSize = filesize($fileInfo);
$this->_logger->info(strlen(file_get_contents($fileInfo));
if($fileSize > 164 ){
$file_contents = '';
$fileName = $supplierCode.$fileDate.'.csv';
$file_contents = file_get_contents($fileInfo);
// Send Mail
$this->_inlineTranslation->suspend();
$storeScope = \Magento\Store\Model\ScopeInterface::SCOPE_STORE;
$senderEmail = $this->_scopeConfig->getValue('trans_email/ident_support/email',$storeScope);
$senderName = $this->_scopeConfig->getValue('trans_email/ident_support/name',$storeScope);
$sender = [
'name' => $senderName,
'email' => $senderEmail
];
$sentToEmail = $supplierEmail;
$sentToName = $supplierCode;
$transport = $this->_transportBuilder
->setTemplateIdentifier('suppliers_email_template')
->setTemplateOptions(
[
'area' => 'frontend',
'store' => \Magento\Store\Model\Store::DEFAULT_STORE_ID,
]
)
->setTemplateVars([
'name' => $sentToName,
'email' => $sentToEmail
])
->setFrom($sender)
->addTo($sentToEmail,$sentToName)
->addAttachment($file_contents, $fileName, 'text/csv')
->getTransport();
$transport->sendMessage();
$this->_inlineTranslation->resume();
}
unlink($fileInfo);
} catch(\Exception $e){
$this->_logger->info('--supplier email exception'.$e->getMessage());
}
}
public function getFileInfo($supplierCode,$fileDate){
$directory = $this->filesystem->getDirectoryWrite(DirectoryList::VAR_DIR); //here files stored with attribute option names
$varPath = $this->_dir->getPath('var');
$filePath = $varPath.'/export/'.$supplierCode.$fileDate.'.csv';
return $filePath;
}
The above code is working well, the problem here is for the subsequent options it is sending other option files also, may be a for loop issue,
I am sending here single attachment but multiple files are going to the email. that means old attachment also going for new email.
The email should send appropriate file for the each option. Can anyone look into it and update me what mistake i am doing here. Thanks
해결책
Change your addAttachment() where you overrite it.
public function addAttachment($content, $fileName, $fileType)
{
$this->attachments = [];
$attachmentPart = $this->partFactory->create();
$attachmentPart->setContent($content)
->setType($fileType)
->setFileName($fileName)
->setDisposition(Mime::DISPOSITION_ATTACHMENT)
->setEncoding(Mime::ENCODING_BASE64);
$this->attachments[] = $attachmentPart;
return $this;
}
I Hope This Helps You.
제휴하지 않습니다 magento.stackexchange