Búsqueda


Categorías

C# [15]
PHP [6]
Zend [1]
FileMaker [1]
.NET [6]
CVS [3]
General [9]
Javascript [2]
Paypal [1]
.NET Compact Framework [3]
C++ [1]
mysql [4]
Linux [1]

Últimos post

Dónde guardar los archivos de configuración en ClickOnce
phpinfo: Valores en error_reporting
Notes about Zend Server and Zend Studio setup
What's new in C# 4
Resolving a 'sticky tag is not a branch' error
Resumen del CodeCamp
CodeCamp Tarragona 2009
Acerca de la calidad del código
ClickOnce en Linux
Programando en PHP (y con CVS) en Visual Studio 2008

Sindicación

RSS 0.90
RSS 1.0
RSS 2.0
Atom 0.3

Enviar email con adjuntos

ctg | 22 Febrero, 2006 23:48

Pongo este código aquí para recordar como se envía un PHP un email con uno o varios ficheros adjuntos.

 <?php
function sendmail ($to, $subject, $text_message, $attachment="")
{
$main_boundary = "----=_NextPart_".md5(rand());
$text_boundary = "----=_NextPart_".md5(rand());
$html_boundary = "----=_NextPart_".md5(rand());
$headers="";
$headers .= "MIME-Version: 1.0\n";
$headers .= "Content-Type: multipart/mixed;\n\tboundary=\"$main_boundary\"\n";
$message="";
$message .= "\n--$main_boundary\n";
$message .= "Content-Type: multipart/alternative;\n\tboundary=\"$text_boundary\"\n";
$message .= "\n--$text_boundary\n";
$message .= "Content-Type: text/plain; charset=\"ISO-8859-1\"\n";
$message .= "Content-Transfer-Encoding: 7bit\n\n";
$message .= "\n--$text_boundary\n";
$message .= "Content-Type: multipart/related;\n\tboundary=\"$html_boundary\"\n";
$message .= "\n--$html_boundary\n";
$message .= "Content-Type: text/html; charset=\"ISO-8859-1\"\n";
$message .= "Content-Transfer-Encoding: quoted-printable\n\n";
$message .= str_replace ("=", "=3D", $text_message)."\n";
if (isset ($attachment) &&amp; $attachment != "" && count ($attachment) >= 1)
{
for ($i=0; $i<count ($attachment); $i++)
{
$attfile = $attachment[$i];
$file_name = basename ($attfile);
$fp = fopen ($attfile, "r");
$fcontent = "";
while (!feof ($fp))
{
$fcontent .= fgets ($fp, 1024);
}
$fcontent = chunk_split (base64_encode($fcontent));
@fclose ($fp);
$message .= "\n--$html_boundary\n";
$message .= "Content-Type: application/octetstream\n";
$message .= "Content-Transfer-Encoding: base64\n";
$message .= "Content-Disposition: inline; filename=\"$file_name\"\n";
$message .= "Content-ID: <$file_name>\n\n";
$message .= $fcontent;
}
}
$message .= "\n--$html_boundary--\n";
$message .= "\n--$text_boundary--\n";
$message .= "\n--$main_boundary--\n";

$result = @mail ($to, $subject, $message, $headers);
return $result;
}
?>

Posted in PHP . Comentario: (0). Retroenlaces:(0). Enlace
«Next post | Previous post»

Comentarios

Deja un comentario
















La mitad de 12: