【发布时间】:2011-06-09 05:08:42
【问题描述】:
如何使用 PHP 通过 SMTP 与邮件服务器通信?
【问题讨论】:
-
如果您认为人们不明白,请澄清您的问题...
如何使用 PHP 通过 SMTP 与邮件服务器通信?
【问题讨论】:
我为我个人PHP framework, phunction 的Email() 方法编写了这个sn-p,也许它可以提供一些帮助。我使用的正则表达式能够验证来自 SMTP 服务器的每个单独的回复。
if (isset($smtp) === true)
{
$result = null;
$stream = stream_socket_client($smtp);
if (is_resource($stream) === true)
{
$data = array('HELO ' . $_SERVER['HTTP_HOST']);
$result .= substr(ltrim(fread($stream, 8192)), 0, 3);
if (preg_match('~^220~', $result) > 0)
{
$auth = array_slice(func_get_args(), 8, 2);
if (count($auth) == 2)
{
$data = array_merge($data, array('AUTH LOGIN'), array_map('base64_encode', $auth));
}
$data[] = sprintf('MAIL FROM: <%s>', implode('', array_slice($from, 0, 1)));
foreach (array_merge(array_values($to), array_values($cc), array_values($bcc)) as $value)
{
$data[] = sprintf('RCPT TO: <%s>', $value);
}
$data[] = 'DATA';
$data[] = implode("\r\n", array_merge(array_diff_key($header, array('Bcc' => true)), array(''), $content, array('.')));
$data[] = 'QUIT';
while (preg_match('~^220(?>250(?>(?>334){1,2}(?>235)?)?(?>(?>250){1,}(?>354(?>250)?)?)?)?$~', $result) > 0)
{
if (fwrite($stream, array_shift($data) . "\r\n") !== false)
{
$result .= substr(ltrim(fread($stream, 8192)), 0, 3);
}
}
if (count($data) > 0)
{
if (fwrite($stream, array_pop($data) . "\r\n") !== false)
{
$result .= substr(ltrim(fread($stream, 8192)), 0, 3);
}
}
}
fclose($stream);
}
return (preg_match('~221$~', $result) > 0) ? true : false;
}
【讨论】:
看看Zend_Mail,它拥有与邮件相关的所有功能
-http://framework.zend.com
-http://framework.zend.com/manual/en/zend.mail.html
【讨论】:
也许你正在寻找这个:
这些示例使用 Pear Mail 包:http://pear.php.net/package/Mail
http://email.about.com/od/emailprogrammingtips/qt/PHP_Email_SMTP_Authentication.htm http://www.cyberciti.biz/tips/howto-php-send-email-via-smtp-authentication.html
【讨论】:
看看PHPMailer。
【讨论】:
查找 mail() 的文档。
【讨论】: