Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial


Use PHP to automatically email a customer after a purchase


I am looking at designing a website to sell an e-book, i know how to setup a shop which uses paypal. but i am not sure how it would work when the customer clicks on my website to buy the book, how would the book be sent to the customer. is there anyway to use php to autosend the book to the customers email address as and when the item is paid.

Thanks for your help.

Chris :)

7 Answers

Phil Rice
Phil Rice
5,536 Points

im not sure how to do what your asking sorry but my first thoughts are how big is your ebook and is it a good idea to send the book via email? I think sending a link to download it would be far better practice.

Id be interested to know how to do what your asking also though.

If chris lightfoot doesn't mind I'm adding this question too :

  • how to send a link for download that is in a simple folder (site.com/files/ebook/1.pdf) but has a long random form too. and the link is available only for 24 hours.[the user can login in his account in the site and request it again] [don't want to use ready systems]

I know these aren't things that can be answered in 1 simple post. but if just tell the topics to cover (no matter in which part (configuring linux, .htaccess ,....)) I'll go for it ;)

Phil- The e-book will be 1 to 2mb.

Mohammad- Thats fine :)

chris lightfoot ,

Yes, you could email a link to where the ebook is, but since you asked how to send it via email, try something like this.

// instatiate mailer class
$mail = new PHPMailer();

// mail settings
$mail->Host = 'mail.yourdomain.com';
$mail->Port = (25 or 587, or whatever your smtp port is);
$mail->SMTPAuth   = true; 
$mail->SetFrom('mailer@yourdomain.com', 'Display Name';
$mail->Username   = "sendingemailaddresslogin@yourdomain";
$mail->Password   = "passwordforsendingemailaddress";
$mail->Subject = 'Subject';
$mail->Body = 'Body';
$mail->AddAttachment('path_to_uploaded_file', 'filename.pdf', "base64", "application/pdf");  // Path, Name, Encoding, Mime type
if(!$mail->Send()) {
    echo "Mailer Error: " . $mail->ErrorInfo;
} else {
    echo "Message with attachment sent!";
Randy Hoyt
Randy Hoyt
Treehouse Guest Teacher

Right now, I assume you have some kind of confirmation page that says "thanks for buying." You'd want to send the email and then redirect to that confirmation page. (It works just like the Contact Form we build in my Simple PHP Application tutorials, but your form would do more than just send an email.) The code Gordon mentioned above would go in that page.

Does that help?

I guess randy you could direct them to the confirmation page then have a url to the pdf document.

Randy Hoyt
Randy Hoyt
Treehouse Guest Teacher

An email would be nice if they need to be able to download it in the future. I know I often buy ebooks and then wait a few days before I download them. If I get a new device, I've even gone back after a year to try to download them again ... some sites let me do that, others don't.