gpt4 book ai didi

php - 这是用 PHP 发送电子邮件的正确方法吗?

转载 作者:IT王子 更新时间:2023-10-29 00:15:13 26 4
gpt4 key购买 nike

我有点担心这个函数是否能按照它应该的方式发送大多数电子邮件和网络邮件客户端都能正确识别的电子邮件,特别是我最担心的是这个问题:

  • 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;
}

最佳答案

虽然这应该可行,但我强烈建议使用预构建的邮件/SMTP 类,例如 Zend_Mail .虽然我不认为整个 Zend Framework 是猫的睡衣,但我确实对他们的邮件处理代码有很好的看法。

编辑:我还应该补充一点,使用预构建的邮件/SMTP 类将抽象出多部分电子邮件的几乎所有复杂性/结构。

2009-05-06 更新:直接回答您的问题。

  • Are the UTF-8 declarations and attachments well formed?

他们看起来还不错。

  • Do I need to use quoted_printable_decode()? If yes, where?

没有。你会想使用 quoted_printable_decode()仅当您正在解码电子邮件时。不是在编码时。你应该使用 quoted_printable_encode() ?接下来我将讨论这个问题。

  • Content-Transfer-Encoding: 7 or 8 bits? I've always seen 7 but since I'm sending a UTF-8 encoded mail I'm not sure.

如果您知道目标 SMTP 服务器可以支持,则只使用 8 位编码。但是,由于您要将电子邮件传递给本地 MTA,因此我不建议设置此值。默认值是 7 位编码,但它有自己的一组限制:代码范围 1-127 的每行最多 998 个八位字节,CR 和 LF 只允许作为 CRLF 行结尾的一部分出现( https://www.rfc-editor.org/rfc/rfc2045#section-2.7 )。

我建议您使用 Quoted-Printable ( https://www.rfc-editor.org/rfc/rfc2045#section-6.7) Content-Transfer-Encoding。你在哪里打电话trim(strip_tags($message, '<a>'))trim($message)你会想用 quoted_printable_encode(trim(...)) 附上那些.

  • Should I use mb_send_mail() or mail() is enough?

如果您知道您不会处理多字节消息(日文、韩文、中文等),那么mail()应该足够了。

现在我已经回答了您最初的问题,让我告诉您哪里存在一些问题。

  1. 您指定纯文本和 Html 内容部分的字符集是 UTF-8,但它并没有出现,因为您实际上是在确保它们确实是 UTF-8 编码的。
  2. 您正在检查 null$to , $bcc , $attachments然而,在你进一步处理它们之前,当它们实际上可能是 null 时你没有做任何事情。 .所以,如果你碰巧收到一个 null对于 $to ,您不处理该变量,但您继续发送电子邮件至 null .

截至目前,这就是我要讨论的全部内容,但我仍然强烈推荐预构建的解决方案,因为他们有很多用户/时间来解决错误。

关于php - 这是用 PHP 发送电子邮件的正确方法吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/809924/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com