Jump to content

Upload file and email


wiggst3r

Recommended Posts

Hi

 

I have a form, which when users fill out the results are sent to me in an email.

I'm wanting to add a filed whereby someone can add a file when they fill out the form and this file will be sent along with the email as an attachment.

 

Does anyone know how I can do this?

 

Any examples of code that people have used before?

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/155324-upload-file-and-email/
Share on other sites

Hi

 

Thanks for the replies.

 

How could I check (uing the if/switch) in my code:

 

<?php

function is_valid_email($email) 
{
  $result = TRUE;
  if(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$", $email)) {
    $result = FALSE;
  }
  return $result;
}

if(isset($_POST['submit']))
{
    
$error = '';
$name = mysql_real_escape_string($_POST['name']);
$email = mysql_real_escape_string($_POST['email']);
$attached = $_POST['attached'];

   if(trim($name) == '')
   {
        echo   '<p>Please enter your name</p>';
   }

   if(trim($email) == '' || !is_valid_email($email))
   {
        echo   '<p>Please enter a valid email address</p>';
   }



else
{

	$target_path = "temp/";

	$target_path = $target_path . basename( $_FILES['attached']['name']);
	$target_path = $target_path . basename( $_FILES['attached']['type']);  
	$target_path = "temp/";


	$target_path = $target_path . basename( $_FILES['attached']['name']); 

	if(move_uploaded_file($_FILES['attached']['tmp_name'], $target_path)) {
	    echo "The file ".  basename( $_FILES['attached']['name']). 
	    " has been uploaded";
	} 
	else
	{
	    echo "There was an error uploading the file, please try again!";
	}

	$mime_type = "application/msword";

	//Begin the message.  Be sure to change this how you want it.
	$message = "Please find my CV attached";
	$headers = "From: $email";
	$to = "[email protected]";
	$subject = "Job Application";

	$from = $email;

	mail($to,$subject,$message,$headers);
	echo "<p>Thank you for submitting your details.</p>";

}

}

?>

All I'd like to do is make sure that the file uploaded is either a word doc, jpg, gif, png or pdf. It can simply be a PDF or .doc file really, not fussed about the image files.

 

I'm still not sure how I'd send the file as an attachment as I'm aware of mime-type issues and setting the right mime-type depending on the file uploaded.

 

All help will be welcomed.

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.