doulizhi1247 2011-09-10 10:45
浏览 69

连接到外部smtp服务器

I'm trying to connect to my cpanel smtp server from localhost. I can make connection with it by fopensocket on port 25,but after that which command should I put to the server?

I read here and try to do same as it,but my server return unrecognized command.

code:

function authgMail($from, $namefrom, $to, $nameto, $subject, $message)
    {

    $smtpServer = "mail.printf.ir"; 
    $port = "25"; 
    $timeout = "45"; 
    $username = "info@printf.ir"; 
    $password = "******"; 
    $localhost = $_SERVER['REMOTE_ADDR'];
    $newLine = "
"; //var just for newlines

    //connect to the host and port
    $smtpConnect = fsockopen($smtpServer, $port, $errno, $errstr, $timeout);
    //echo $errstr." - ".$errno;
    $smtpResponse = fgets($smtpConnect, 4096);
    if(empty($smtpConnect))
    {
       $output = "Failed to connect: $smtpResponse";
       //echo $output;
       return $output;
    }
    else
    {
       $logArray['connection'] = "Connected to: $smtpResponse";
    }

    //you have to say HELO again after TLS is started
       fputs($smtpConnect, "HELO $localhost". $newLine);
       $smtpResponse = fgets($smtpConnect, 4096);
       $logArray['heloresponse2'] = "$smtpResponse";

    //request for auth login
    fputs($smtpConnect,"AUTH LOGIN" . $newLine);
    $smtpResponse = fgets($smtpConnect, 4096);
    $logArray['authrequest'] = "$smtpResponse";

    //send the username
    fputs($smtpConnect, base64_encode($username) . $newLine);
    $smtpResponse = fgets($smtpConnect, 4096);
    $logArray['authusername'] = "$smtpResponse";

    //send the password
    fputs($smtpConnect, base64_encode($password) . $newLine);
    $smtpResponse = fgets($smtpConnect, 4096);
    $logArray['authpassword'] = "$smtpResponse";

    //email from
    fputs($smtpConnect, "MAIL FROM: " . $newLine);
    $smtpResponse = fgets($smtpConnect, 4096);
    $logArray['mailfromresponse'] = "$smtpResponse";

    //email to
    fputs($smtpConnect, "RCPT TO: " . $newLine);
    $smtpResponse = fgets($smtpConnect, 4096);
    $logArray['mailtoresponse'] = "$smtpResponse";

    //the email
    fputs($smtpConnect, "DATA" . $newLine);
    $smtpResponse = fgets($smtpConnect, 4096);
    $logArray['data1response'] = "$smtpResponse";

    //construct headers
    $headers = "MIME-Version: 1.0" . $newLine;
    $headers .= "Content-type: text/html; charset=iso-8859-1" . $newLine;
    $headers .= "To: $nameto $to" . $newLine;
    $headers .= "From: $namefrom " . $newLine;

    //observe the . after the newline, it signals the end of message
    fputs($smtpConnect, "To: 
From: $from
Subject: $subject
$headers

$message
.
");
    $smtpResponse = fgets($smtpConnect, 4096);
    $logArray['data2response'] = "$smtpResponse";

    // say goodbye
    fputs($smtpConnect,"QUIT" . $newLine);
    $smtpResponse = fgets($smtpConnect, 4096);
    $logArray['quitresponse'] = "$smtpResponse";
    $logArray['quitcode'] = substr($smtpResponse,0,3);
    fclose($smtpConnect);
    //a return value of 221 in $retVal["quitcode"] is a success



    $a='Mail Sent Sucessfully';
        print_r($logArray);
    return($a);
    }
        echo (authgMail("info@printf.ir", "Moein", "moein_7line@yahoo.com", "Akbar Agha", "Test ", "Test"));

what has been echo:


Array ( [connection] => Connected to: 220-linux10.avaserver.com ESMTP Exim 4.69 #1 Sat, 10 Sep 2011 15:08:40 +0430 [heloresponse2] => 220-We do not authorize the use of this system to transport unsolicited, [authrequest] => 220 and/or bulk e-mail. [authusername] => 250 linux10.avaserver.com Hello 127.0.0.1 [79.127.22.89] [authpassword] => 503 AUTH command used when not advertised [mailfromresponse] => 500 unrecognized command [mailtoresponse] => 500 unrecognized command [data1response] => 550 Access denied - Invalid HELO name (See RFC2821 4.1.3) [data2response] => [quitresponse] => [quitcode] => ) Mail Sent Sucessfully
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 如何在scanpy上做差异基因和通路富集?
    • ¥20 关于#硬件工程#的问题,请各位专家解答!
    • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
    • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
    • ¥30 截图中的mathematics程序转换成matlab
    • ¥15 动力学代码报错,维度不匹配
    • ¥15 Power query添加列问题
    • ¥50 Kubernetes&Fission&Eleasticsearch
    • ¥15 報錯:Person is not mapped,如何解決?
    • ¥15 c++头文件不能识别CDialog