How to attach a csv file to php email (code running from the magento root)

How to attach a csv file to php email (code running from the magento root)

August 16, 2026 · Illustrated author avatarBy Stefan Bogdanescu
How to attach a csv file to php email (code running from the magento root)

Attaching Multiple CSV Files to Emails in Magento

Sending emails with attachments is a common requirement for many e-commerce businesses running on the Magento platform. If you’re trying to attach multiple CSV files from your root directory using PHP, I’ll guide you through the process step by step.

Understanding Attachments in Magento

In Magento, email attachments are sent as part of the MIME content. To add an attachment, you need to use the Mage_Core_Model_Email_Template or Zend_Mail_Transport_Sendmail classes.

Adding Multiple CSV Attachments

To attach multiple CSV files, you’ll need to modify your existing code as follows:

$body = "Hi there, here is some body content";
$mail = Mage::getModel('core/email');
$mail->setToName('John');
$mail->setToEmail('ab@example.net');
$mail->setBody($body);
$mail->setSubject('The Subject');
$mail->setFromEmail('yourstore@url.com');
$mail->setFromName("Your Name");
$mail->setType('text');// You can use 'html' or 'text'

// Create an array of attachment files
$attachments = [
    '/path/to/file1.csv',
    '/path/to/file2.csv',
    '/path/to/file3.csv'
];

try {
    // Attach the CSV files
    foreach ($attachments as $attachment) {
        $mail->createAttachment(
            file_get_contents($attachment),
            Zend_Mime::TYPE_BASE64,
            'application/csv',
            'filename.csv' // You can change this to be the actual filename
        );
    }

    $mail->send();
    Mage::getSingleton('core/session')->addSuccess('Your request has been sent');

}
catch (Exception $e) {
    Mage::getSingleton('core/session')->addError('Unable to send.');
}

?>

In this modified code, we’re creating an array of CSV file paths and then iterating through them using a foreach loop. For each file, we call the createAttachment() method with the following parameters:

  • The contents of the attachment (obtained using file_get_contents())
  • The MIME type to use for the attachment (Zend_Mime::TYPE_BASE64)
  • The subtype for the attachment (in this case, ‘application/csv’ for CSV files)
  • An optional filename to give to the attachment

Best Practices and Potential Issues

  1. File Paths: Ensure that the file paths in your $attachments array are correct and absolute. Relative paths might not work as expected.

  2. File Permissions: Make sure the user running your PHP script has read permissions on the files being attached.

  3. Email Size Limit: Keep an eye on the total size of the email, including the attachments. Magento has a default limit of 5MB for emails. You can increase this in your configuration if needed (system->config->Advanced->System->Maxemailattached).

  4. Performance: Attaching large files can impact the performance of your script and potentially time out. Consider compressing or breaking large files into smaller chunks.

Conclusion

Attaching multiple CSV files to an email from a Magento root directory is a straightforward process once you understand the MIME handling in PHP and Magento’s built-in email functionality. Always remember to validate and sanitize any user-provided file paths to prevent potential security vulnerabilities.

For more advanced email customization in Magento, I recommend checking out this comprehensive guide on email templates on our blog at Laravel Company. It covers everything from creating new templates to sending emails programmatically.

Happy coding!

Themes Email Import/Export

Our network