Using the function mail()
An email is composed of a header and a body, and the body can be separated into several parts. In the pattern the term used to identify the separator of these parts is boundary
. So let’s define a boundary
to our email. The rules for generating a boundary
can be found in the internet easily. But simply put, it’s nothing more than a random string that should appear in the email only when indicating a part of the email.
$boundary = "XYZ-".md5(date("dmYis"))."-ZYX";
Get the information from the file you will attach
// Arquivo enviado via formulário
$path = $_FILES['attachment']['tmp_name'];
$fileType = $_FILES['attachment']['type'];
$fileName = $_FILES['attachment']['name'];
// Ou arquivo local
$path = '/caminho/para/o/arquivo';
$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
Setting the header (There is other important header information you can add to prevent email from falling into the SPAM box).
// 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;
Definition of the HTML message
$mensagem = "--$boundary" . PHP_EOL;
$mensagem .= "Content-Type: text/html; charset='utf-8'" . PHP_EOL;
$mensagem .= "Mensagem"; // Adicione aqui sua mensagem
$mensagem .= "--$boundary" . PHP_EOL;
Attaching a file
$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;
Sending the email
mail($para, $assunto, $mensagem, $headers);
Using the Phpmailer
Download the Phpmailer and extract the files in your project folder.
Include the main Phpmailer archive
require_once('caminho/para/o/phpmailer/class.phpmailer.php');
Preparing the email
$email = new PHPMailer();
$email->From = '[email protected]';
$email->FromName = 'Seu nome';
$email->Subject = 'Assunto';
$email->Body = 'Corpo do email';
$email->AddAddress( '[email protected]' );
Attaching the file
$file_to_attach = 'caminho/do/arquivo/para/anexo';
$email->AddAttachment( $file_to_attach , 'nome_do_arquivo.pdf' );
Sending the email
$email->Send();
Phpmailer reduces several lines of code to a simple command $email->AddAttachment();
, much simpler! Using pure PHP will be several more lines and probably will encounter several difficulties and bugs.
I think it’s best you use Phpmailer or another library to send emails with attachment because it’s simpler than using the mail function().
– rray
This might help http://wiki.locaweb.com.br/pt-br/Enviando_e-mails_com_anexo_em_PHP
– Silvio Andorinha