连接到外部smtp服务器

时间:2011-09-10 10:45:18

标签: php smtp sendmail

我正在尝试从localhost连接到我的cpanel smtp服务器。 我可以通过端口25上的fopensocket与它建立连接,但之后我应该将哪个命令放到服务器上?

我看了here并尝试做同样的事情,但我的服务器返回无法识别的命令

代码:

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 = "\r\n"; //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: \r\nFrom: $from\r\nSubject: $subject\r\n$headers\r\n\r\n$message\r\n.\r\n");
    $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"));

什么是回声:


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 个答案:

没有答案