Code >> Email MySQL Query Results as a CSV File Attachment

Email MySQL Query Results as a CSV File Attachment

This standalone PHP file runs a MySQL query. Then sends the results in an email as a CSV (comma separated values) file attachment. This CSV file is properly formatted so it can be opened by OpenOffice.org Calc, or Microsoft Excel.

Language: PHP
File Size: 15 kb
Version: 1.0 - November 11, 2009
MD5 Sum: 4ee7747dd67af1d398b1f6ed523ba21e
download Email MySQL Query Results as a CSV File Attachment

License & Copyright:

Copyright © 2009 Stephen R. Owens

This software is licensed under the GNU GPL version 3.0.

About the PHP script to Email the Results of a MySQL Query as a CSV File Attachment

I created this code as a standalone PHP file that can be run by opening it in a web browser, or running it from a Cron Job. The source code in this PHP file is well commented and can be modified to fit your needs. The GNU GPL license gives you the room to adapt it to your project.

What it does:

  1. Runs a MySQL query (that you define in the settings)
  2. Builds a CSV file from the query results
  3. Directly emails the CSV file as an attachment (this code doesn't save the results to a file on the server. If you are looking for that, try Google).

Some example of how others have used this file include:

  • Automatically run at the end of every month as a Cron Job, to generate data for the accounting department to verify monthly sales.
  • Turned into a class file to email all kinds of data results to different employees from an eCommerce site.
  • Used to send violating data from a database when certain flags have been set off by web game players that could be potentialy cheating.

Requirements

A web server with PHP and a MySQL database.
You will need to know how to:

  • Assign values to PHP variables
    • Samples and comments in the source code are provided to help in this process.
    • If you need more help with PHP. A good place to start is the PHP tutorials at W3schools.com: http://w3schools.com/php/php_variables.asp
  • Write an SQL query

How to Use

Step 1: Add your Server Information, and Personal Settings, to the Source Code

The source code is well documented and you will be able to figure out what to enter, and where to enter it, by following these directions:

  1. Open the file email-query-results-as-csv-file.php
  2. On line 33 you will see  a section called “Configure Your Personal Settings
  3. Between lines 37 and 71, you will enter your MySQL server address and login, database name, the email addresses you want the email to be sent from and to, and an email message that is to be sent to describe the attachment.

Step 2: Upload the file to Your Web Sever

After you fill in your specific details, upload the file email-query-results-as-csv-file.php to your web server.

Setp 3: Run the File from a Web Browser

Run the file email-query-results-as-csv-file.php from a web browser. It will output messages of success or error at each step.

Support

If you have questions on using this tool, or have a comment, you can contact me or post a message into the comments section below.

If you need helping modifying this file for a particular purpose, or integrating it with another PHP program, and you don't have the coding skills, I will code for money. Contact me for a quote.

History

2009-NOV-11: v1.0
* Initial Public Release

Comments for "Email MySQL Query Results as a CSV File Attachment"

1.
junaid wrote:
February 23, 2010, 1:23 am
Good work. keep it up.
2.
Marco G. wrote:
March 2, 2010, 7:55 am
Really great work. very easy to setup and run!

Grazie
3.
baku wrote:
April 12, 2010, 5:24 pm
GReat! You save my life.

Im trying to modify the code in order to send N files por N querys. But I'm not sure if it is possible.

Do you understand the idea? Query 1 --> File1.csv ; Query2 --> File2.csv, etc...
For sure I could send N emails, but maybe this will be marked as SPAM.

I undestood how to create the CSV file, but I don't know if it possible to attach 2 or more files with the code.

Can you help me?
4.
Stephen Owens wrote:
May 28, 2010, 7:24 am
@baku

Attaching multiple files to one email is very possible. You would have to repeat the code found from lines 167 to Lines 181. These lines attach the CSV file. You will also have to modify the code in lines 167 to 181 to uniquely name each file attachment.

Lines 107 to 136 runs the query and builds the CSV file. Repeat these lines for each needed CSV file.

If you need help modifing the source, I sling code for money.
Add A Comment

Leave a Comment

This is a captcha-picture. It is used to prevent mass-access by robots. (see: www.captcha.net)
 Code in the picture
 Your Name (required)
 Email (optional) (will not be published)
 Website (optional)
 Comment Subject (optional)
 Notify me of follow-up comments via email.