0

i am using php mail function to sent attachment, but its not working. Here is my code:

     $headers = array(
        "Mime-Version: 1.0",
        "Content-Type: text/html; charset=charset=UTF-8",
        "From: Test <myemail>",
        "Content-Disposition: attachment; filename=\"" . $_SERVER['DOCUMENT_ROOT']."/project_name/test.txt" . "\"\r\n"; // For Attachment
    );    
    $headers = join("\r\n", $headers);

    mail($to, $subject, $body, $headers);

Email sent successfully and file is attached, but the content is hidden.

Maha Dev
  • 3,915
  • 2
  • 31
  • 50

4 Answers4

0

I recommend you to use PHPMailer.

To use PHPMailer:

  • Download the PHPMailer script from here: http://github.com/PHPMailer/PHPMailer
  • Extract the archive and copy the script's folder to a convenient place in your project.
  • Include the main script file -- require_once('path/to/file/class.phpmailer.php');

Now use as :

$email = new PHPMailer();
$email->From      = 'you@example.com';
$email->FromName  = 'Your Name';
$email->Subject   = 'Message Subject';
$email->Body      = $bodytext;
$email->AddAddress( 'destinationaddress@example.com' );
$file_to_attach = 'PATH_OF_YOUR_FILE_HERE';
$email->AddAttachment( $file_to_attach , 'nameOfFile.pdf' );
return $email->Send();   
Gyan
  • 498
  • 6
  • 10
0

Your content type should be

"Content-Type:  multipart/mixed; charset=charset=UTF-8",

see this answer, this can guide you solve your problem.

I also have a big doubt, if it is correct

 "Content-Disposition: attachment; filename=\"" . $_SERVER['DOCUMENT_ROOT']."/project_name/test.txt" . "\"\r\n"; // For Attachment

combination of single and double quote does not seems to good. Can you please change it to

 "Content-Disposition: attachment; filename=\" . $_SERVER['DOCUMENT_ROOT'] . "/project_name/test.txt" . "\"\r\n";

or try using single quote as you are not parsing any variable.

Prafulla Kumar Sahu
  • 9,321
  • 11
  • 68
  • 105
0

If you want to write your own code and don't want to implement the mail libraries. Please do it like this

$handle = fopen($file_name, "r");
$content = fread($handle, filesize($file_name));
fclose($handle);
$encoded_content = chunk_split(base64_encode($content));

$boundary = md5("foobar");
$message = "I have some message";
//header

$headers = "MIME-Version: 1.0\r\n"; 
$headers .= "Content-Type: multipart/mixed; boundary = $boundary\r\n\r\n"; 

//plain text 
$body = "--$boundary\r\n";
$body .= "Content-Type: text/plain; charset=ISO-8859-1\r\n";
$body .= "Content-Transfer-Encoding: base64\r\n\r\n"; 
$body .= chunk_split(base64_encode($message)); 

//attachment
$body .= "--$boundary\r\n";
$body .="Content-Type: $file_type; name=".$file_name."\r\n";
$body .="Content-Disposition: attachment; filename=".$file_name."\r\n";
$body .="Content-Transfer-Encoding: base64\r\n";
$body .="X-Attachment-Id: ".rand(1000,99999)."\r\n\r\n"; 
$body .= $encoded_content;

mail($to, $subject, $body, $headers);
simurgrai
  • 36
  • 2
0
$to = "YOUR EMAIL ID";
$from = "Website <website@mydomain.com>";
$subject = "Test Attachment Email";

$separator = md5(time());

// carriage return type (we use a PHP end of line constant)
$eol = PHP_EOL;

// attachment name
$filename = "document.pdf";

//$pdfdoc is PDF generated by FPDF
$attachment = chunk_split(base64_encode($pdfdoc));

// main header
$headers  = "From: ".$from.$eol;
$headers .= "MIME-Version: 1.0".$eol; 
$headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\"";

// no more headers after this, we start the body! //

$body = "--".$separator.$eol;
$body .= "Content-Transfer-Encoding: 7bit".$eol.$eol;
$body .= "This is a MIME encoded message.".$eol;

// message
$body .= "--".$separator.$eol;
$body .= "Content-Type: text/html; charset=\"iso-8859-1\"".$eol;
$body .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
$body .= $message.$eol;

// attachment
$body .= "--".$separator.$eol;
$body .= "Content-Type: application/octet-stream; name=\"".$filename."\"".$eol; 
$body .= "Content-Transfer-Encoding: base64".$eol;
$body .= "Content-Disposition: attachment".$eol.$eol;
$body .= $attachment.$eol;
$body .= "--".$separator."--";

// send message
if (mail($to, $subject, $body, $headers)) {
echo "mail send ... OK";
} else {
echo "mail send ... ERROR";
}
Palani Samy
  • 169
  • 9