how do i get a form to upload a file to be attached to an e-mail

Posted on 2005-03-01
Medium Priority
Last Modified: 2010-08-05
i have a pre-bult form that can be filled out and submited. when it get submited it sends a text e-mail with the information of all the fields in it. i want users to be able to upload a .doc file that gets attached to the e-mail that gets sent out. im pretty sure i have the code for it i just dont know how to set it up. this is part of whats in my formmail.php file

// check for a file if there is a file upload it

if ($file_name) {

   if ($file_size > 0) {

      if (!ereg("/$", $path_to_file))

         $path_to_file = $path_to_file."/";

      $location = $path_to_file.$file_name;

      if (file_exists($path_to_file.$file_name))

         $location .= ".new";



      $content .= "Uploaded File: ".$location."\n";



Question by:Justin Malone
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Accepted Solution

aksteve earned 1600 total points
ID: 13434887
I would use PHPmailer



$mail = new PHPMailer();

$mail->IsSMTP();                                   // send via SMTP
$mail->Host     = "smtp1.site.com;smtp2.site.com"; // SMTP servers
$mail->SMTPAuth = true;     // turn on SMTP authentication
$mail->Username = "jswan";  // SMTP username
$mail->Password = "secret"; // SMTP password
$mail->From     = "from@email.com";
$mail->FromName = "Mailer";
$mail->AddAddress("josh@site.com","Josh Adams");
$mail->AddAddress("ellen@site.com");               // optional name

$mail->WordWrap = 50;                              // set word wrap
$mail->AddAttachment($file);      // attachment

$mail->Subject  =  "Here is the subject";
$mail->Body     =  "This is the body";

   echo "Message was not sent <p>";
   echo "Mailer Error: " . $mail->ErrorInfo;

echo "Message has been sent";
LVL 25

Assisted Solution

by:Marcus Bointon
Marcus Bointon earned 400 total points
ID: 13437509
Before you can email the file, you need to handle the upload. There's lots of docs and examples here:


Definitely use PHPMailer, just as aksteve says.

Author Comment

by:Justin Malone
ID: 13530745
Wow i completely forgot about this question, I have just been bussy. points to aksteve, and ill sort it all out later. thanks for the help

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
This article discusses how to implement server side field validation and display customized error messages to the client.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question