Location: PHPKode > projects > YW-GuestBook > YW Guestbook 1.45/YW Guestbook/ywgb/libs/mail_smtp.php
<?php
  //-------------------------------------------------------------------------
  //                             YW Guestbook
  //                              mail_smtp.php
  //
  //                              Version: 1.2
  //-------------------------------------------------------------------------
  //                     Copyright (c) 2005  YahooWebs.TK
  //                        http://www.Yahoowebs.tk
  //-------------------------------------------------------------------------
  //  This program is free software. You can redistribute it and/or modify
  //  it under the terms of the License that was distributed with this file!
  //-------------------------------------------------------------------------
     define ( "SMTPHOST" , "localhost" );
     define ( "SMTPPORT" , "25" );
     define ( "SMTPUSERNAME" , "" );
     define ( "SMTPPASSWORD" , "" );
     define ( "RETURNPATH" , "webmaster@".vgb_servername() );     
     define ( "MAILCHARSET" , "iso-8859-1" );
  //------------------------------------------------------------------------
    
  function vgb_mail( $to , $s , $m, $h = array()) {
    
    global $settings,$socket,$errorMSG;
    
    // set header
    $h['MIME-Version'] = "1.0";
    $h['X-Mailer'] = "YW Guestbook Mailer V".$settings['VERSION'];
    $h['From'] = ($settings['EMAILFROM'] != "") ? $settings['EMAILFROM'] : "YW Guestbook Mailer V".$settings['VERSION'];
    if (!isset($h['Content-Type']))
      $h['Content-Type'] = "text/plain; charset=".MAILCHARSET;
    $h['Content-transfer-encoding'] = "8bit";
    $h['X-Priority'] = "3";
    $h['X-MSMail-Priority'] = "Normal";
    
    $header = "Return-Path: ".RETURNPATH."\r\n".
              "Subject: $s\r\n".
              "To: $to\r\n";
    foreach ($h as $f => $v) {
      $header .= $f.": ".$v."\r\n";
    }
    
	 	$m = preg_replace("#(?<!\r)\n#si", "\r\n", $m);

  	if (!$socket = fsockopen(SMTPHOST,SMTPPORT,$error,$msg,20)) {
	    echo "<br /><b>Can't connect to SMTP host !</b><br />$error: $msg<br /><br />";
	    return false;
    }
	
	  if (!vgb_getresponse("220")) 
      return vgb_smtpfail();

  	if ((SMTPUSERNAME != "") && (SMTPPASSWORD != "")) { 
		  // start authentication
      fputs($socket,"EHLO ".SMTPHOST."\r\n");
		  if (!vgb_getresponse("250"))
        return vgb_smtpfail();

		  fputs($socket, "AUTH LOGIN\r\n");
		  if (!vgb_getresponse("334"))
        return vgb_smtpfail();

		  fputs($socket,base64_encode(SMTPUSERNAME)."\r\n");
		  if (!vgb_getresponse("334"))
        return vgb_smtpfail();

		  fputs($socket,base64_encode(SMTPPASSWORD)."\r\n");
		  if (!vgb_getresponse("235"))
        return vgb_smtpfail();
	  }
	  else {
		  fputs($socket, "HELO " . SMTPHOST . "\r\n");
		  if (!vgb_getresponse("250"))
        return vgb_smtpfail();
	  }

	  fputs($socket, "MAIL FROM: <".RETURNPATH.">\r\n");
	  if (!vgb_getresponse("250"))
      return vgb_smtpfail();

		fputs($socket, "RCPT TO: <$to>\r\n");
		if (!vgb_getresponse("250"))
      return vgb_smtpfail();
	
    fputs($socket, "DATA\r\n");
   	if (!vgb_getresponse("354"))
      return vgb_smtpfail();

  	fputs($socket, "$header\r\n");

  	fputs($socket, "$m\r\n");

  	fputs($socket, ".\r\n");
  	if (!vgb_getresponse("250"))
      return vgb_smtpfail();

  	fputs($socket, "QUIT\r\n");
	  fclose($socket);

	  return true;
  }
 
  function vgb_getresponse($response) { 
	  global $socket,$errorMSG;
    
    $servermsg = "";
    while (substr($servermsg, 3, 1) != ' ') { 
		  if (!($servermsg = fgets($socket, 256))) { 
			  $errorMSG = "<br /><b>No response codes received from Server</b><br /><br />";
        return false; 
		  } 
	  } 

	  if (!(substr($servermsg, 0, 3) == $response)) { 
		  $errorMSG = "<br /><b>Wrong response from SMTP mail server:</b><br />Server responded '$servermsg' instead of '$response'<br /><br />";
      return false; 
	  }
    return true; 
  }

  function vgb_smtpfail() {
    global $errorMSG;
    
    echo $errorMSG;
    return false;
  }
?>
Return current item: YW-GuestBook