Send mail with attachment

Asked

Viewed 165 times

2

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

Gives an error saying you don’t know Function mime_content_type(). I want to use 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);
  • The function equivalent to mime_content_type() is feint

  • Gives the same error. I just want the attachment to be attached, type <a href=mailto:[email protected]>email</a> where the mail software opens.

  • You want the attachment to turn the body of the email with html formatting?

  • No, that appears attached for later the user to insert the subject and the message in the mail software.

1 answer

2

The function mime_content_type() is already obsolete. As said the user @lost, should be used today finfo(), that fulfills the same functionality in a better way. You can add at the beginning of your script the following function, as seen in 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 in your script:

$fileType = mime_content_type( $path );

for

$fileType = _mime_content_type( $path );
  • Give me the following error Class 'finfo' not found

  • This error says that PHP did not find the finfo Class. What is the version of your PHP and what is your operating system? Starting 5.3 PHP already comes with finfo, but if you use Windows, you need to enable it in php.ini file, uncommenting the line that contains Extension=php_fileinfo.dll and restarting Apache after that.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.