estoy un poco preocupado si esta función envía mensajes de correo electrónico que pueden ser reconocidos correctamente en la mayoría de los clientes de correo electrónico y correo web como debe, específicamente estoy más preocupado por esta duda:¿Es esta la forma correcta de enviar correos electrónicos con PHP?
- son el UTF -8 declaraciones y archivos adjuntos bien formados?
- ¿Debo usar quoted_printable_decode()? ¿Si sí donde?
- Content-Transfer-Encoding: 7 u 8 bits? Siempre he visto 7 pero desde que estoy enviando un correo codificado en UTF-8 no estoy seguro.
- ¿Debo usar mb_send_mail() o mail() es suficiente?
EDIT: no sé por qué, pero el código no aparece correctamente, lo hice disponibles @http://gist.github.com/104818
EDIT 2: Soy consciente de otras alternativas (bibliotecas) para manejo de correo electrónico, pero por el bien de mi propia curiosidad y conocimiento solo deseo saber si este código es 100% bueno, o si tiene errores.
function Email($name, $from, $to, $subject, $message, $bcc = null, $attachments = null)
{
ini_set('SMTP', 'localhost');
ini_set('sendmail_from', $from);
$name = filter_var($name, FILTER_SANITIZE_STRING);
$from = filter_var($from, FILTER_SANITIZE_EMAIL);
$subject = filter_var($subject, FILTER_SANITIZE_STRING);
$boundary = '_Boundary_' . md5(microtime(true) . mt_rand(0, PHP_INT_MAX));
$headers = array
(
'MIME-Version: 1.0',
'Content-Type: multipart/mixed; boundary="Mixed' . $boundary . '"',
'Date: ' . date('r', time()),
'From: "' . $name . '" <' . $from . '>',
'Reply-To: "' . $name . '" <' . $from . '>',
'Return-Path: "' . $name . '" <' . $from . '>',
'X-Mailer: PHP ' . phpversion(),
'X-Priority: 2',
'X-MSMail-Priority: High',
'X-Originating-IP: ' . $_SERVER['SERVER_ADDR'],
);
if (is_null($to) === false)
{
if (is_array($to) === false)
{
$to = explode(',', $to);
}
foreach ($to as $key => $value)
{
$to[$key] = filter_var($value, FILTER_SANITIZE_EMAIL);
}
$to = implode(', ', array_filter($to));
}
if (is_null($bcc) === false)
{
if (is_array($bcc) === false)
{
$bcc = explode(',', $bcc);
}
foreach ($bcc as $key => $value)
{
$bcc[$key] = filter_var($value, FILTER_SANITIZE_EMAIL);
}
$headers[] = 'BCC: ' . implode(', ', array_filter($bcc));
}
if (is_null($attachments) === false)
{
settype($attachments, 'array');
foreach ($attachments as $key => $value)
{
if (is_file($value) === true)
{
$attachments[$key] = array
(
'',
'--Mixed' . $boundary,
'Content-Type: application/octet-stream; name="' . basename($value) . '"',
'Content-Disposition: attachment; filename="' . basename($value) . '"',
'Content-Transfer-Encoding: base64',
'',
trim(chunk_split(base64_encode(file_get_contents($value)))),
);
$attachments[$key] = implode("\n", $attachments[$key]);
}
else
{
unset($attachments[$key]);
}
}
$attachments = implode("\n", $attachments) . "\n";
}
$message = array
(
'This is a multi-part message in MIME format.',
'',
'--Mixed' . $boundary,
'Content-Type: multipart/alternative; boundary="Alt' . $boundary . '"',
'',
'--Alt' . $boundary,
'Content-Type: text/plain; charset="UTF-8"',
'Content-Disposition: inline',
'Content-Transfer-Encoding: 8bit',
'',
trim(strip_tags($message, '<a>')),
'',
'--Alt' . $boundary,
'Content-Type: text/html; charset="UTF-8"',
'Content-Disposition: inline',
'Content-Transfer-Encoding: 8bit',
'',
trim($message),
'',
'--Alt' . $boundary . '--',
$attachments,
'--Mixed' . $boundary . '--',
);
if (@mail($to, stripslashes($subject), implode("\n", $message), implode("\n", $headers)) === true)
{
return true;
}
return false;
}
Sangrienta buena respuesta Sr. Jones. Espero que recibas la recompensa +1 – da5id
¡Estoy de acuerdo, Zend_Mail tiene todo lo que necesitas y más! Use una biblioteca desarrollada por muchas personas, generalmente tiene menos errores. – Kekoa
"Si sabe que no va a manejar mensajes de Multibyte (japonés, coreano, chino, etc.), entonces mail() debería ser suficiente". - ¿Qué mensajes en otros idiomas, como alemán, francés, portugués y español? ¿Mail() todavía es suficiente? –