Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Email form + attachment

Status
Not open for further replies.

leeboycymru

Programmer
Jan 23, 2007
185
GB
I had a simple form sending an email with the users details. I want to now add a file attachment, and have looked in google and there seems to be answers, but they are very difficult to follow.

This is my code:

Code:
<form method="post" action="sendmail.php">
      Name: <br/>
      <input name="name" type="text" id="Contact" size="30" />
      <br/>
      <br/>
      Company Name: <br/>
      <input name="company" type="text" id="Contact1" size="30" />
      <br/>
      <br/>
      Telephone: <br/>
      <input name="telephone" type="text" id="Contact2" size="30" />
      <br/>
      <br/>
      Email: <br/>
      <input name="email" type="text" id="Contact3" size="30" />
      <br/>
      <br/>
      Message:<br/>
      <textarea id="Contact4" name="message" rows="10" cols="40"></textarea>
      <br/>
      <br/>
	  Attachment:<br/>
	  <input type="file" name="FileAttachment" />
	  <br/>
	  <br/>
      <input id="SubContact" type="submit" />
    </form>

and here's the php, can somebody help me fill in the blanks please.

Code:
<?
$name = $_REQUEST['name'];
$company = $_REQUEST['company'];
$telephone = $_REQUEST['telephone'];
$email = $_REQUEST['email'];
$message = $_REQUEST['message'];
$attachment = $_FILES['FileAttachment'];
$message = "Message submitted at ".date("d/m/y - h:m")."\r\n
By: $name\r\n
Company Name: $company\r\n
Phone: $telephone\r\n
Message: \r\n $message";
if (empty($_REQUEST['email']) || empty($_REQUEST['name']) || empty($_REQUEST['message']) || empty($_REQUEST['company'])) {
?>
<html>
<head>
<title>Email Error</title>
</head>
<body>
<h1>Email Error</h1>
<p>
Sorry, it appears that you have forgotten to enter either your name, your email address or your message.</p>
<p>Please press the <strong>BACK</strong> button in your browser and try again.</p>
</body>
</html>
<?
}
else {
mail("info@hobbsvalve.co.uk", "Message from website",
	$message, "From: $name <$email>");
header ("Location: [URL unfurl="true"]http://www.hobbsvalve.co.uk/thankyou.htm");[/URL]
}
?>
 
the easy answer is to use phpmailer. this is a freely downloadable class.
if you want to do the whole thing yourself, it's more than filling in the blanks. you need to get to grips with mime types and encoding the content. not difficult but easier to use phpmailer. there is an FAQ on this in the forum.
 
Hi,

I downloaded the php mailer and worked through it and I dont think it will do for what i need to do.

Isnt the phpmailer a package that will allow me to send emails to people with attachments, when what I need is to allow people to attach an attachment to a form on the contact us page and send that to my client as a website enquiry.

I dont think the phpmailer from what I can see is what I need. Is this true?

Lee
 
yup. it is what you need!

something like this (assuming that the file control on your form is called userfile)

Code:
	$mail = new PHPMailer();
	
	$mail->From     = "";
	$mail->FromName = "";
	$mail->AddAddress(''); //add to address
	if (isset($_FILES['userfile']) && $_FILES['userfile']['error'] == 0){
		$mail->AddAttachment($_FILES['userfile']['tmp_name'],$_FILES['userfile']['name']);
	}
	$mail->Body    = ""; //html body of message (or plain text if no AltBody being supplied)
    $mail->AltBody = ""; //text body of message
	$mail->Subject = "";
	$mail->WordWrap = 75;
	$mail->isHTML(); //if html mail
	$mail->isMail(); //use built in php mail() function
	if(!$mail->Send()){
           die ('error sending mail');
	}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top