How to send a csv file in an email attachment?

I am a newbie to the web development world and I need help from time to time. Right now I am trying to send a csv file in email an attachment. I am able to generate the csv file but I can't figure out the code to tell it to send or even where to put the code...

I am sure that phpmailer is my answer somehow, I have already downloaded all the phpmailer files. I have read so many different options but they don't really give step by step answers.
Lorna ChandlerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

gr8gonzoConsultantCommented:
Like you said, PHPMailer is definitely a good option, and if you've downloaded all the files, then you should have a subfolder in there that contain some simple examples. The easiest one would be:

<?php
include_once('path/to/class.phpmailer.php'); // <-- This loads up the PHPMailer engine / code

$mail = new PHPMailer(); // This creates a new instance of PHPMailer 
$mail->From = "lorna@chandler.com"; // Set this to your email address
$mail->AddAddress("recipient@someplace.com", "Person's Name"); // Defines who it's going to 

$mail->Subject = "Set this to the email subject you want";
$mail->Body = "Set this to the body of the email";
$mail->AddAttachment("path/to/your/file.csv"); // Adds the attachment

if(!$mail->Send()) { // Sends the email
  echo "Mailer Error: " . $mail->ErrorInfo; // Shows you an error if it fails for some reason
} else {
  echo "Message sent!"; // Shows you a success message if it succeeds
}

Open in new window


That's about as easy as it can get.

Now, just so you know, just because you've executed the technical commands to send an email does NOT guarantee that the email will be successfully delivered. There are a variety of things that can come into the picture that can impact delivery. For example, your recipient's email server might think the email is spam, and either block it or put it into a spam folder. Senders (you) cannot ever guarantee delivery, nor can you ever guarantee to be notified of delivery or that the user has opened the email. All of those things are optional actions that are up to the recipients, although if you want to improve your chances of your email going out successfully, check out my article on that topic:
https://www.experts-exchange.com/articles/1222/16-Tips-to-Improve-Email-Delivery.html
0
Lorna ChandlerAuthor Commented:
what am i doing wrong?? that simple code doesn't work for me.... i put the phpmailer on the ftp client and changed the path to my csv file but when i put it on the server, i can't open the page.... I have been working on this for 2 days and I can't seem to figure it out...
0
Lorna ChandlerAuthor Commented:
I need a file that will email the csv file from the download... I have read and googled and tried every tutorial I can find and can't find a way to send that as an attachment in an email.
download2.php
createcsv2.php
0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

gr8gonzoConsultantCommented:
When you say you can't open the page, what's the exact result? Is it just a blank page?

If so, you might be getting an error without errors being displayed. So do this - add this code at the top of your page:
error_reporting(E_ALL);
ini_set("display_errors", 1);

Open in new window


...and then add this line of code:
echo __LINE__."<br />\n";

Open in new window

...before each line, so the code looks like this:

<?php
error_reporting(E_ALL);
ini_set("display_errors", 1);

echo __LINE__."<br />\n";
include_once('path/to/class.phpmailer.php'); // <-- This loads up the PHPMailer engine / code

echo __LINE__."<br />\n";
$mail = new PHPMailer(); // This creates a new instance of PHPMailer 

echo __LINE__."<br />\n";
$mail->From = "lorna@chandler.com"; // Set this to your email address

echo __LINE__."<br />\n";
$mail->AddAddress("recipient@someplace.com", "Person's Name"); // Defines who it's going to 

echo __LINE__."<br />\n";
$mail->Subject = "Set this to the email subject you want";

echo __LINE__."<br />\n";
$mail->Body = "Set this to the body of the email";

echo __LINE__."<br />\n";
$mail->AddAttachment("path/to/your/file.csv"); // Adds the attachment

echo __LINE__."<br />\n";
if(!$mail->Send()) { // Sends the email
  echo __LINE__."<br />\n";
  echo "Mailer Error: " . $mail->ErrorInfo; // Shows you an error if it fails for some reason
} else {
  echo __LINE__."<br />\n";
  echo "Message sent!"; // Shows you a success message if it succeeds
}

Open in new window


Adjust the properties for your CSV file path and path to phpmailer and then run the script and show us what the output is. The __LINE__ is a magic constant that will evaluate to the current line number in the code, so you should be able to see how far the code is progressing (if it is at all), and the top 2 lines should turn on the displaying of any errors that might be happening.

If you STILL get a blank page after that, then the PHP script isn't running, which could mean you have a syntax error or something.
1
Lorna ChandlerAuthor Commented:
I didn't know there was a way to check for errors on the server!! I fixed the errors and it is sending an email, but there is no attachment. I'm certain I don't have the correct path to the attachment, but I'm not entirely sure what the path is going to be since the file is only created after the download page is ran. would the path be download/users.csv??
0
gr8gonzoConsultantCommented:
I don't see any code in the files that you attached that would create a permanent file in the filesystem. You have this code:

header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename=users.csv');
$output = fopen('php://output', 'w');
fputcsv($output, array('No', 'First Name', 'Last Name', 'Phone Number', 'Email', 'Address'));

if (count($users) > 0) {
    foreach ($users as $row) {
        fputcsv($output, $row);
    }
}

Open in new window


...which is all just communication with the browser.

header('Content-Type: text/csv; charset=utf-8');
...tells the browser, "The response you're about to get from me is UTF-8 encoded and contains CSV data."

header('Content-Disposition: attachment; filename=users.csv');
...tells the browser, "Instead of displaying the response I'm sending you, you should prompt the user to save the data as a file on their computer, and I'm suggesting that you use the filename users.csv."

$output = fopen('php://output', 'w');
...is PHP opening up the normal output destination, so that when it uses the fputcsv() commands, instead of writing to a file, it's just sending the resulting line of CSV data to the output (straight to the browser). Basically the same as if you used echo or print or any of those commands to display something.

So the data you're generating is never saved anywhere on the filesystem, so you can't email something that's not there.

If you want to generate the CSV data and store it into a file and THEN send it to the browser and also email it, you would change your fopen() function to indicate the filename where you want to save things. For example:

header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename=users.csv');

// Create lorna_users.csv on the server
$output = fopen('lorna_users.csv', 'w');
fputcsv($output, array('No', 'First Name', 'Last Name', 'Phone Number', 'Email', 'Address'));
foreach ($users as $row) { fputcsv($output, $row); }
fclose($output);

// Send the entire contents of lorna_users.csv to the output, so the browser has its data to download
readfile("lorna_users.csv");

// Here's where you would put the PHPMailer code
include_once("......");
$mail = new PHPMailer();
...etc...
$mail->AddAttachment("lorna_users.csv", "users.csv"); // Takes the data from lorna_users.csv, but calls the attached file "users.csv"
...etc...

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Lorna ChandlerAuthor Commented:
YOU ARE BEAUTIFUL!!!!!!! THANK YOU SOOOO MUCH!!!! That worked!! thank you again!!
0
gr8gonzoConsultantCommented:
Glad to hear it worked. Just make sure you close the question by marking the appropriate comment as the solution. Thanks!
0
Lorna ChandlerAuthor Commented:
Thank you for all your help!!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Development

From novice to tech pro — start learning today.