Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Send mail via SMTP using PHP

Posted on 2006-05-31
6
Medium Priority
?
343 Views
Last Modified: 2008-03-10
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.
0
Comment
Question by:dc-ops
4 Comments
 
LVL 5

Expert Comment

by:t_itanium
ID: 16796934
it may be aproblem with authenticating smtp...

or there is something blocking the connection ..such as firewall or other..
cheers
0
 
LVL 9

Expert Comment

by:david_levine
ID: 16797336
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.

0
 

Author Comment

by:dc-ops
ID: 16809085
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.
0
 
LVL 5

Accepted Solution

by:
sscotti earned 2000 total points
ID: 16826883
Not too well versed with PHP for smtp.

However, can you send mail using the same server and a standard e-mail client like Thunderbird or Outlook?  What parametes to you use for that (i.e. username@domain.com or just username?).

How is your mailserver configured for SMTP.  Usually, there are parameters to specify Authentication, relaying rules, etc.  Maybe these are not configured to allow sending in the manner you are trying to send.  If you are using an external server you will have to check with whoever owns it.

Another thing.  If the script is running on you server and not remotely you could probably set it up so that it doesn't require authentication (.e.g.  My ISP is qwest.net.  I don't have to use SMTP authentication with them because I have a fixed IP.  I'm on their network and that lets me send without authenticating.  If I connect to an app on my machine at home from the net I can still send via qwest using a PHP app like SquirrelMail or something similar).

Also, don't know if Windows supports sendmail, but if it were LINUX /  UNIX you were using you could try /usr/bin/sendmail or /usr/sbin/sendmail as an alternative.

Having a more descriptive error message would help.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When it comes to security, close monitoring is a must. According to WhiteHat Security annual report, a substantial number of all web applications are vulnerable always. Monitis offers a new product - fully-featured Website security monitoring and pr…
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
Any person in technology especially those working for big companies should at least know about the basics of web accessibility. Believe it or not there are even laws in place that require businesses to provide such means for the disabled and aging p…
The viewer will get a basic understanding of what section 508 compliance can entail, learn about skip navigation links, alt text, transcripts, and font size controls.
Suggested Courses
Course of the Month21 days, 3 hours left to enroll

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question