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['empl oyeename'] ;//employe e name;
$employeeid=$_POST['employ eeid']; // employee id
$fp = @fsockopen($mailserver,25, $errno,$er rstr,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($sen der)."\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-E ncoding: 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.
// 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['empl
$employeeid=$_POST['employ
$fp = @fsockopen($mailserver,25,
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($sen
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
$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-E
$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====================
//====
//====
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.
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.
$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.
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,$er rstr,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.
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,
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
or there is something blocking the connection ..such as firewall or other..
cheers