We're a community of 1.1M IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,080,324 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

mailing exported sql tables in excel file to email address

what I am trying to do is use this php script to load the data being submitted in the html form into my database and then populate the database into an excel (csv) file and then e-mail it to my address.

Everything works great it populates into the database and creates the xls file perfect. But it is wanting me to download the file. What can I add to the script to have it e-mail the file to my e-mail address INSTEAD of downloading it.

HERE IS THE CODE:

<?php

 header( 'Content-Type: text/csv' );
 header( 'Content-Disposition: attachment;filename=Fund_Tours_Client_List.csv' );

define('DB_NAME', 'mydatabase');
define('DB_USER', 'myusername');
define('DB_PASSWORD', 'mypassword');
define('DB_HOST', 'localhost');

$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
        if (!$link)   
            {
                die('Could not connect: ' . mysql_error());  
                }   

$db_selected = mysql_select_db(DB_NAME, $link);

if (!$db_selected) {
    die('Can\'t use ' . DB_NAME . ': ' . mysql_error());
}

$value1 = $_POST['groupname'];
$value2 = $_POST['name'];
$value3 = $_POST['address'];
$value4 = $_POST['city'];
$value5 = $_POST['state'];
$value6 = $_POST['zip'];
$value7 = $_POST['homephone'];
$value8 = $_POST['cellphone'];
$value9 = $_POST['email'];
$value10 = $_POST['age'];
$value11 = $_POST['maritalstatus'];
$value12 = $_POST['income'];
$value13 = $_POST['contact1'];
$value14 = $_POST['contact2'];
$value15 = $_POST['contact3'];
$value16 = $_POST['date1'];
$value17 = $_POST['date2'];
$value18 = $_POST['date3'];


$sql = "INSERT INTO clients (groupname, name, address, city, state, zip, homephone, cellphone, email, age, maritalstatus, income, contact1, contact2, contact3, date1, date2, date3) VALUES ('$value1', '$value2', '$value3', '$value4', '$value5', '$value6', '$value7', '$value8', '$value9', '$value10', '$value11', '$value12', '$value13', '$value14', '$value15', '$value16', '$value17', '$value18')";

if (!mysql_query($sql)) {
    die('Error: ' . mysql_error());
}

mysql_close();

$conn = mysql_connect( 'localhost', 'fundtour_rick', 'Fund.Tours' ) or die( mysql_error( ) );
 mysql_select_db( 'fundtour_info', $conn ) or die( mysql_error( $conn ) );

 $query = sprintf( 'SELECT
`groupname` AS `Group`,
`name` AS `Customer Name`, 
`address` AS `Address`,
`city` AS `City`,
`state` AS `State`,
`zip` AS `Zip Code`,
`homephone` AS `Home Phone`,
`cellphone` AS `Cell Phone`,
`email` AS `E-Mail`,
`age` AS `Age Group`,
`maritalstatus` AS `Marital Status`,
`income` AS `Household Income`,
`contact1` AS `Contact VIA`,
`contact2` AS `Contact VIA`,
`contact3` AS `Contact VIA`,
`date1` AS `1st Date`,
`date2` AS `2nd Date`,
`date3` AS `3rd Date`

FROM fundtour_info.clients clients' );

 $result = mysql_query( $query, $conn ) or die( mysql_error( $conn ) );

 $row = mysql_fetch_assoc( $result );
 if ( $row )
 {
 echocsv( array_keys( $row ) );
 }

 while ( $row )
 {
 echocsv( $row );
 $row = mysql_fetch_assoc( $result );
 }

 function echocsv( $fields )
 {
 $separator = '';
 foreach ( $fields as $field )
 {
 if ( preg_match( '/\\r|\\n|,|"/', $field ) )
 {
 $field = '"' . str_replace( '"', '""', $field ) . '"';
 }
 echo $separator . $field;
 $separator = ',';
 }
 echo "\r\n";
 }

?>

Thanks for any help

3
Contributors
2
Replies
11 Months
Discussion Span
2 Months Ago
Last Updated
4
Views
agon024
Newbie Poster
1 post since Apr 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

what I am trying to do is use this php script to load the data being submitted in the html form into my database and then populate the database into an excel (csv) file and then e-mail it to my address.

I think someone ask the question here:

http://forums.phpfreaks.com/topic/260518-e-mailing-exported-sql-tables-in-excel-file-format/

LastMitch
Industrious Poster
4,374 posts since Mar 2012
Reputation Points: 149
Solved Threads: 350
Skill Endorsements: 47

This one was from 6 years ago, but it may still be relevant
http://www.daniweb.com/web-development/php/threads/60795/send-email-with-attachment

pixelsoul
Master Poster
709 posts since Feb 2011
Reputation Points: 171
Solved Threads: 107
Skill Endorsements: 19

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page generated in 0.0668 seconds using 2.67MB