2

With this code:

  $Message = 'TEXT';
  $boundary = "---";
  $headers = "From: $from\nX-Mailer: Carline Server www.carline.ru";
  $headers .= "Content-Type: multipart/mixed; boundary=\"$boundary\"";
  $body = "--$boundary\n";
  $body .= "Content-type: text/html; charset='utf-8'\n";
  $body .= "Content-Transfer-Encoding: quoted-printable\n\n";
  $body .= "Content-Disposition: attachment; filename==?utf-8?B?".base64_encode($_FILES['photo1']['name'])."?=\n\n";
  $body .= $Message."\n";
  $body .= "--$boundary\n";
  $text = file_get_contents($_FILES['photo1']['tmp_name']);
  $body .= "Content-Type: application/octet-stream; name==?utf-8?B?".base64_encode($_FILES['photo1']['name'])."?=\n"; 
  $body .= "Content-Transfer-Encoding: base64\n";
  $body .= "Content-Disposition: attachment; filename==?utf-8?B?".base64_encode($_FILES['photo1']['name'])."?=\n\n";
  $body .= chunk_split(base64_encode($text))."\n";
  $body .= "--".$boundary ."--\n";


  mail($AdminEmail, "RE:", $body, $headers);

It sends a file-content in the message body

Content-Type: application/octet-stream; name==?utf-8?B?MDAwMDU3ODcuanBn?=
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename==?utf-8?B?MDAwMDU3ODcuanBn?=

/9j/4AAQSkZJRgABAQEASABIAAD/2wBDAAIBAQIBAQICAgICAgICAwUDAwMDAwYEBAMFBwYHBwcG
BwcICQsJCAgKCAcHCg0KCgsMDAwMBwkODw0MDgsMDAz/2wBDAQICAgMDAwYDAwYMCAcIDAwMDAwM
DAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAz/wAARCAGQAZADASIA
...

How to change this code for sending file as attachment?

3 Answers 3

1

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

Send Multi attachment email

<?php
function multi_attach_mail($to, $files, $sendermail){
// email fields: to, from, subject, and so on
$from = "Files attach <".$sendermail.">";
$subject = date("d.M H:i")." F=".count($files);
$message = date("Y.m.d H:i:s")."\n".count($files)." attachments";
$headers = "From: $from";

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

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

// multipart boundary
$message = "--{$mime_boundary}\n" . "Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n";

// preparing attachments
for($i=0;$i<count($files);$i++){
    if(is_file($files[$i])){
        $message .= "--{$mime_boundary}\n";
        $fp =    @fopen($files[$i],"rb");
    $data =    @fread($fp,filesize($files[$i]));
                @fclose($fp);
        $data = chunk_split(base64_encode($data));
        $message .= "Content-Type: application/octet-stream; name=\"".basename($files[$i])."\"\n" .
        "Content-Description: ".basename($files[$i])."\n" .
        "Content-Disposition: attachment;\n" . " filename=\"".basename($files[$i])."\"; size=".filesize($files[$i]).";\n" .
        "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
        }
    }
$message .= "--{$mime_boundary}--";
$returnpath = "-f" . $sendermail;
$ok = @mail($to, $subject, $message, $headers, $returnpath);
if($ok){ return $i; } else { return 0; }
}
?>
0

An example of header construction

    $header = "From: ".$from_name." <".$from_mail.">\r\n";
    $header .= "Reply-To: ".$replyto."\r\n";
    $header .= "MIME-Version: 1.0\r\n";
    $header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
    // content type: mixed or related ( better mixed for attachments
    $header .= "This is a multi-part message in MIME format.\r\n";
    $header .= "--".$uid."\r\n";
    $header .= "Content-type:text/plain; charset=iso-8859-1\r\n";
    $header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
    $header .= $message."\r\n\r\n";
    $header .= "--".$uid."\r\n";
    $header .= "Content-Type: application/octet-stream; name=\"".$filename."\"\r\n";  
    // use different content types here
    $header .= "Content-Transfer-Encoding: base64\r\n";
    $header .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";
    $header .= $content."\r\n\r\n";
    $header .= "--".$uid."--";
0

You should be using pre-written libraries (e.g. http://swiftmailer.org/) to send emails, esp. the ones with attachments, whether inline or not.

Constructing valid and cross-mail client supported message is considerably advance task, esp. if you have never done this before. So why spend time re-inventing a weel when someone did the job for you already?

Put this question from the other end and if you want to build your own library, analyze what's already been done in SwiftMailer and use those snippets where needed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.