Send mail with attachment

2

I want to send an email by clicking on a button or hyperlink with an attachment. The file will find itself in the directory where the page is. I am using this code Send email with attachments in php

It gives an error saying that it does not know function mime_content_type (). I want to use it without forms.

$boundary = "XYZ-".md5(date("dmYis"))."-ZYX";

$path = '/teste.txt';
$fileType = mime_content_type( $path );
$fileName = basename( $path );

// Pegando o conteúdo do arquivo
$fp = fopen( $path, "rb" ); // abre o arquivo enviado
$anexo = fread( $fp, filesize( $path ) ); // calcula o tamanho
$anexo = chunk_split(base64_encode( $anexo )); // codifica o anexo em base 64
fclose( $fp ); // fecha o arquivo

// cabeçalho do email
$headers = "MIME-Version: 1.0" . PHP_EOL;
$headers .= "Content-Type: multipart/mixed; ";
$headers .= "boundary=" . $boundary . PHP_EOL;
$headers .= "$boundary" . PHP_EOL;

$mensagem .= "Content-Type: ". $fileType ."; name=\"". $fileName . "\"" . PHP_EOL;
$mensagem .= "Content-Transfer-Encoding: base64" . PHP_EOL;
$mensagem .= "Content-Disposition: attachment; filename=\"". $fileName . "\"" . PHP_EOL;
$mensagem .= "$anexo" . PHP_EOL;
$mensagem .= "--$boundary" . PHP_EOL;

$para="[email protected]";
$assunto="teste";

mail($para, $assunto, $mensagem, $headers);
    
asked by anonymous 25.11.2014 / 15:04

1 answer

2

The mime_content_type() function is already deprecated. As the user @ lost said, you should use finfo() , which complies with the same functionality in a better way. You can add at the beginning of your script the following function, as seen in this question :

function _mime_content_type($filename) {
    $result = new finfo();
    if (is_resource($result) === true) {
        return $result->file($filename, FILEINFO_MIME_TYPE);
    }
    return false;
}

And then replace it in your script :

$fileType = mime_content_type( $path );

by

$fileType = _mime_content_type( $path );
    
25.11.2014 / 15:32