Jump to content

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 = "example@gmail.com";
	$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.

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.