Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 174
  • Last Modified:

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

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";



Justin Malone
Justin Malone
2 Solutions
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";
Marcus BointonCommented:
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.
Justin MaloneSystem AdministratorAuthor Commented:
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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now