Jump to content

[SOLVED] Adding Attachment to Email?


j.g.

Recommended Posts

Hello List-

 

This is my first time attempting this; but I'd like to add a file selected by the user to the email that is sent to the recipient...  I've got the basics done where it's allowing the input for the name, email, phone, and browsing to select a file; and it's sending an email w/ the name, email, and phone in it, but I don't know how to incorporate the attaching of the email to this email.

 

Here's my code:

 

session_start();
require_once("$DOCUMENT_ROOT/includes/form_functions.php");
require_once("$DOCUMENT_ROOT/includes/form_processor.php");
  
$to = 'xxx@xxx.net';

$headers = 'From: info@xxx.com' . "\r\n" .
    'Reply-To: info@xxx.com' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

$subject = "Employment Form";

$items = array('name', 
                'email',
                'phone',
                'resume file');
                
$required = array();                

if( $_POST["submit"] == "Submit" )
{

    $required = array( "name"	=>	"text",           						
         	     "email"   =>	"text",
         	     "phone"  =>	"text"
		    );
		       	          
   $error_msg = form_processor( $required, $_POST, $num_results = 1 );

   if( !$error_msg )
  {
	  
      $message = '';
      foreach($items as $i => $item)
      {
          $message .= ucfirst($item).": ".$_POST[$item]."\n\r";
      }
      $message .= "Sent: ".date('Y-m-d h:i:s')."\n\r";
  
      if (mail($to, $subject, $message, $headers))
      {
          header ("Location: /thankyouform.php");
  exit();
      }
   }
}
  
require_once("header.php")

 

Any help or direction that you could give me to get this working is greatly appreciated b/c I just cannot get this working!

 

Thanks,

-j.g.

Link to comment
Share on other sites

Thanks for the replies!

 

I ended up going the phpmailer route; but, am still at the same spot -- sending email w/ name, phone, and email, but file is not attached.

 

Here's my line for it (what's wrong w/ it??):

$mail->AddAttachment($_FILES["file_name"]["name"]);

 

 

And here's my input field for the file:

<div align="left">Attach Resume

  <input TYPE='FILE' NAME='file_name' "SIZE='40'/>

</div>

 

Entire php code:

  session_start();
  require_once("$DOCUMENT_ROOT/includes/form_functions.php");
  require_once("$DOCUMENT_ROOT/includes/form_processor.php");
  
  //set up the emailer
  require_once("$LIBRARY_ROOT/class.phpmailer.php");
  
  $mail = new PHPMailer();

  $mail->From = 'info@xxx.com';
  $mail->FromName = 'info@xxx.com';
  $mail->Subject = 'Employment Form Submission';

$items = array('name', 
                'email',
                'phone',
                'resume file');
                
$required = array();                

if( $_POST["submit"] == "Submit" )
{

   $required = array( "name" =>	"text",           						
		   "email"  =>	"text",
		   "phone" =>	"text"
      	          );
		       	          
   $error_msg = form_processor( $required, $_POST, $num_results = 1 );

   if( !$error_msg )
   {
	  
     $body = '';
     foreach($items as $i => $item)
     {
        $body .= ucfirst($item).": ".$_POST[$item]."\n\r";
     }
     $body .= "Sent: ".date('Y-m-d h:i:s')."\n\r";
        
     //send the email
     $mail->Body = $body;
     $mail->AddAddress('xxx@xxx.net');
     $mail->AddAttachment($_FILES["file_name"]["name"]);
        
     if ($mail->Send())
     {
        header ("Location: /thankyou.php");
        exit();

     }
     else
     {
          "There has been an error sending your email to TERM Billing.<br>";
     }
   }
}
  
require_once("header.php")

 

Thanks much!

-j.g.

Link to comment
Share on other sites

I've even tried referring to the file directly by using it's 'tmp' value, but still not attaching the file...

 

$mail->AddAttachment($_FILES["file_name"]["tmp"]);

 

 

Does anyone see what I'm doing wrong?  ???

 

 

Thanks again!

-j.g.

Link to comment
Share on other sites

Thanks for the help!

 

So if anyone else runs into this same issue, this was resolved by tweaking the 'AddAttachment' line:

 

$mail->AddAttachment($_FILES["file_name"]["tmp_name"],$_FILES["file_name"]["name"]);

 

Hope that helps someone else in the future!

-j.g.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.