Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Email problem with Windows server

Status
Not open for further replies.

jewel464g

Technical User
Jul 18, 2001
198
US
I've searched the FAQs and previous posts but I don't see where anyone else has had the same problem that I am having. I'm working on Windows IIS 5, PHP version 4.0.6. I'm using "Professional PHP programming" wrox press, to learn PHP. I'm trying to send an email from an online form.

I've read in the book that Windows systems often do not have a local e-mail system. So I should use the class smtp_mail. I keep getting this error message:

Parse error: parse error, expecting `'('' in E:\Jessica\Practice\condition air\smtp_mail.inc on line 92

Fatal error: Cannot instantiate non-existent class: smtp_mail in E:\Jessica\Practice\condition air\smtpmail.php on line 32

I'm thinking that if I fix the first error that the second one will clear up.

This is line 92
92 function handle_e-mail($from, $to, $data)
93 {
94 # split recipient list

I'll attach the entire include file here, if further information is needed.

<?php

/*
* save as &quot;smtp_mail.inc&quot;
*/

class smtp_mail {
var $fp = false;
var $lastmsg = &quot;&quot;;

/*
* read_line() reads a line from the socket
* and returns the numeric code and the rest of the line
*/

function read_line()
{
$ret = false;

$line = fgets($this->fp, 1024);

if(ereg(&quot;^([0-9]+).(.*)$&quot;, $line, &$data)) {
$recv_code = $data[1];
$recv_msg = $data[2];

$ret = array($recv_code, $recv_msg);
}

return $ret;
}

/*
* dialogue() sends a command $cmd to the remote server
* and checks whether the numeric return code is the one
* we expect ($code).
*/

function dialogue($code, $cmd)
{
$ret = true;

fwrite($this->fp, $cmd.&quot;\r\n&quot;);

$line = $this->read_line($this->fp);

if($line == false) {
$ret = false;
$this->lastmsg = &quot;&quot;;
} else {
$this->lastmsg = &quot;$line[0] $line[1]&quot;;

if($line[0] != $code) {
$ret = false;
}
}

return $ret;
}

/*
* error_message() prints out an error message,
* including the last message received from the SMTP server.
*/

function error_message()
{
echo &quot;SMTP protocol failure (&quot;.$this->lastmsg.&quot;).<br>&quot;;
}

/*
* crlf_encode() fixes line endings
* RFC 788 specifies CRLF (hex 0x13 0x10) as line endings
*/

function crlf_encode($data)
{
# make sure that the data ends with a newline character
$data .= &quot;\n&quot;;
# remove all CRs and replace single LFs with CRLFs
$data = str_replace(&quot;\n&quot;, &quot;\r\n&quot;, str_replace(&quot;\r&quot;, &quot;&quot;, $data));
# in the SMTP protocol a line consisting of a single &quot;.&quot; has
# a special meaning. We therefore escape it by appending one space.
$data = str_replace(&quot;\n.\r\n&quot;, &quot;\n. \r\n&quot;, $data);

return $data;
}

/*
* handle_e-mail() talks to the SMTP server
*/

function handle_e-mail($from, $to, $data)
{
# split recipient list
$rcpts = explode(&quot;,&quot;, $to);

$err = false;
if(!$this->dialogue(250, &quot;HELO phpclient&quot;) ||
!$this->dialogue(250, &quot;MAIL FROM:$from&quot;)) {
$err = true;
}

for($i = 0; !$err && $i < count($rcpts); $i++) {
if(!$this->dialogue(250, &quot;RCPT TO:$rcpts[$i]&quot;)) {
$err = true;
}
}

if($err || !$this->dialogue(354, &quot;DATA&quot;) ||
!fwrite($this->fp, $data) ||
!$this->dialogue(250, &quot;.&quot;) ||
!$this->dialogue(221, &quot;QUIT&quot;)) {
$err = true;
}

if($err) {
$this->error_message();
}

return !$err;
}

/*
* connect() connects to an SMTP server on the well-known port 25
*/

function connect($hostname)
{
$ret = false;

$this->fp = fsockopen($hostname, 25);

if($this->fp) {
$ret = true;
}

return $ret;
}

/*
* send_e-mail() connects to an SMTP server, encodes the message
* optionally, and sends $data. The envelope sender address
* is $from. A comma-separated list of recipients is expected in $to.
*/

function send_e-mail($hostname, $from, $to, $data, $crlf_encode = 0)
{
if(!$this->connect($hostname)) {
echo &quot;cannot open socket<br>\n&quot;;
return false;
}

$line = $this->read_line();
$ret = false;

if($line && $line[0] == &quot;220&quot;) {
if($crlf_encode) {
$data = $this->crlf_encode($data);
}

$ret = $this->handle_e-mail($from, $to, $data);
} else {
$this->error_message();
}

fclose($this->fp);

return $ret;
}
}
?>



 
why complicate?

your php must be configured to send e-mails by SMTP. The only thing you need is to use the PHP function mail()

But, to be sure, print the phpinfo() and seek to the var SMTP. If is set, you can send e-mail easiely Anikin
Hugo Alexandre Dias
Web-Programmer
anikin@ip.pt
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top