Jump to content

Mail with simple .txt attachment gone wrong.


Kyrus

Recommended Posts

Hello.

 

Very quick version: Does anyone know how to encode an attachment in MIME to accept line rbeaks in.txt documents?

 

Long version:

 

I have this code that puts the attachment into my email that I'm gonna send below on the code.

 

$fileatt = "encomendas/$filename.txt"; // Path to the file
$fileatt_type = "text/html"; // File Type
$fileatt_name = "$filename.txt"; // Filename that will be used for the file as the attachment
$email_from = "[email protected]"; // Who the email is from
$email_subject = "Encomenda"; // The Subject of the email
$email_txt = "Dados do Cliente referente a Medicalexpress online<br>\n<br>\nNúmero: $nr <br>\n Nome: $name <br>\n Endereço: $endereco <br>\n Localidade: $localidade <br>\n Código postal: $cod_postal <br>\n País: $pais <br>\n Telefone: $telefone <br>\n Fax: $fax <br>\n Profissão: $profissao <br>\n Actividade: $actividade <br>\n Met. pagamento: $metpag <br>\n Email: $email <br>\n Nr. Contribuinte: $nrcont <br>\n Mensagem: $msg";
$email_to = "[email protected]"; // Who the email is too
$headers = "From: ".$email_from;

$file = fopen($fileatt,'rb');
$enc_txt = fread($file,filesize($fileatt));
fclose($file);

$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";

$headers .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_boundary}\"";

$email_message .= "This is a multi-part message in MIME format.\n\n" .
"--{$mime_boundary}\n" .
"Content-Type:text/html; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$email_message . "\n\n" .
$email_txt . "\n\n";


$enc_txt = chunk_split(base64_encode($enc_txt));

$email_message .= "--{$mime_boundary}\n" .
"Content-Type: {$fileatt_type};\n" .
" name=\"{$fileatt_name}\"\n" .
//"Content-Disposition: attachment;\n" .
//" filename=\"{$fileatt_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$enc_txt . "\n\n" .
"--{$mime_boundary}--\n";

 

It works. It sends the .txt attachment I want in the email.

 

The thing is, the .txt that comes with the email isint accepting line breaks. I guess it has to do with the base64 encoding so please can anyone tell me how to encode my .txt to accept line breaks?

 

Thank you.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.