Dewey
Dewey

Reputation: 1

PHP mail delivers attachment or message, but not both

The PHP below will attach the file from the server, but will not allow me to edit the message input for the body of the email.

<?php
  $fileatt = './dwrdocuments/dwr.fdf'; // Path to the file
  $fileatt_type = "application/octet-sdiveam"; // File Type
  $fileatt_name = date(mdy_his).'_dwr.fdf'; 
  $email_from = $_POST['From']; // Who the email is from
  $email_subject = 'DWR Submittal'; // The Subject of the email
  $email_txt = $_POST['Comments']; // Message that the email has in it

  $email_to = '[email protected]'; // Who the email is to

  $headers = "From: ".$email_from;

  $file = fopen($fileatt,'rb');
  $data = fread($file,filesize($fileatt));
  fclose($file);

  $semi_rand = md5(time());
  $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";

  $headers .= "\nMIME-Version: 1.0\n" .
  "Content-Type: multipart/mixed;\n" .
  " boundary=\"{$mime_boundary}\"";

  $email_message .= "This is a multi-part message in MIME format.\n\n" .
  "--{$mime_boundary}\n" .
  "Content-Type:text/html; charset=\"iso-8859-1\"\n" .
  "Content-divansfer-Encoding: 7bit\n\n" .
  $email_message . "\n\n";

  $data = chunk_split(base64_encode($data));

  $email_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";

  $ok = @mail($email_to, $email_subject, $email_message, $headers);
  header ("Location: ../confirm.html");  
?>

If I do input a message for the body of the email by replacing:

  $email_message . "\n\n";

the attachment does not come through, but the message will... along with some gibberish

--==Multipart_Boundary_xd05fe3fbc7afe814087a952a9c31f1fax Content-Type: application/octet-sdiveam; name="061711_080900_dwr.fdf" Content-Transfer-Encoding: base64 JUZERi0xLjIKJe+/ve+/ve+/ve+/vQoxIDAgb2JqCjw8IAovRkRGIDw8IC9GaWVsZHMgWyA8PC9U KFdvcmtfRGF0ZSkvVihramgpPj48PC9UKE1haW50ZW5hbmNlX0FyZWEpL1YoKT4+PDwvVChSb3V0 ZV9OdW1iZXIpL1YoKT4+PDwvVChEdWVfRGF0ZSkvVigyMDExLTA2LTE2IDIyOjI2OjAxKT4+PDwv VChSb2FkX1NlZ21lbnRfSUQpL1YoKT4+PDwvVChDcm9zc19TZWN0aW9uX1Bvc2l0aW9uKS9WKCk+ Pjw8L1QoTG9jYWxfTmFtZSkvVigpPj48PC9UKEZhY2lsaXR5X05hbWUpL1YoKT4+PDwvVChGYWNp bGl0eV9BZGRyZXNzKS9WKCk+Pl0gCi9GIChodHRwOi8vY2FnZWRuYXRpb24uY29tL2pjcy9waHAv ZHdyX2Zvcm0ucGRmKSAvSUQgWyA8ZjIxODgyZTJmOGQxOGJjYjY5OGRhYzlmNTllNzIwYWM+Cl0g Pj4gCj4+IAplbmRvYmoKdHJhaWxlcgo8PAovUm9vdCAxIDAgUiAKCj4+CiUlRU9GCg== 

I would like to include a body to the email for comments and notes.

Upvotes: 0

Views: 693

Answers (2)

NoBugs
NoBugs

Reputation: 9496

Have you tried ending headers with \r\n?

http://php.net/manual/en/function.mail.php

Upvotes: 0

Maerlyn
Maerlyn

Reputation: 34105

Why don't you use a mailer library? There are a lot out there, like Swiftmailer, PHPMailer or Zend_Mail. You'd save yourself the headache of dealing with the email details.

Upvotes: 1

Related Questions