R
R
Raily2014-07-22 14:39:56
PHP
Raily, 2014-07-22 14:39:56

Why does a corrupted file come as an attachment when sending an email via PHP?

I'm trying to send an email with an attachment (a .pdf file) using the PHP mail() function.
The letter is sent, but the pdf file arrives damaged, when you try to open it, a message appears that the file is damaged (for example, sent by e-mail as an attachment and not decoded correctly). What could be wrong? I use the following code (found on the Internet):

function send_mail($to, $theme, $file_pdf) {   	
  $from = "[email protected]";
  $f         = fopen($file_pdf,"rb"); 
    $un        = strtoupper(uniqid(time())); 
    $head      = "From: $from\n"; 
    $head     .= "To: $to\n"; 
    $head     .= "Subject: $theme\n"; 
    $head     .= "X-Mailer: PHPMail Tool\n"; 
    $head     .= "Reply-To: $from\n"; 
    $head     .= "Mime-Version: 1.0\n"; 
    $head     .= "Content-Type:multipart/mixed;"; 
    $head     .= "boundary=\"----------".$un."\"\n\n"; 
    $zag       = "------------".$un."\nContent-Type:text/html;\n"; 
    $zag      .= "Content-Transfer-Encoding: 8bit\n\n$text\n\n"; 
    $zag      .= "------------".$un."\n"; 
    $zag      .= "Content-Type: application/octet-stream;"; 
    $zag      .= "name=\"".basename($file_pdf)."\"\n"; 
    $zag      .= "Content-Transfer-Encoding:base64\n"; 
    $zag      .= "Content-Disposition:attachment;"; 
    $zag      .= "filename=\"".basename($file_pdf)."\"\n\n"; 
    $zag      .= chunk_split(base64_encode(fread($f,filesize($file_pdf))))."\n"; 
     
    return @mail($to, $theme, $zag, $head); 
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Entelis, 2014-07-22
@DmitriyEntelis

Use swiftmailer.org and you'll be fine.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question