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 != ''){

  public function sendEmailtoSuppliers($supplierCode,$supplierEmail,$fileDate){
        $fileInfo = $this->getFileInfo($supplierCode,$fileDate);
        $fileSize = filesize($fileInfo);    
        if($fileSize > 164 ){
            $file_contents = '';
            $fileName = $supplierCode.$fileDate.'.csv';
            $file_contents = file_get_contents($fileInfo);
                // Send Mail
    $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
        'area' => 'frontend',
        'store' => \Magento\Store\Model\Store::DEFAULT_STORE_ID,
        'name'  => $sentToName,
        'email'  => $sentToEmail
    ->addAttachment($file_contents, $fileName, 'text/csv')



    } 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();
    $this->attachments[] = $attachmentPart;

    return $this;

I Hope This Helps You.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.