【问题标题】:PHP: How to communicate with mail server thru SMTP?PHP:如何通过 SMTP 与邮件服务器通信?
【发布时间】:2011-06-09 05:08:42
【问题描述】:

如何使用 PHP 通过 SMTP 与邮件服务器通信?

【问题讨论】:

  • 如果您认为人们不明白,请澄清您的问题...

标签: php smtp


【解决方案1】:

我为我个人PHP framework, phunctionEmail() 方法编写了这个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;
}

【讨论】:

    【解决方案2】:

    看看Zend_Mail,它拥有与邮件相关的所有功能

    -http://framework.zend.com

    -http://framework.zend.com/manual/en/zend.mail.html

    【讨论】:

      【解决方案3】:

      使用fsockopen 打开一个套接字。使用fwrite 写入套接字。使用fgets逐行读取套接字或使用fread逐字节读取。

      【讨论】:

      • 这正是我想要的。注意:当使用fwrite() 与 smtp 服务器通信时,您必须使用换行符 "\r\n" 终止您的命令字符串,以便 smtp 服务器开始执行您的命令。
      【解决方案4】:

      也许你正在寻找这个:

      这些示例使用 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

      【讨论】:

        【解决方案5】:

        看看PHPMailer

        【讨论】:

          【解决方案6】:

          查找 mail() 的文档。

          【讨论】:

          • 你没明白我在问什么。
          • @Bronislaw 我真的认为他会这样做,这是另一个资源email.about.com/od/emailprogrammingtips/qt/… 如果您不喜欢答案,请在您的问题中更详细地说明。
          • 我需要发送例如RCPT TO 命令并获得响应...我不想发送邮件。
          • @Bronislaw 请在您的原始帖子中添加该内容和更多详细信息。努力使问题尽可能清晰,以便我们给您一个非常明确的答案。
          猜你喜欢
          • 2011-09-19
          • 1970-01-01
          • 1970-01-01
          • 2011-02-07
          • 2018-10-17
          • 2020-08-05
          • 1970-01-01
          • 2011-10-21
          • 2016-06-15
          相关资源
          最近更新 更多