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 = '[email protected]';

$headers = 'From: [email protected]' . "\r\n" .
    'Reply-To: [email protected]' . "\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
https://forums.phpfreaks.com/topic/66371-solved-adding-attachment-to-email/
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 = '[email protected]';
  $mail->FromName = '[email protected]';
  $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('[email protected]');
     $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.

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.