srujana Posted September 17, 2006 Share Posted September 17, 2006 Hai all,i want a feedback form in my site and i tried with mail() function.But my hosting service doesnt provide me. And he suggested to do it in any other way like smtp.I tried with many scripts which are online.But couldnt succeed.I took Professional PHP4 book from my library and tried with the code which is there in it.It is not sending email .. and it even shows that there is no error.I used 3 files.I am pasting them here.and the o/p i got.I dont know where i am going wrong.Please help me:(This is my_mail_class.php[code]<?php//my_mail_class.phpclass My_Mail{ var $to=''; var $from=''; var $reply_to=''; var $cc=''; var $bcc=''; var $subject=''; var $body=''; var $validate_email=true; var $rigorous_email_check=false; var $allow_empty_subject=false; var $allow_empty_body=false; var $headers = array(); var $ERROR_MSG; var $ERR_EMPTY_MAIL_TO="Empty TO Field!"; var $ERR_EMPTY_SUBJECT="Empty SUBJECT Field!"; var $ERR_EMPTY_BODY="Empty BODY Field!"; var $ERR_SEND_MAIL_FAILURE="An Error Occured While Attempting To Send EMAIL!"; var $ERR_TO_FIELD_INVALID="TO Field Contains Invalid Email Address!"; var $ERR_CC_FIELD_INVALID="CC Field Contains Invalid Email Address!"; var $ERR_BCC_FIELD_INVALID="BCC Field Contains Invalid Email Address!"; var $STR_NO_ERROR="No error has occured yet"; function CheckFields() { if(empty($this->to)) { $this->ERROR_MSG=$this->ERR_EMPTY_MAIL_TO; return false; } if(!$this->allow_empty_subject && empty($this->subject)) { $this->ERROR_MSG=$this->ERR_EMPTY_SUBJECT; return false; } if(!$this->allow_empty_body && empty($this->body)) { $this->ERROR_MSG=$this->ERR_EMPTY_BODY; return false; } $this->to=ereg_replace(";",",",$this->to); $this->cc=ereg_replace(";",",",$this->cc); $this->bcc=ereg_replace(";",",",$this->bcc); if(!empty($this->from)) $this->headers[]="From: $this->from"; if(!empty($this->reply_to)) $this->headers[]="Reply-To: $this->reply_to"; //check email addresses if specified so. if($this->validate_email) { $to_emails=explode(",",$this->to); if(!empty($this->cc)) $cc_emails=explode(",",$this->cc); if(!empty($this->bcc)) $bcc_emails=explode(",",$this->bcc); //use MX records to further check email addresses. if($this->rigorous_email_check) { if(!$this->rigorousEmailCheck($to_emails)) { $this->ERROR_MSG=$this->ERR_TO_FIELD_INVALID; return false; } else if(is_array($cc_emails) && !$this->rigorousEmailCheck($cc_emails)) { $this->ERROR_MSG=$this->ERR_CC_FIELD_INVALID; return false; } else if(is_array($bcc_emails) && !$this->rigorousEmailCheck($bcc_emails)) { $this->ERROR_MSG=$this->ERR_BCC_FIELD_INVALID; return false; } } else { if(!$this->email_check($to_emails)) { $this->ERROR_MSG=$this->ERR_TO_FIELD_INVALID; return false; } else if(is_array($cc_emails) && !$this->email_check($cc_emails)) { $this->ERROR_MSG=$this->ERR_CC_FIELD_INVALID; return false; } else if(is_array($bcc_emails) && !$this->email_check($bcc_emails)) { $this->ERROR_MSG=$this->ERR_BCC_FIELD_INVALID; return false; } } } return false; } function emailcheck($emails) { foreach($emails as $email) { if(eregi("<(.+)>",$email,$match)) $email=$match[1]; if(!eregi("^[_\-\.0-9a-z]+@([0-9a-z][_0-9a-z\.]+)\.([a-z]{2,4}$)",$email)) return false; } return true; } function rigorousEmailCheck($emails) { if(!$this->emailCheck($emails)) return false; foreach ($emails as $email) { list($user,$domain)=split("@",$email,2); if(checkdnsrr($domain,"ANY")) return true; else { return false; } } } function viewMsg() { if(!$this->checkFields()) return false; $this->headers=array(); $this->buildHeaders(); $this->headers[]="From: $this->from"; $this->headers[]="To: $this->to"; $this->headers[]="Subject: $this->subject"; $msg= implode("\r\n", $this->headers); $msg.="\r\n\r\n"; $msg.=$this->body; return $msg; } function send() { if(!$this->checkFields()) return true; $this->buildHeaders(); if(mail($this->to,stripslashes(trim($this->sunject)),stripslashes($this->body),implode("\r\n",$this->headers))) return true; else { $this->ERROR_MSG=$this->ERR_SEND_MAIL_FAILURE; return false; } } function errorMsg() { if(empty($this->ERROR_MSG)) return $this->STR_NO_ERROR; return $this->ERROR_MSG; }}?>[/code]The above class is then extended by my_smtp_mail_class.php[code]<?php//my_smtp_mail_class.phpinclude "./my_mail_class.php";class My_Smtp_Mail extends My_Mail{ var $smtp_host=''; var $smtp_port=25; var $socket=0; var $response_code=0; var $response_msg=''; var $ERR_SMTP_HOST_NOT_SET='SMTP host not set!'; var $ERR_SMTP_CONNECTION_FAILED='Failed to connect to the specified SMTP host!'; var $ERR_SMTP_NOT_CONNECTED='Establish a connection to an SMTP server first!'; var $ERR_COMMAND_UNRECOGNIZED='Unrecognizable command!'; var $ERR_HELO_WITHOUT_ARG='HELO command needs an argument'; var $ERR_MAIL_WITHOUT_ARG='MAIL FROM command needs an argument'; var $ERR_RCPT_WITHOUT_ARG='RCPT TO command needs an argument'; var $ERR_DATA_WITHOUT_ARG='DATA command needs an argument'; var $ERR_UNKNOWN_RESPONSE_FROM_SERVER='Unknown response from the server'; var $ERR_HELO_FAILED='HELOcommand failed'; var $ERR_MAIL_FAILED='MAIL TO command failed'; var $ERR_RCPT_FAILED='RCPT TO command failed'; var $ERR_DATA_FAILED='DATA command failed'; var $ERR_QUIT_FAILED='QUIT command failed'; var $ERR_INIT_SOCKET_ERROR="Couldn't initialize the socket!"; function connect() { if(empty($this->smtp_host)) { $this->ERROR_MSG=$this->ERR_SMTP_HOST_NOT_SET; return false; } $this->socket=fsockopen($this->smtp_host,$this->smtp_port,&$err_no,&$err_str); if(!$this->socket) { if(!$err_no) { $err_str=$this->ERR_INIT_SOCKET_ERROR; } $this->ERROR_MSG=$this->ERR_SMTP_CONNECTION_FAILED."$err_no: $err_str"; return false; } if(!$this->getResponse()) { $this->ERROR_MSG=$this->ERR_UNKNOWN_RESPONSE_FROM_SERVER.":".$this->response_msg; return false; } if($this->response_code!=220) { $this->ERROR_MSG=$this->ERR_SMTP_CONNECTION_FAILED." ".$this->response_code." ".$this->response_msg; return false; } return true; } function getResponse() { if(!$this->socket) { $this->ERROR_MSG=$this->ERR_SMTP_NOT_CONNECTED; return false; } $server_response=fgets($this->socket,1024); if(ereg("^([0-9]{3}) (.*)$",$server_response,$match)) { $this->response_code=$match[1]; $this->response_msg=$match[2]; return true; } $this->response_msg=$server_response; return false; } function talk($cmd,$args='') { if(!$this->socket) { $this->ERROR_MSG=$this->ERR_SMTP_NOT_CONNECTED; return false; } switch($cmd) { case "HELO": if(empty($args)) { $this->ERROR_MSG=$this->ERR_HELO_WITHOUT_ARG; return false; } $smtp_cmd="HELO $arg\r\n"; fwrite($this->socket,$smtp_cmd); if(!$this->getResponse()) { $this->ERROR_MSG=$this->ERR_UNKNOWN_RESPONSE_FROM_SERVER.":".$this->response_msg; return false; } if($this->response_code!=250) { $this->ERROR_MSG=$this->ERR_HELO_FAILED." ".$this->response_code." ".$this->response_msg; return false; } break; case "MAIL": if(empty($args)) { $this->ERROR_MSG=$this->ERR_MAIL_WITHOUT_ARG; return false; } $smtp_cmd="MAIL FROM: $arg\r\n"; fwrite($this->socket,$smtp_cmd); if(!$this->getResponse()) { $this->ERROR_MSG=$this->ERR_UNKNOWN_RESPONSE_FROM_SERVER.":".$this->response_msg; return false; } if($this->response_code!=250) { $this->ERROR_MSG=$this->ERR_MAIL_FAILED." ".$this->response_code." ".$this->response_msg; return false; } break; case "RCPT": if(empty($arg)) { $this->ERROR_MSG=$this->ERR_RCPT_WITHOUT_ARG; return false; } $to_emails=explode(",",$arg); foreach ($to_emails as $email) { $smtp_cmd="RCPT TO: $email\r\n"; fwrite($this->socket,$smtp_cmd); if(!$this->getResponse()) { $this->ERROR_MSG=$this->ERR_UNKNOWN_RESPONSE_FROM_SERVER.":".$this->response_msg; return false; } if($this->response_code!=250) { $this->ERROR_MSG=$this->ERR_RCPT_FAILED." ".$this->response_code." ".$this->response_msg; return false; } } break; case "DATA": if(empty($arg)) { $this->ERROR_MSG=$this->ERR_DATA_WITHOUT_ARG; return false; } $smtp_cmd="DATA\r\n"; fwrite($this->socket,$smtp_cmd); if(!$this->getResponse()) { $this->ERROR_MSG=$this->ERR_UNKNOWN_RESPONSE_FROM_SERVER.":".$this->response_msg; return false; } if($this->response_code!=354) { $this->ERROR_MSG=$this->ERR_DATA_FAILED." ".$this->response_code." ".$this->response_msg; return false; } $smtp_cmd="$arg\r\n"."."."\r\n"; fwrite($this->socket,$smtp_cmd); if(!$this->getResponse()) { $this->ERROR_MSG=$this->ERR_UNKNOWN_RESPONSE_FROM_SERVER.":".$this->response_msg; return false; } if($this->response_code!=250) { $this->ERROR_MSG=$this->ERR_DATA_FAILED." ".$this->response_code." ".$this->response_msg; return false; } break; case "QUIT": $smtp_cmd="QUIT\r\n"; fwrite($this->socket,$smtp_cmd); if(!$this->getResponse()) { $this->ERROR_MSG=$this->ERR_UNKNOWN_RESPONSE_FROM_SERVER.":".$this->response_msg; return false; } if($this->response_code!=251) { $this->ERROR_MSG=$this->ERR_QUIT_FAILED." ".$this->response_code." ".$this->response_msg; return false; } break; default: $this->ERROR_MSG=$this->ERR_COMMAND_UNRECOGNIZED; return false; break; } return true; } function send() { if(!$this->checkFields()) return false; $this->buildHeaders(); if(!$this->connect()) return false; if(!$this->talk("HELO",$GLOBALS["SERVER_NAME"])) return false; if(!$this->talk("MAIL",$this->from)) return false; if(!$this->talk("RCPT",$this->to)) return false; if(!empty($this->to)) $this->headers[]="To: $this->to"; if(!empty($this->subject)) $this->headers[]="Subject: $this->subject"; if(!$this->talk("DATA",implode("\r\n",$this->headers)."\r\n\r\n".$this->body)) return false; if(!$this->talk("QUIT")) return false; fclose($this->socket); return true; }}?> [/code]Now,the below script is for testing....[code]<?php//my_smtp_mail_class_test.phpinclude("./my_smtp_mail_class.php");$mail=new My_Smtp_Mail();$mail->smtp_host=''; //i tried with localhost and also with the smtp server name my hostingsite provided to me.. but nothing worked$mail->to="myid@mydomain.com";$mail->from="anotherid@gmail.com";$mail->cc='';$mail->bcc='';$mail->subject="Testing..1..2..3..4";$mail->body="Just testing...";$mail->rigorous_email_check=1;if($mail->send()){ echo ("Successfully sent an email titled $mail->subject!");}else die("Error while attempting to send an email titled $mail->subject:".$mail->errorMsg());echo("<br>");echo(str_replace("\r\n","<br>",$mail->viewMsg()));?>[/code]The output i got is:[b]Error while attempting to send an email titled Testing..1..2..3..4:No error has occured yet[/b] Quote Link to comment https://forums.phpfreaks.com/topic/21046-smtp-script-not-working/ Share on other sites More sharing options...
shortj75 Posted September 17, 2006 Share Posted September 17, 2006 first try this script it is the form and mail script in onethis script is set to work with the name simpleemail.php so you will want to name this page that ok[code]//this is so you dont get any notices or warnings<? error_reporting(E_ERROR);?><?//this is the mail script$to = $_POST['to'];$sub=$_POST['sub'];$mess = $_POST['mess']; $from="From: $_POST[email]";if(isset($_POST['submit'])){ $mail=mail("$to", "$sub", "$mess", "$from"); if(!$mail){print "<center>Sorry Your E-mail Was Not Sent</center>";}else{print "<center>Your E-mail Was Sent Successfuly</center>"; } }else{//and here is the mail formprint "<form method=post action=simpleemail.php>";print "<center><table cellpadding=0 cellspacing=0 class=contacttable><tH class=contacthead><center>Simple E-mail</CENTER>";print "<TR><TD>To: <input type=text name=to size=40>";print "<TR><TD>From: <input type=text name=email>";print "<TR><TD>Subject: <input type=text name=sub size=70>";print "<TR><TD>Message:<br />";print "<textarea name=mess cols=80 rows=10></textarea><BR />";print "<input type=submit name=submit value=send>";print "</th></tr></td></table></center></form>";}?>[/code]if this script doesnt work your host either doesnt have a mail server or hasnt enabled it to let there users run scripts from there pages and i know for a fact this script works because i use to use it on my page Quote Link to comment https://forums.phpfreaks.com/topic/21046-smtp-script-not-working/#findComment-93420 Share on other sites More sharing options...
shortj75 Posted September 17, 2006 Share Posted September 17, 2006 if the script i posted workes you may want to try to declair your varibles like so [code]$to = "myid@mydomain.com";$sub = "Testing..1..2..3..4";$mess = "Just testing..."; $from ="From: $_POST[email]";[/code]instead of[code]$mail=new My_Smtp_Mail();$mail->smtp_host=''; //i tried with localhost and also with the smtp server name my hostingsite provided to me.. but nothing worked$mail->to="myid@mydomain.com";$mail->from="anotherid@gmail.com";$mail->cc='';$mail->bcc='';$mail->subject="Testing..1..2..3..4";$mail->body="Just testing...";$mail->rigorous_email_check=1;[/code]because your host has register globals off witch is recomended and if that is the case you may have to declair them like so $to = $_POST['to'];or else they wont work Quote Link to comment https://forums.phpfreaks.com/topic/21046-smtp-script-not-working/#findComment-93421 Share on other sites More sharing options...
srujana Posted September 17, 2006 Author Share Posted September 17, 2006 shortj75,thanks for ur reply.the script above is saying that the email is sent successfully... the same happens with my past scripts but i havent get any mail.this is because that my hosting service wont allow mail function so some reasons.so.. i am looking for an alternative..have u seen my script above... can u find anything wrong in tht? Quote Link to comment https://forums.phpfreaks.com/topic/21046-smtp-script-not-working/#findComment-93431 Share on other sites More sharing options...
redarrow Posted September 17, 2006 Share Posted September 17, 2006 try this code as i no that some computers smtp that have been setup with tight condition will only send mail with the php correct header information.Your also have to cheek your spam box as meny free mail systems see meny posts as spam if there not in your address book ok.test it.php[code]<?php$to = 'nobody@example.com';$subject = 'the subject';$message = 'hello';$headers = 'From: webmaster@example.com' . "\r\n" . 'Reply-To: webmaster@example.com' . "\r\n" . 'X-Mailer: PHP/' . phpversion();mail($to, $subject, $message, $headers);?> [/code] Quote Link to comment https://forums.phpfreaks.com/topic/21046-smtp-script-not-working/#findComment-93452 Share on other sites More sharing options...
srujana Posted September 17, 2006 Author Share Posted September 17, 2006 I already worked with that script and failed... coz my hosting service is not providing tht function for some reasons:(do u have any other script?? Quote Link to comment https://forums.phpfreaks.com/topic/21046-smtp-script-not-working/#findComment-93458 Share on other sites More sharing options...
redarrow Posted September 17, 2006 Share Posted September 17, 2006 did you ask the hosting company why?have you asked what you can use to send email?if you dont ask you will never no .ask good luck. Quote Link to comment https://forums.phpfreaks.com/topic/21046-smtp-script-not-working/#findComment-93459 Share on other sites More sharing options...
srujana Posted September 17, 2006 Author Share Posted September 17, 2006 ya.. i asked them several times...what they said is to try smtp...since i had no other choice.. i am running back of books and net and tried so many scripts...this is my position right now:( Quote Link to comment https://forums.phpfreaks.com/topic/21046-smtp-script-not-working/#findComment-93474 Share on other sites More sharing options...
redarrow Posted September 17, 2006 Share Posted September 17, 2006 there you go mate[code]<?php@set_time_limit(0); require_once 'smtp_mail.php'; $to = "expertphp@yahoo.com";$from = "from@myaccount.com";$subject = "Subject here"; $headers = "MIME-Version: 1.0\r\n". "Content-type: text/html; charset=iso-8859-1\r\n". "From: \"My Name\" <".$from.">\r\n". "To: \"Client\" <".$to.">\r\n". "Date: ".date("r")."\r\n". "Subject: ".$subject."\r\n"; $message = "<html><body><b>html message</b><br><font color=\"red\">here</font><img src=\"http://static.php.net/www.php.net/images/php.gif\" border=\"0\" alt=\"\"></body></html>";$response = smtp_mail($to, $subject, $message, $from, $headers); if($response[0]) echo "The message has been sent !<br />\n".$response[1];else echo "The message can not been sent !<br />\n".$response[1]; ?> [/code] Quote Link to comment https://forums.phpfreaks.com/topic/21046-smtp-script-not-working/#findComment-93481 Share on other sites More sharing options...
srujana Posted September 17, 2006 Author Share Posted September 17, 2006 is smtp_mail.php will be there in default.. i executed thinking so and got that cannot include.. Quote Link to comment https://forums.phpfreaks.com/topic/21046-smtp-script-not-working/#findComment-93586 Share on other sites More sharing options...
shortj75 Posted September 18, 2006 Share Posted September 18, 2006 your host probably means to do it by haveing you maile go through a different mail host like yahoo or something like so[code]<?php$from = "who from <from@therehost.com>";$to = "you<you@yourhost.com>";$subject = "whats up";$body = "nothing and you";$host = "smtp.host.com"; //(like smtp.yahoo.com)or msn or who ever you user for your email address$username = "your_username";(example: killer)$password = "your_password";(example: dog or ****)$headers = array ('From' => $from, 'To' => $to, 'Subject' => $subject);$smtp = Mail::factory('smtp', array ('host' => $host, 'auth' => true, 'username' => $username, 'password' => $password));$mail = $smtp->send($to, $headers, $body);if (PEAR::isError($mail)) { echo("<p>" . $mail->getMessage() . "</p>"); } else { echo("<p>Message successfully sent!</p>"); }[/code]or something along that line this code might or might not work for you but some one else may have one that will work better for what you need Quote Link to comment https://forums.phpfreaks.com/topic/21046-smtp-script-not-working/#findComment-93871 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.