Categories: How To

How to Send Email with Attachment in Magento Without Saving File to Webserver

While working with Magento, you may need to send numerous Emails from registration confirmation to order delivery, refund requests and many more. Many times you may require to send Email with attachments without storing them to server as they are no longer required once sent. For example, if you are accepting recruitment applications from the candidates, you don’t require to save their resumes on the web server rather just want to send them directly to Email attachments. This require needs custom code in Magento.

Today I’m going to share custom code to Send Email with Attachment in Magento Without Saving File to Webserver:

This whole process works as getting the files and storing them in a temporary folder and send it as attachments directly to Email without saving as the temporary folder auto removes the files after sometimes.

Note that this code only works with Zend mail.

<?php $html="Email body"; $mail = new Zend_Mail('utf-8'); $mail->setBodyText($html);
$mail->setSubject('EmailSubject');
$mail->setFrom('test@gmail.com', 'MageComp'); // test@gmail.com is the sender Email ID, MageComp is name of the sender
$mail->addTo('test123@gmail.com','Magecomp'); // test123@gmail.com is the recipient Email ID, MageComp is name of recipient
$tmpFilePath = $_FILES['documentfile']['tmp_name'];   // uploaded pdf file and ‘documentfile’ is the name of file type field name
if ($tmpFilePath != ""){
   $fname = $_FILES['documentfile']['name'];
   $ftempname = $_FILES['documentfile']['tmp_name'];                   
   $_file = $mail->createAttachment(file_get_contents($ftempname));  
   $_file->type = 'application/pdf'; 
   $_file->filename = $fname;
}
try {
  $mail->send();
}
catch(Exception $e)
{
  Mage::log($e->getMessage());
}

This simple code saves your time to immediately send Emails without storing attachments in web servers and helps you get rid of saving non-required and unused files to make your server bulky. Hope this code has helped you to fulfill your requirements and still if you need any kind of help, I always welcome questions and feedback through comments.

Click to rate this post!
[Total: 6 Average: 4.2]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate 🎖️ Certified Magento Developer👨‍💻. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.🏏

Recent Posts

How to Delete Product Variant in a Shopify Remix App using GraphQL Mutations?

Managing a Shopify store efficiently involves keeping your product catalog organized. This includes removing outdated…

9 hours ago

6 Innovative Tools Revolutionizing E-Commerce Operations

E-commerce has transformed the way consumers shop for products and services and interact with businesses.…

3 days ago

How Upcoming Cookie Changes Will Affect Your E-commerce Website?

The e-commerce world is constantly in flux. New tech and strategies emerge daily to help…

3 days ago

Magento 2: How to Add Header and Footer in Checkout

Hello Magento Friends, In today’s blog, we will discuss adding a header and footer to…

3 days ago

Understanding Flexbox Layout in React Native

Hello React Native Friends, Building a visually appealing and responsive mobile app is crucial in…

5 days ago

HYVÄ Themes Releases: 1.3.6 & 1.3.7 – What’s New

We have brought exciting news for Magento store owners. Hyvä Themes recently released 1.3.6 and…

6 days ago