DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Lightning Data Service for Lightning Web Components
  • Understanding the New SEC Rules for Disclosing Cybersecurity Incidents
  • Building a REST Service That Collects HTML Form Data Using Netbeans, Jersey, Apache Tomcat, and Java
  • Handle Sensitive Data Securely With Skyflow

Trending

  • How To Develop a Truly Performant Mobile Application in 2025: A Case for Android
  • Fixing Common Oracle Database Problems
  • Integrating Security as Code: A Necessity for DevSecOps
  • AI-Powered Professor Rating Assistant With RAG and Pinecone
  1. DZone
  2. Data Engineering
  3. Data
  4. How to Generate a CSV File From Form Data Send as an Email Attachment

How to Generate a CSV File From Form Data Send as an Email Attachment

If you've aggregated some great data, learn how to quickly pack it up and ship it out using PHP.

By 
Gaurav Kumar user avatar
Gaurav Kumar
·
Updated Apr. 21, 20 · Tutorial
Likes (3)
Comment
Save
Tweet
Share
27.9K Views

Join the DZone community and get the full member experience.

Join For Free

I wrote this post because, in our daily programming, we do things that we want to share with everyone so we can make programming simpler by helping each other.

Recently, I worked on a task where I needed to generate a CSV file from a Form and then send it on an email. We can also send database generated data CSV file to email by using this below method.

Let’s create an HTML form first that will generate a CSV file.

<form>
  First name:<br>
  <input type="text" name="firstname">
  <br>
  Last name:<br>
  <input type="text" name="lastname">
   Email: <br>
  <input type="text" name="email">
</form>

Let’s put some code to create a PHP form submission:

<?php
$email=$_POST['email'];
$firstName=$_POST['firstName'];
$lastName=$_POST['lastName'];

$to = "sample@ecomspark.com";
$subject = "Send Form data as CSV attachment and send email ";

$message = "".
"Email: $email" . "\n" .
"First Name: $firstName" . "\n" .
"Last Name: $lastName";

Let’s write the code for CSV generation and PHP attachment:

<?php
//The Attachment

$cr = "\n";
$data = "Email" . ',' . "First Name" . ',' . "Last Name" . $cr;
$data .= "$email" . ',' . "$firstName" . ',' . "$lastName" . $cr;

$fp = fopen('file.csv','a');
fwrite($fp,$data);
fclose($fp);

//Make the attachment
$attachments[] = Array(
   'data' => $data,
   'name' => 'file.csv',
   'type' => 'application/vnd.ms-excel'
);


//Generate a boundary string

$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";


//Add the headers for a file attachment


$headers = "MIME-Version: 1.0\n" .
           "From: {$from}\n" .
     "Cc: emaito@domain.com\n".
           "Content-Type: multipart/mixed;\n" .
           " boundary=\"{$mime_boundary}\"";


//Add a multipart boundary above the plain message


$message = "This is a multi-part message in MIME format.\n\n" .
          "--{$mime_boundary}\n" .
          "Content-Type: text/html; charset=\"iso-8859-1\"\n" .
          "Content-Transfer-Encoding: 7bit\n\n" .
          $text . "\n\n";


//Add sttachments

foreach($attachments as $attachment){
   $data = chunk_split(base64_encode($attachment['data']));
   $name = $attachment['name'];
   $type = $attachment['type'];

   $message .= "--{$mime_boundary}\n" .
              "Content-Type: {$type};\n" .
              " name=\"{$name}\"\n" .              

              "Content-Transfer-Encoding: base64\n\n" .
              $data . "\n\n" ;
}

$message .= "--{$mime_boundary}--\n";
mail($to, $subject, $message, $headers);
?>

Put together the above code and you will be able to generate and email CSV files in PHP. Please feel free to ask any questions in the comments section.

CSV Form (document) Data (computing)

Opinions expressed by DZone contributors are their own.

Related

  • Lightning Data Service for Lightning Web Components
  • Understanding the New SEC Rules for Disclosing Cybersecurity Incidents
  • Building a REST Service That Collects HTML Form Data Using Netbeans, Jersey, Apache Tomcat, and Java
  • Handle Sensitive Data Securely With Skyflow

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!