Lorna Chandler
asked on
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.
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.
ASKER
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...
ASKER
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
download2.php
createcsv2.php
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:
...and then add this line of code:
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.
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);
...and then add this line of code:
echo __LINE__."<br />\n";
...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
}
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.
ASKER
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??
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
YOU ARE BEAUTIFUL!!!!!!! THANK YOU SOOOO MUCH!!!! That worked!! thank you again!!
Glad to hear it worked. Just make sure you close the question by marking the appropriate comment as the solution. Thanks!
ASKER
Thank you for all your help!!
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