www.webdeveloper.com
Results 1 to 7 of 7

Thread: attaching an excel sheet to an email

  1. #1
    Join Date
    Oct 2009
    Posts
    6

    attaching an excel sheet to an email

    hi,

    I'm using MIME to send a html email and i wanted to attach an excel sheet that will be generated from My SQL database to that email.
    can you help me on how can i generate and attach that excel sheet to emails that my script sends?

    thank you

  2. #2
    Join Date
    Mar 2010
    Posts
    2,803
    This tute shows how to send attachments with html emails.

    The tute attaches a zip file, but you should just need to change the mime type and file name to send a spreadsheet instead.

  3. #3
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,392
    I like to use PHPMailer and just call the applicable methods, because I'm lazy. (Use the preceding link to download the PHP5-compatible version, as the link on their web site points to the PHP4 version.)
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  4. #4
    Join Date
    Aug 2009
    Posts
    593
    I'm guessing you also require the ability to create the Excel Spreadsheet using PHP?
    Although I've never tried it fully, you can start by looking at something like COM.

    Eg.

    PHP Code:
    <?php

        $excel 
    = new COM('Excel.Application');
    Then start adding sheets, data, styling the columns and saving the file to the filesystem.

    I tried the code above and it created an Excel instance in the background, obviously on my Windows computer. I only have working examples on creating spreadsheets using Visual Basic, but I'm sure the concept is the same with the PHP variant.

  5. #5
    Join Date
    Mar 2010
    Posts
    2,803
    Quote Originally Posted by NogDog View Post
    I like to use PHPMailer and just call the applicable methods, because I'm lazy.
    I'm lazy as well but rather than put up with any limitations a 3rd party application might have, I give the user a form with a tinyMCE text editor for them to write their html email content and the option to upload attachments. The code in the tute does the rest when the form data including target email addreses is submited to it.

    This way I can customise the form and php script to however I like and then simply bolt it on to any other php application.

  6. #6
    Join Date
    Apr 2008
    Location
    Jeddah, Saudi Arabia
    Posts
    43
    Thank you Trina for helping that, it is really helpful

  7. #7
    Join Date
    Oct 2009
    Posts
    6
    thank you for responds
    in the fallowing code i make a csv file but i have no idea if it works or how to attach it because in the cases above the file was already there!

    PHP Code:
    <?php
    include_once('functions.php');
            
    $todaydate date("Y-m-d");
            
    $usersquery mysql_query("SELECT * FROM users") or die(mysql_error);
            
    $usersnumber mysql_num_rows($usersquery);
            if (
    $usersnumber == 0)
            {
                echo 
    "there are no users registered in this website";
            }
            else
            {
                
    $cardsoverall mysql_query("SELECT * FROM cards") or die(mysql_error);
                
    $cardsoverallnum mysql_num_rows($cardsoverall);
                
    $startnum = ($cardsoverallnum 1);
                echo 
    "You have " $usersnumber " user(s)<br/>";
                echo 
    "You have " $cardsoverallnum " card(s)<br/>";
                echo 
    "Excel starting number is: " $startnum "<br/>";
                echo 
    "============================<br/>";
                while(
    $userarray mysql_fetch_array($usersquery))
                {
                    
    $name $userarray['first'];
                    
    $email '<html><body><strong>' $name ",</strong><br/><br/>" .
                             
    "<strong>Your cards have been processed, and added to your account.</strong><br/>" .
                             
    "<strong>Please sign into your 'Account Navigation' page. Please set the asking price(s) for your cards and view your account balance.</strong><br/><br/>" .
                             
    "<table rules='all' border='1' style='border-color: #666;' cellpadding='10'>" 
                             
    "<tr style='background: #eee;'><th><center><strong>Item Number</strong><center></th><th><center><strong>Description</strong></center></th></tr>";
                             
                    
    $username $userarray['username'];
                    echo 
    $username ": ";
                    
    $cards mysql_query("SELECT * FROM cards WHERE title = '' AND seller = '$username'") or die("card finding error");
                    
    $cardsnumber mysql_num_rows($cards);
                    if (
    $cardsnumber == 0)
                    {
                        echo 
    "No new cards for this user<br/>";
                    }
                    else
                    {
                        
    $_file 'Cardslist.csv'
                        
    $_fp = @fopen$_file'w' ); 
                        
    $_csv_data"Item Number" "," "Description" "\n"
                        @
    fwrite$_fp$_csv_data );        
                        while (
    $newcards mysql_fetch_array($cards))
                        {
                            
    $cardid $newcards['cardid'];
                            if (
    $newcards['sn'] != NULL)
                                
    $serialnum " /" $newcards['sn'];
                            else
                                
    $serialnum '';
                                
                            if (
    $newcards['cardnumber'] != NULL)
                                
    $title $newcards['year'] . " " $newcards['brand'] . " "$newcards['setname'] . " #" $newcards['cardnumber'] . " - " $newcards['playername'] . $serialnum;
                            else
                                
    $title $newcards['year'] . " " $newcards['brand'] . " "$newcards['setname'] . " - " $newcards['playername'] . $serialnum;
                            
                            
    $horizental strtolower($newcards['horizontal']);
                            
    $backhorizental strtolower($newcards['backhorizental']);
                            
    $seller strtolower($newcards['seller']);
                            
    $updatecards mysql_query("UPDATE cards SET title = '$title', horizontal = '$horizental', backhorizental = '$backhorizental', aday = '$todaydate', ava = 'yes', sold = 'no', send = 'no', shipping = 'no', seller = '$seller' WHERE cardid = '$cardid'") or die("changing information error");
                            if (!
    $updatecards)
                            {
                                echo 
    "ERROR CARD " $title " with item number of " $cardid " had a problem!";
                            }
                            else
                            {
                                
    $email $email "<tr><td><center>" $cardid "</center></td><td><center>" $title "</center></td></tr>";
                                
    $_csv_data$cardid ',' $title "\n"
                                @
    fwrite$_fp$_csv_data ); 
                            }
                        }
                        
    $email $email "</table><br/><br/><strong>Your card processing receipt is attached in this email.<br/>If you have any comments, questions or concerns please feel free to contact us</strong>" 
                                           
    "<br/><br/><strong>SCM</strong><br/>" "<a href = 'http://www.scm.com'>scm.com</a>";
                        @
    fclose$_fp );
                        
    $useremail $userarray['email'];
                        
    $headers "From: scm <no-reply@scm.com>" "\r\n";
                        
    $headers .= "Reply-To: no-reply@scm.com" ."\r\n";
                        
    $headers .= 'Return-path: no-reply@scms.com' "\r\n";
                        
    $headers .= "MIME-Version: 1.0\r\n";
                        
    $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
                        if (
    mail ($useremail'Cards were added to your Account'$email$headers"-fno-reply@scm.com"))
                            echo 
    "E-mail was sent<br/>";
                        else
                            echo 
    "error - e-mail didn't go through<br/>";
                    }
                }
            }
    ?>
    Thank you

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles