Link to home
Start Free TrialLog in
Avatar of dc-ops
dc-ops

asked on

Send mail via SMTP using PHP

I am trying to send a mail via SMTP using PHP. The coding as below:


 // initialization setting
 $mailserver="";//smtp mail server;
 $sender=""; //sender address, example: test@fedex.com;
 $pass=""; //sender mailbox password that is for authenticate user.
 $from= ""; //for mail body content same as sender.
 $to1=""; //for receive address.
 $subject=""; //mail subject;
 
 $employeename=$_POST['employeename'];//employee name;
 $employeeid=$_POST['employeeid']; // employee id
 
 $fp = @fsockopen($mailserver,25,$errno,$errstr,15);
 if ($fp){
  $instr= fgets($fp,512);
  if (substr($instr,0,3)!="220"){
            echo "resp=1&reason=1.".$instr;
            exit();
  }

 


  $outstr="EHLO Me\r\n";
  fputs($fp,$outstr);
  //echo $outstr;
   
  $instr= fread($fp,2048);
  ////echo  $instr;
 
      if (substr($instr,0,3)!="250"){
            echo "resp=1&reason=2.".$instr;
            exit();
      }

 
  $outstr="AUTH LOGIN\r\n";
  fputs($fp,$outstr);
  //echo $outstr;
 
  $instr= fgets($fp,512);
  //echo  $instr;
 
  if (substr($instr,0,3)!="334"){
            echo "resp=1&reason=3.".$instr;
            exit();
  }
 
   
  $outstr=base64_encode($sender)."\r\n";
  fputs($fp,$outstr);
  //echo $outstr;
 
  $instr= fgets($fp,512);
  //echo  $instr;
 
  if (substr($instr,0,3)!="334"){
            echo "resp=1&reason=4.".$instr;
            exit();
  }

 
  $outstr= base64_encode($pass)."\r\n";
  fputs($fp,$outstr);
  //echo $outstr;
 
  $instr= fgets($fp,512);
  //echo  $instr;
 
  if (substr($instr,0,3)!="235"){
            echo "resp=1&reason=5.".$instr;
            exit();
  }  
 
  $outstr="MAIL FROM:<".$sender.">\r\n";
  fputs($fp,$outstr);
  //echo $outstr;
 
  $instr= fgets($fp,512);
  //echo  $instr;
 
  if (substr($instr,0,3)!="250"){
            echo "resp=1&reason=6.".$instr;
            exit();
  }

 
  $outstr="RCPT TO:<".$to1.">\r\n";
  fputs($fp,$outstr);
  //echo $outstr;
 
  $instr= fgets($fp,512);
  //echo  $instr;
 
  if (substr($instr,0,3)!="250"){
            echo "resp=1&reason=7.".$instr;
            exit();
  }

 /* $outstr="RCPT TO:<".$to2.">\r\n";
  fputs($fp,$outstr);
 
  $instr= fgets($fp,512);
  if (substr($instr,0,3)!="250"){
            echo "resp=1&reason=".$instr;
            exit();
  }*/
 
  $outstr="DATA\r\n";
  fputs($fp,$outstr);
  //echo $outstr;
 
  $instr= fgets($fp,512);
  //echo  $instr;
 
  if (substr($instr,0,3)!="354"){
            echo "resp=1&reason=8.".$instr;
            exit();
  }

 
  $data="Date: ".date("r",time())."\r\n";
  $data.="To: ".$to."\r\n";
  $data.="From: ".$from."\r\n";
  $data.="Reply-to: ".$sender."\r\n";
  $data.="Subject:".$subject."\r\n";
  $data.="X-Priority: 3\r\n";
  $data.="X-Mailer: phpmailer [version 1.54]\r\n";
  $data.="Return-Path: ".$sender."\r\n";
  $data.="MIME-Version: 1.0\r\n";
  $data.="Content-Transfer-Encoding: 8bit\r\n";
  $data.="Content-Type: text/plain; charset = \"utf-8\"\r\n";
  $data.="\r\n";
  $data.="
Employee name: $employeename
Employee ID: $employeeid";

  $outstr=$data."\r\n.\r\n";
  fputs($fp,$outstr);
 
  $instr= fgets($fp,512);
  //echo  $instr;
  if (substr($instr,0,3)!="250"){
            echo "resp=1&reason=9.".$instr;
            exit();
  }
 
  $outstr="QUIT\r\n";
  fputs($fp,$outstr);
  //echo $outstr;

  fclose($fp);
 
  //====
//  echo "Congratulation! TEST Mail is sent success!<br />\r\n<br />\r\n==========mail body below==============<br />\r\n"."mail from:".$from."<br />\r\nrcpt to:".$to."<br />\r\ndata:".$data."<br />\r\n=====================================";//remove this line while seccess;
  //====
  //====
  echo "resp=0";
 
 }else{
       echo "resp=1&reason=mail connect error";
 }
 
Everytime I tried out, it gives me the error "mail connect error". Any kind experts out there, please help.

*I have my own web server, running windows server 2003. Its running on IIS, with PHP and Perl supported.
Avatar of t_itanium
t_itanium
Flag of United Arab Emirates image

it may be aproblem with authenticating smtp...

or there is something blocking the connection ..such as firewall or other..
cheers
Avatar of david_levine
david_levine

What's the mail server? At least in the code you posted:
$mailserver="";//smtp mail server;

and I didn't see any code that's setting it to the name of the mail server. If you're running local on the IIS machine and it's acting as a SMTP server (listening on port 25) then use 127.0.0.1 or localhost as the mail server name.

Avatar of dc-ops

ASKER

Hi David,

I purposely omit out the mail server, to avoid any security implications. And I am connecting to an external SMTP server, hence when the below code will run when attempting to connect to the stated SMTP server:

$fp = @fsockopen($mailserver,25,$errno,$errstr,15);
 if ($fp){
  $instr= fgets($fp,512);
  if (substr($instr,0,3)!="220"){
          echo "resp=1&reason=1.".$instr;
          exit();
  }

If it fails to connect, then the below code will generate the error message:

echo "resp=1&reason=mail connect error";

Kindly advise.
ASKER CERTIFIED SOLUTION
Avatar of sscotti
sscotti
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial