Jump to content

Adding Email Attachment to File Upload Code


CWD

Recommended Posts

I am using the W3 Upload File code and need to change it to send the file via attachment to the email. Can anyone help me with this? I've found several different codes that look like they're all written in different styles of php and can't seem to get anything to work. Here is the code I am working with:

<?php//--------------------------Set these paramaters--------------------------// Subject of email sent to you.$subject = 'Quote Form'; // Your email address. This is where the form information will be sent. $emailadd = 'removed email address'; // Where to redirect after form is processed. $url = 'removed URL'; // Where to direct file upload.$file= 'removed URL';// Makes all fields required. If set to '1' no field can not be empty. If set to '0' any or all fields can be empty.$req = '0'; // --------------------------Do not edit below this line--------------------------$text = "Results from form:\n\n"; $space = ' ';$line = '';foreach ($_POST as $key => $value){if ($req == '1'){if ($value == ''){echo "$key is empty";die;}}$j = strlen($key);if ($j >= 20){echo "Name of form element $key cannot be longer than 20 characters";die;}$j = 20 - $j;for ($i = 1; $i <= $j; $i++){$space .= ' ';}$value = str_replace('\n', "$line", $value);$conc = "{$key}:$space{$value}$line";$text .= $conc;$space = ' ';}mail($emailadd, $subject, $text, 'From: '.$emailadd.'');echo '<META HTTP-EQUIV=Refresh CONTENT="0; URL='.$url.'">';{}if ((($_FILES["file"]["type"] == "application/ai")|| ($_FILES["file"]["type"] == "application/postscript")|| ($_FILES["file"]["type"] == "application/cdr")|| ($_FILES["file"]["type"] == "application/dxf")|| ($_FILES["file"]["type"] == "applicationj/eps")|| ($_FILES["file"]["type"] == "application/pdf"))&& ($_FILES["file"]["size"] < 900000))  {  if ($_FILES["file"]["error"] > 0)    {    echo "Return Code: " . $_FILES["file"]["error"] . "<br />";    }  else    {    echo "Upload: " . $_FILES["file"]["name"] . "<br />";    echo "Type: " . $_FILES["file"]["type"] . "<br />";    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";    echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";    if (file_exists("upload/" . $_FILES["file"]["name"]))      {      echo $_FILES["file"]["name"] . " already exists. ";      }    else      {      move_uploaded_file($_FILES["file"]["tmp_name"],      "upload/" . $_FILES["file"]["name"]);      echo "Stored in: " . "upload/" . $_FILES["file"]["name"];      }    }  }else  {  echo "Invalid file";  }?>

Any and all help will be greatly appreciated!! Thank you. Please Note: I removed my email address and url in code above for posting on this forum.

Link to comment
Share on other sites

That code doesn't do anything with attachments, it sends an email first then copies the file to a location on the server.If you want to do any more advanced than sending regular text emails, look into the PEAR Mail package. It makes it much easier to add attachments and other content. This is the function I use to send mail with Mail and Mail_mime, which supports text/html content parts and attachments:

function send_email($to, $text, $html, $headers = array(), $attachments = array()){  // $headers should be an array of k/v pairs  // see http://pear.php.net/manual/en/package.mail.mail-mime.addattachment.php and comments below for information about the attachments array  require_once 'Mail.php';  require_once 'Mail/mime.php';  $mime = new Mail_mime();  $mime->setTXTBody($text);  $mime->setHTMLBody($html);  // check for attachments  for ($i = 0; $i < count($attachments); $i++)  {	$file = isset($attachments[$i]['file']) ? $attachments[$i]['file'] : '';						  // filename or file data	$type = isset($attachments[$i]['type']) ? $attachments[$i]['type'] : 'application/octet-stream';  // content type	$name = isset($attachments[$i]['name']) ? $attachments[$i]['name'] : '';						  // filename to use if file is data instead of a filename	$is_filename = isset($attachments[$i]['is_filename']) ? $attachments[$i]['is_filename'] : true;   // true if file is a filename, false if it's data	$encoding = isset($attachments[$i]['encoding']) ? $attachments[$i]['encoding'] : 'base64';		// file encoding to use	if ($file != '')	  $mime->addAttachment($file, $type, $name, $is_filename, $encoding);  }  $body = $mime->get();  $headers = $mime->headers($headers);  $mail =& Mail::factory('mail');  $mail->send($to, $headers, $body);}

Link to comment
Share on other sites

That code doesn't do anything with attachments, it sends an email first then copies the file to a location on the server.If you want to do any more advanced than sending regular text emails, look into the PEAR Mail package. It makes it much easier to add attachments and other content. This is the function I use to send mail with Mail and Mail_mime, which supports text/html content parts and attachments:
Thank you for your reply. I realize the code doesn't have anything to do with sending email attachments. My client advised that he does not want the attachment sent to his server, but to his email. The code is set up for the submittal form that includes a file upload. I need to add something that will still allow the send form results and send the email attachment along with the form results. Is this even possible? Because everything I've seen so far only allows for sending the attachment and not form results.
Link to comment
Share on other sites

Archived

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

×
×
  • Create New...