日期:2014-05-17  浏览次数:20498 次

通过STMP发邮件,这段代码错在哪?
发不出邮件,也不返回错误提示。各位高手看看代码错在哪?

PHP code
<?php
header( "content-type:text/html;charset=utf-8" );
$smtp = array( "url" => "smtp.gmail.com",
    "port" => "465",
    "username" => "", //用户名
    "password" => "", //密码
    "from" => "", //发件人e-mail
    "to" => "", //收件人e-mail
    "subject" => "测试一下标题",
    "body" => "测试一下内容" 
    );

$CRLF = "\r\n";
$test = "";

$header = array( 'Return-path' => '<' . $smtp['from'] . '>',
    'Date' => date( 'r' ),
    'From' => '<' . $smtp['from'] . '>',
    'MIME-Version' => '1.0',
    'Subject' => inlineCode( $smtp['subject'] ),
    'To' => $smtp['to'],
    'Content-Type' => 'text/html; charset=UTF-8; format=flowed',
    'Content-Transfer-Encoding' => 'base64' 
    );

$data = buildHeader( $header ) . $CRLF . chunk_split( base64_encode( $smtp['body'] ) );
$content = "EHLO " . $smtp["url"] . $CRLF; // 先得hello一下
$content .= "AUTH LOGIN" . $CRLF . base64_encode( $smtp["username"] ) . $CRLF . base64_encode( $smtp["password"] ) . $CRLF; // 验证登陆
$content .= "MAIL FROM:" . $smtp["from"] . $CRLF; // 发件地址
$content .= "RCPT TO:" . $smtp["to"] . $CRLF; // 收件地址
$content .= "DATA" . $CRLF . $data . $CRLF . "." . $CRLF; // 发送内容
$content .= "QUIT" . $CRLF; // 退出

$curl = curl_init();
curl_setopt( $curl, CURLOPT_URL, $smtp['url'] );
curl_setopt( $curl, CURLOPT_PORT, $smtp['port'] );
curl_setopt( $curl, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; .NET CLR 3.0.4506.2152)' );
curl_setopt( $curl, CURLOPT_TIMEOUT, 10 );
curl_setopt( $curl, CURLOPT_RETURNTRANSFER, true ); // curl接收返回数据
curl_setopt( $curl, CURLOPT_SSL_VERIFYPEER, false );
curl_setopt( $curl, CURLOPT_SSL_VERIFYHOST, false );
curl_setopt( $curl, CURLOPT_CUSTOMREQUEST, $content );

$test = curl_exec( $curl );

var_dump( $test );

curl_close( $curl );

function inlineCode( $str ) {
    $str = trim( $str );
    return $str ? '=?UTF-8?B?' . base64_encode( $str ) . '?= ' : '';
} 
function buildHeader( $headers ) {
    $ret = '';
    foreach( $headers as $k => $v ) {
        $ret .= $k . ': ' . $v . "\n";
    } 
    return $ret;
} 

?>


------解决方案--------------------
不知道是不是1楼所说的问题

只能提供一个我用的发邮件的php,楼主可以试试
PHP code

                                <?php
                                if($_SERVER["REQUEST_METHOD"]=="POST"){?>
                                <?php                                                            
                                //ok的邮箱发送。
                                include "smtp.class.php";
                                //$smtpserver = "SMTP.163.com"; //您的smtp服务器的地址
                                $smtpserver="";
                                $port =25; //smtp服务器的端口,一般是 25 
                                //$smtpuser = ""; //您登录smtp服务器的用户名
                                //$smtppwd = ""; //您登录smtp服务器的密码
                                $mailtype = "HTML"; //邮件的类型,可选值是 TXT 或 HTML ,TXT 表示是纯文本的邮件,HTML 表示是 html格式的邮件
                                $sender = "{$email}"; 
                                //发件人,一般要与您登录smtp服务器的用户名($smtpuser)相同,否则可能会因为smtp服务器的设置导致发送失败
                                $smtp  =   new smtp($smtpserver,$port,false,$smtpuser,$smtppwd,$sender);   //第三个值为false时,可匿名登录发送邮件,将$smtpuser、$smtppwd注释掉
                                //$smtp->debug = true; //是否开启调试