PHP Sendmail......8O

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
aredden
Forum Commoner
Posts: 29
Joined: Fri May 25, 2007 7:10 am

PHP Sendmail......8O

Post by aredden »

Hey, I appologize in advance, I'm quite noob to PHP, working on a form that emails information, well have that working properly right now, but I'm trying to have the information stored in a TXT file and that file emailed to me as an attachment. It sounds possible, any help would be great. Thanks in advance. 8O

EDIT: If this is possible is it also possible to store the information in a way that all variables can be saved to be recalled by a separate form.
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Post by Grim... »

aredden
Forum Commoner
Posts: 29
Joined: Fri May 25, 2007 7:10 am

Please Help.

Post by aredden »

Got the text document formatted in a way that is acceptable for now, now im faced with attaching it to the sendmail function so that It can be retrived. heres what I have for my code thus far.

Code: Select all

<?php
// Read POST request params into global vars
$to = "me@example.com"
$from    = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];

// Obtain file upload vars
$fileatt      = (file_get_contents('D0K341_.dat'));
$headers = "From: $from";

  $file = fopen($fileatt);
  $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: text/plain;\n" .
              " name=\"D0K341_.dat\"\n" .
              //"Content-Disposition: attachment;\n" .
              //" filename=\"D0K341_.dat\"\n" .
              "Content-Transfer-Encoding: base64\n\n" .
              $data . "\n\n" .
              "--{$mime_boundary}--\n";


// Send the message
$ok = @mail($to, $subject, $message, $headers);
if ($ok) {
  echo "<p>Mail sent! Yay PHP!</p>";
} else {
  echo "<p>Mail could not be sent. Sorry!</p>";
}
?>
Post Reply