FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Adding field to message data





Shike
Ok, I've got an email form, sending information correctly, except I cannot figure out how to add data from an extra field.

My extra Field is a personal name field with name and id "person".

here's my code. (I modified code from here by: Kevin Yank)
Code:
<?php
// Read POST request params into global vars
$from    = $_POST['email'];;
$subject = $_POST['system'];
$message = $_POST['description'];


// Obtain file upload vars
$fileatt      = $_FILES['fileatt']['tmp_name'];
$fileatt_type = $_FILES['fileatt']['type'];
$fileatt_name = $_FILES['fileatt']['name'];

$headers = "From: $from";

if (is_uploaded_file($fileatt)) {
  // Read the file to be attached ('rb' = read binary)
  $file = fopen($fileatt,'rb');
  $data = fread($file,filesize($fileatt));
  fclose($file);

  // Generate a boundary string
  $semi_rand = md5(time());
  $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
 
  // Add the headers for a file attachment
  $headers .= "\nMIME-Version: 1.0\n" .
              "Content-Type: multipart/mixed;\n" .
              " boundary=\"{$mime_boundary}\"";

  // Add a multipart boundary above the plain message
  $message = "This is a multi-part message in MIME format.\n\n" .
             "--{$mime_boundary}\n" .
             "Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
             "Content-Transfer-Encoding: 7bit\n\n" .
             $message . "\n\n";

  // Base64 encode the file data
  $data = chunk_split(base64_encode($data));

  // Add file attachment to the message
  $message .= "--{$mime_boundary}\n" .
              "Content-Type: {$fileatt_type};\n" .
              " name=\"{$fileatt_name}\"\n" .
              //"Content-Disposition: attachment;\n" .
              //" filename=\"{$fileatt_name}\"\n" .
              "Content-Transfer-Encoding: base64\n\n" .
              $data . "\n\n" .
              "--{$mime_boundary}--\n";
}

// Send the message
$ok = @mail("submit@shike.frih.net", $subject, $message, $headers);
if ($ok) {
  echo "<p>Mail sent!</p><a href='http://shike.frih.net' target='_self'>Return Home</a>";
} else {
  echo "<p>Mail could not be sent. Sorry!</p>";
}
?>


any help would be appreciated
Kaneda
Not quite sure what you mean by "data from extra field", but if you mean that you have another POST parameter, which contains the actual name of the person sending (as opposed to the email address of him/her), and you want this to be used as the sender, you can do it like this:
(have to use quote-tag here, in order to make some stuff bold...)

Quote:
...
$from = $_POST['email'];;
$subject = $_POST['system'];
$message = $_POST['description'];
$name = $_POST['person'];
if (!ctype_print($name) || !ctype_print($from) || !ctype_print($subject)) {
die("Attempt at email injection");
}


...


Quote:
...
$headers = "From: $name <$from>";

if (is_uploaded_file($fileatt)) {
...


The ctype_print bit ensures that the user didn't add non-printable characters (like \n \r etc.) in the name, from and subject fields - since that's a pretty sure sign of an attempt to use your server as spamming tool.
Shike
Kaneda,

That is perfect. Thanks.
Related topics
I'm having problems understanding keys
Build an online dictionary by PHP/MySQL
No Messages in this thread on Aug 27th???
[PhP] News Posting Tutorial (code, actually ^^')
good site to learn php or asp?
PHP Tutorials
My post gone
Twitpic Share Photos On Twitter
multiplayer: use db, sockets or others?
Problem adding text to Longtext field
problem in saving and retrieving data in longtext format
Simple php & mysql script
Database connection class!
Sending Form Data to E-mail
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.