我有點擔心,如果這個功能將能夠正確地對多數電子郵件和網絡郵件客戶端的方式應該得到承認的郵件,特別是我最關心這個疑惑:這是用PHP發送電子郵件的正確方法嗎?
- 是UTF -8聲明和附件是否形成良好?
- 我需要使用quoted_printable_decode()嗎?如果是,在哪裏?
- 內容傳輸編碼:7或8位?我一直看到7,但因爲我發送一個UTF-8編碼的郵件,我不確定。
- 我應該使用mb_send_mail()還是mail()就夠了?
編輯:我不知道爲什麼,但代碼顯示不正確,我將其提供@http://gist.github.com/104818
編輯2:我知道其他替代品(庫)的電子郵件處理,但爲了我自己的好奇心和知識,我只想知道這段代碼是否100%好,或者它是否有問題。
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;
}
血腥的好回答瓊斯先生。我希望你得到賞金+1 – da5id 2009-05-08 00:19:47
我同意,Zend_Mail擁有你需要的一切和更多!使用由許多人開發的庫,它通常具有較少的錯誤。 – Kekoa 2009-05-11 15:43:58