4

I'm having a problem with sending attachments in php mail. The problem is that the email appears to send and is received, but isn't recognized as an attachment. If I go into the source of the mail, I can see that it's sending the base64 and includes the attachment name as it should so I'm confused why it wouldn't be recognized by the client. Any ideas?

$uid = md5(uniqid(time()));
$headers = "From: ".$from_name." <".$mailto.">\n";
$headers .= "Reply-To: ".$from_mail."\n";
$headers .= "MIME-Version: 1.0\n";
$headers .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\n";
$headers .= "This is a multi-part message in MIME format.\n";
$headers .= "--".$uid."\n";
$headers .= "Content-type:text/plain\n";
$headers .= "Content-Transfer-Encoding: 8bit\n";
$headers .= $message."\n";

foreach ($_FILES as $file) {
    $name = $file['name'];
    $handle = fopen($file['tmp_name'], "r");
    $content = fread($handle, $file['size']);
    fclose($handle);
    $content = chunk_split(base64_encode($content));

    $headers .= "--{$uid}\n";
    $headers .= "Content-Type: {$file['type']}; name=\"{$name}\"\n";
    $headers .= "Content-Transfer-Encoding: base64\n";
    $headers .= "Content-Disposition: attachment; filename=\"{$name}\"\n";
    $headers .= $content;
}

$headers .= "--{$uid}--";
$mailto = 'myemail@email.com';
return mail($mailto, $subject, $message, $headers, '-f' . $mailto);
Nisse Engström
  • 4,738
  • 23
  • 27
  • 42
Purify
  • 589
  • 2
  • 11
  • 26

1 Answers1

2

Problem

There are a number of things wrong with your code:

  • Email headers must be terminated by CRLF. You only have LF.
  • The B64-encoded file and the message are added to the headers. They should be sent as message body.
  • There must be an extra CRLF between the message headers and the message body in each part.
  • The body parts must be separated by delimiters that begin with a CRLF.

Email syntax

This is an excerpt of what the structure of a multi-part message body looks like according to RFC 2046. Pay special attention to the CRLF's. (BNF syntax, somewhat simplified.)


multipart-body := [preamble CRLF]
                  dash-boundary CRLF
                  body-part *encapsulation
                  close-delimiter
                  [CRLF epilogue]

dash-boundary := "--" boundary

body-part := MIME-part-headers [CRLF *OCTET]

encapsulation := delimiter
                 CRLF body-part

delimiter := CRLF dash-boundary

close-delimiter := delimiter "--"

The code

Your code could look something like this:

$uid = md5(uniqid(time()));
$headers = "From: ".$from_name." <".$mailto.">\r\n";
$headers .= "Reply-To: ".$from_mail."\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n";

$body = "This is a multi-part message in MIME format.\n";
$body .= "--".$uid."\r\n";
$body .= "Content-type:text/plain\r\n";
$body .= "Content-Transfer-Encoding: 8bit\r\n";
$body .= "\r\n";
$body .= $message;

foreach ($_FILES as $file) {
  $name = $file['name'];
  $handle = fopen($file['tmp_name'], "r");
  $content = fread($handle, $file['size']);
  fclose($handle);
  $content = chunk_split(base64_encode($content));

  $body .= "\r\n--{$uid}\r\n";
  $body .= "Content-Type: {$file['type']}; name=\"{$name}\"\r\n";
  $body .= "Content-Transfer-Encoding: base64\r\n";
  $body .= "Content-Disposition: attachment; filename=\"{$name}\"\r\n";
  $body .= "\r\n";
  $body .= $content;
}

$body .= "\r\n--{$uid}--";
$mailto = 'myemail@email.example';
return mail($mailto, $subject, $body, $headers, '-f' . $mailto);

References

Community
  • 1
  • 1
Nisse Engström
  • 4,738
  • 23
  • 27
  • 42