Joscer Posted April 22, 2011 Share Posted April 22, 2011 Good Afternoon, I wonder if anybody can help me with a couple of PHP issues I am having. I am a Junior Web Designer and have very basic PHP knowledge. I have already done most of the work but I'm having an issue that is driving me mental. I purchased a html template which has a pre-configured contact form that uses jquery validation and "sendmail function." I have made the relevant changes to the PHP file (recipient address and subjet line) however when I go live with the system it only works once and then the html page doesn't process the message. If I go to the PHP location I get a 500 Internal Server Error message. I have contacted my hosting provider (mellowhost) and they have said that my script will only work if I add SMTP Authentication to it, which I have no idea how to do. Here is my script: <?php // Chnage this according to your settings $to = '[email protected]'; // Change the mail according to your need. $subject = "Contact Form"; // Mail Subject $success_msg = '<strong style="color:#0033CC">Your Message has been submitted and we will get back to you ASAP.</strong><br /><br />'; // The message displayed after successful mail delivery $failure_msg = '<h2>Contact Form Submitted!</h2> <p>Due to some reason mail is not sent.</p>'; // The message displayed after mail delivery failure //Do not edit below this line. if ( trim($_POST['names']) != '' ) { $names = stripslashes(strip_tags($_POST['names'])); } else { $names = 'No name entered'; } if ( trim($_POST['emails']) != '' ) { $emails = stripslashes(strip_tags($_POST['emails'])); } else { $emails = 'No email entered'; } if ( trim($_POST['phone']) != '' ) { $phone = stripslashes(strip_tags($_POST['phone'])); } else { $phone = 'No phone number entered'; } if ( trim($_POST['comments']) != '' ) { $comments = nl2br(stripslashes(strip_tags($_POST['comments']))); } else { $comments = 'No comments entered'; } ob_start(); ?> <html> <head> <style type="text/css"> .style3 {color: #000000} </style> </head> <body> <table width="550" border="1" cellspacing="2" cellpadding="2"> <tr bgcolor="#eeffee"> <td width="132"><span class="style3">Name</span></td> <td width="398"><?=$names;?></td> </tr> <tr bgcolor="#eeeeff"> <td>Email</td> <td><?=$emails;?></td> </tr> <tr bgcolor="#eeffee"> <td>Comments</td> <td><?=$comments;?></td> </tr> </table> </body> </html> <? $body = ob_get_contents(); ob_end_clean(); $headers = 'MIME-Version: 1.0' . "\r\n"; $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; $headers .= 'From: '.$names.' <'.$emails.'>' . "\r\n"; if(mail($to, $subject, $body, $headers)) { echo $success_msg; } else { echo $failure_msg; } ?> Please can somebody help me find some PHP solace? Thanks-in-advance. Liam Quote Link to comment https://forums.phpfreaks.com/topic/234461-php-nightmare/ Share on other sites More sharing options...
guyfromfl Posted April 22, 2011 Share Posted April 22, 2011 Your ISP needs to set the permission to 755 on the file giving you the 500 error. Do you have FireBug installed? What does the returned data say? Quote Link to comment https://forums.phpfreaks.com/topic/234461-php-nightmare/#findComment-1204969 Share on other sites More sharing options...
Joscer Posted April 22, 2011 Author Share Posted April 22, 2011 Your ISP needs to set the permission to 755 on the file giving you the 500 error. Do you have FireBug installed? What does the returned data say? Tried that. Still does the same. Any other suggestions? Quote Link to comment https://forums.phpfreaks.com/topic/234461-php-nightmare/#findComment-1204973 Share on other sites More sharing options...
fugix Posted April 22, 2011 Share Posted April 22, 2011 look in your php.ini file for your smtp settings Quote Link to comment https://forums.phpfreaks.com/topic/234461-php-nightmare/#findComment-1204990 Share on other sites More sharing options...
Joscer Posted April 22, 2011 Author Share Posted April 22, 2011 look in your php.ini file for your smtp settings and... Quote Link to comment https://forums.phpfreaks.com/topic/234461-php-nightmare/#findComment-1204995 Share on other sites More sharing options...
gevensen Posted April 22, 2011 Share Posted April 22, 2011 just for fun try the below in a php file of its own of your server, unless your localhost on your machine is set up properly it will only send from your webserver change the emails to your and try and run the file if it mails your server is set up and the issue is in your code dont forget if your trying to send from your local computer it will fail unless you jump through hoops to make your development server send <?php $to = '[email protected]'; $subject = 'the subject'; $message = 'hello this is a test'; $headers = 'From: [email protected]' . "\r\n" . 'Reply-To: [email protected]' . "\r\n" . 'X-Mailer: PHP/' . phpversion(); mail($to, $subject, $message, $headers); ?> Quote Link to comment https://forums.phpfreaks.com/topic/234461-php-nightmare/#findComment-1204998 Share on other sites More sharing options...
Joscer Posted April 22, 2011 Author Share Posted April 22, 2011 just for fun try the below in a php file of its own of your server, unless your localhost on your machine is set up properly it will only send from your webserver change the emails to your and try and run the file if it mails your server is set up and the issue is in your code dont forget if your trying to send from your local computer it will fail unless you jump through hoops to make your development server send <?php $to = '[email protected]'; $subject = 'the subject'; $message = 'hello this is a test'; $headers = 'From: [email protected]' . "\r\n" . 'Reply-To: [email protected]' . "\r\n" . 'X-Mailer: PHP/' . phpversion(); mail($to, $subject, $message, $headers); ?> You must bare with me as I have very basic PHP knowledge. I pased the above code into a blank PHP file and uploaded with default file permissions. I went to the location and it did a 500 Internal Server Error. Thanks-in-advance Liam Quote Link to comment https://forums.phpfreaks.com/topic/234461-php-nightmare/#findComment-1204999 Share on other sites More sharing options...
gevensen Posted April 22, 2011 Share Posted April 22, 2011 post the actual 500 error HTTP Error 500 Internal server error Introduction The Web server (running the Web Site) encountered an unexpected condition that prevented it from fulfilling the request by the client (e.g. your Web browser or our CheckUpDown robot) for access to the requested URL. This is a 'catch-all' error generated by the Web server. Basically something has gone wrong, but the server can not be more specific about the error condition in its response to the client. In addition to the 500 error notified back to the client, the Web server should generate some kind of internal error log which gives more details of what went wrong. It is up to the operators of the Web server site to locate and analyse these logs. 500 errors in the HTTP cycle Any client (e.g. your Web browser or our CheckUpDown robot) goes through the following cycle when it communicates with the Web server: * Obtain an IP address from the IP name of the site (the site URL without the leading 'http://'). This lookup (conversion of IP name to IP address) is provided by domain name servers (DNSs). * Open an IP socket connection to that IP address. * Write an HTTP data stream through that socket. * Receive an HTTP data stream back from the Web server in response. This data stream contains status codes whose values are determined by the HTTP protocol. Parse this data stream for status codes and other useful information. This error occurs in the final step above when the client receives an HTTP status code that it recognises as '500'. Frank Vipond. September 2010. Fixing 500 errors - general This error can only be resolved by fixes to the Web server software. It is not a client-side problem. It is up to the operators of the Web server site to locate and analyse the logs which should give further information about the error. Fixing 500 errors - CheckUpDown Please contact us (email preferred) whenever you encounter 500 errors on your CheckUpDown account. We then have to liaise with your ISP and the vendor of the Web server software so they can trace the exact reason for the error. Correcting the error may require recoding program logic for the Web server software, which could take some time. Quote Link to comment https://forums.phpfreaks.com/topic/234461-php-nightmare/#findComment-1205000 Share on other sites More sharing options...
Joscer Posted April 22, 2011 Author Share Posted April 22, 2011 Internal Server Error The server encountered an internal error or misconfiguration and was unable to complete your request. Please contact the server administrator, [email protected] and inform them of the time the error occurred, and anything you might have done that may have caused the error. More information about this error may be available in the server error log. Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request. Quote Link to comment https://forums.phpfreaks.com/topic/234461-php-nightmare/#findComment-1205001 Share on other sites More sharing options...
gevensen Posted April 22, 2011 Share Posted April 22, 2011 If mail() encounters an error, it just returns false. To find why, we need to discover where the sendmail command put its error. On Linux, this is usually /var/log/messages. The mail errors don't end up in /var/log/httpd/errors (because apache doesn't know that /usr/sbin/sendmail returning false is an error), nor in /var/log/mail/ (because the email never got as far as postfix, and no SMTP/delivery error occurred.) Quote Link to comment https://forums.phpfreaks.com/topic/234461-php-nightmare/#findComment-1205002 Share on other sites More sharing options...
PFMaBiSmAd Posted April 22, 2011 Share Posted April 22, 2011 If your web host told you that you unconditionally need to use SMTP Authentication in order to send an email through the mail server at your web host, you will need to do so. The php mail() function does not support SMTP Authentication. You could write a script that opens a socket with the mail server and exchanges SMTP commands, including those needed for SMTP Authentication, but it will be easier if you just use an existing php mailer class that can do this for you. See the phpmailer - http://phpmailer.worxware.com/ or swift mailer - http://swiftmailer.org/ Quote Link to comment https://forums.phpfreaks.com/topic/234461-php-nightmare/#findComment-1205004 Share on other sites More sharing options...
gevensen Posted April 22, 2011 Share Posted April 22, 2011 just to make sure mail is failing try $to = '[email protected]'; $subject = 'the subject'; $message = 'hello this is a test'; $headers = 'From: [email protected]' . "\r\n" . 'Reply-To: [email protected]' . "\r\n" . 'X-Mailer: PHP/' . phpversion(); if (mail($to, $subject, $message, $headers)) { echo 'mail() Success!' . "<br />\n"; } else { echo 'mail() Failure!' . "<br />\n"; } Quote Link to comment https://forums.phpfreaks.com/topic/234461-php-nightmare/#findComment-1205005 Share on other sites More sharing options...
gevensen Posted April 22, 2011 Share Posted April 22, 2011 I would listen to him he has more posts than me!! If your web host told you that you unconditionally need to use SMTP Authentication in order to send an email through the mail server at your web host, you will need to do so. The php mail() function does not support SMTP Authentication. You could write a script that opens a socket with the mail server and exchanges SMTP commands, including those needed for SMTP Authentication, but it will be easier if you just use an existing php mailer class that can do this for you. See the phpmailer - http://phpmailer.worxware.com/ or swift mailer - http://swiftmailer.org/ Quote Link to comment https://forums.phpfreaks.com/topic/234461-php-nightmare/#findComment-1205006 Share on other sites More sharing options...
Joscer Posted April 22, 2011 Author Share Posted April 22, 2011 I'm extremely grateful for your responses but I'm not a PHP programmer and I don't understand all of these terms. All I need to do is make my current PHP script SMTP authenticated. Can I just add some code onto it and configure the details to match my SMTP details? I will try that mail fail thingy now Quote Link to comment https://forums.phpfreaks.com/topic/234461-php-nightmare/#findComment-1205008 Share on other sites More sharing options...
Joscer Posted April 22, 2011 Author Share Posted April 22, 2011 If your web host told you that you unconditionally need to use SMTP Authentication in order to send an email through the mail server at your web host, you will need to do so. The php mail() function does not support SMTP Authentication. You could write a script that opens a socket with the mail server and exchanges SMTP commands, including those needed for SMTP Authentication, but it will be easier if you just use an existing php mailer class that can do this for you. See the phpmailer - http://phpmailer.worxware.com/ or swift mailer - http://swiftmailer.org/ I didn't think I needed another mailer class as I'm using PEAR. Quote Link to comment https://forums.phpfreaks.com/topic/234461-php-nightmare/#findComment-1205010 Share on other sites More sharing options...
Joscer Posted April 22, 2011 Author Share Posted April 22, 2011 gevensen It failed on line 8 Warning: mail() [function.mail]: Could not execute mail delivery program '/usr/sbin/sendmail -t -i' in /home2/gainford/public_html/z/bin/test.php on line 8 Line 8 contains mail($to, $subject, $message, $headers); which my host has told me will not work as it's not SMTP authenticated. I can only think that it's initially working becuase it's only initially routing locally and eventually being sent to relay due to low priority on local routing. I am losing the will to live with all of this... Quote Link to comment https://forums.phpfreaks.com/topic/234461-php-nightmare/#findComment-1205012 Share on other sites More sharing options...
PFMaBiSmAd Posted April 22, 2011 Share Posted April 22, 2011 as I'm using PEAR The code you posted at the start of this thread isn't using PEAR. It is using the php mail() function. Quote Link to comment https://forums.phpfreaks.com/topic/234461-php-nightmare/#findComment-1205013 Share on other sites More sharing options...
Joscer Posted April 22, 2011 Author Share Posted April 22, 2011 as I'm using PEAR The code you posted at the start of this thread isn't using PEAR. It is using the php mail() function. Please can you advise me what I need to do with my current script (initially posted) so that it will work with my SMTP mail server? Quote Link to comment https://forums.phpfreaks.com/topic/234461-php-nightmare/#findComment-1205015 Share on other sites More sharing options...
PFMaBiSmAd Posted April 22, 2011 Share Posted April 22, 2011 You can use PEAR:Mail or one of the two scripts I already suggested. At the point of calling the mail() function in the current script, you would instead create an instance of whatever mailer class you are using, configure it to use SMTP Authentication (password and mail box name/email address) against a mail box you have on the mail server, pass it the to and from address, the subject, and the message body, and call the send method. Quote Link to comment https://forums.phpfreaks.com/topic/234461-php-nightmare/#findComment-1205017 Share on other sites More sharing options...
Joscer Posted April 22, 2011 Author Share Posted April 22, 2011 You can use PEAR:Mail or one of the two scripts I already suggested. At the point of calling the mail() function in the current script, you would instead create an instance of whatever mailer class you are using, configure it to use SMTP Authentication (password and mail box name/email address) against a mail box you have on the mail server, pass it the to and from address, the subject, and the message body, and call the send method. I don't know how to do any of that and your technical talk has baffled me. I am giving up. Quote Link to comment https://forums.phpfreaks.com/topic/234461-php-nightmare/#findComment-1205019 Share on other sites More sharing options...
PFMaBiSmAd Posted April 22, 2011 Share Posted April 22, 2011 So, have you even visited the web site of one of the php mailer classes of your choice and looked at an example showing how to use SMTP Authentication with it? In programming, lack of attempt = automatic failure. You have got to at least try or I could move this thread to the Freelancing Forum section for you if you want to pay someone to do this and get it working on your web host? Quote Link to comment https://forums.phpfreaks.com/topic/234461-php-nightmare/#findComment-1205025 Share on other sites More sharing options...
Joscer Posted April 22, 2011 Author Share Posted April 22, 2011 This is how my script looks. I have made some changes with it: <?php // Chnage this according to your settings $to = '[email protected]'; // Change the mail according to your need. $subject = "Contact Form"; // Mail Subject $success_msg = '<strong style="color:#0033CC">Your Message has been submitted and we will get back to you ASAP.</strong><br /><br />'; // The message displayed after successful mail delivery $failure_msg = '<h2>Contact Form Submitted!</h2> <p>Due to some reason mail is not sent.</p>'; // The message displayed after mail delivery failure //Do not edit below this line. if ( trim($_POST['names']) != '' ) { $names = stripslashes(strip_tags($_POST['names'])); } else { $names = 'No name entered'; } if ( trim($_POST['emails']) != '' ) { $emails = stripslashes(strip_tags($_POST['emails'])); } else { $emails = 'No email entered'; } if ( trim($_POST['phone']) != '' ) { $phone = stripslashes(strip_tags($_POST['phone'])); } else { $phone = 'No phone number entered'; } if ( trim($_POST['comments']) != '' ) { $comments = nl2br(stripslashes(strip_tags($_POST['comments']))); } else { $comments = 'No comments entered'; } ob_start(); ?> <html> <head> <style type="text/css"> .style3 {color: #000000} </style> </head> <body> <table width="550" border="1" cellspacing="2" cellpadding="2"> <tr bgcolor="#eeffee"> <td width="132"><span class="style3">Name</span></td> <td width="398"><?=$names;?></td> </tr> <tr bgcolor="#eeeeff"> <td>Email</td> <td><?=$emails;?></td> </tr> <tr bgcolor="#eeffee"> <td>Comments</td> <td><?=$comments;?></td> </tr> </table> </body> </html> <? $body = ob_get_contents(); ob_end_clean(); $headers = 'MIME-Version: 1.0' . "\r\n"; $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; $headers .= 'From: '.$names.' <'.$emails.'>' . "\r\n"; // Sending emails via SMTP *********************************************** function mail_smtp ($to, $subject, $mess, $hdrs=""){ require_once "Mail.php"; $host = "slocalhost"; $port = "465"; $username = "[email protected]"; $password = "BLOCKED"; $headers = array ( 'To' => $to, 'Subject' => $subject, 'MIME-Version' => '1.0', 'Content-type' => 'text/html; charset=utf-8', 'Content-transfer-encoding' => '8Bit'); if($hdrs!=''){ $arr=explode("\n",trim($hdrs)); if($arr) foreach($arr as $line){ $arrnew=explode(": ",trim($line)); $headers[trim($arr[0])]=trim($arrnew[1]); } } $smtp = Mail::factory('smtp', array ('host' => $host, 'port' => $port, 'auth' => true, 'username' => $username, 'password' => $password)); $mail = $smtp->send($to, $headers, $mess); if(PEAR::isError($mail)){ return false; } else{ return true; } }?> if(mail_smtp($to, $subject, $body, $headers)) { echo $success_msg; } else { echo $failure_msg; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/234461-php-nightmare/#findComment-1205026 Share on other sites More sharing options...
Joscer Posted April 22, 2011 Author Share Posted April 22, 2011 just for fun try the below in a php file of its own of your server, unless your localhost on your machine is set up properly it will only send from your webserver change the emails to your and try and run the file if it mails your server is set up and the issue is in your code dont forget if your trying to send from your local computer it will fail unless you jump through hoops to make your development server send <?php $to = '[email protected]'; $subject = 'the subject'; $message = 'hello this is a test'; $headers = 'From: [email protected]' . "\r\n" . 'Reply-To: [email protected]' . "\r\n" . 'X-Mailer: PHP/' . phpversion(); mail($to, $subject, $message, $headers); ?> That works but only once and then it does a 500 and I have to wait about 30 mins b4 it works again (just like the contact form). Quote Link to comment https://forums.phpfreaks.com/topic/234461-php-nightmare/#findComment-1205029 Share on other sites More sharing options...
Joscer Posted April 23, 2011 Author Share Posted April 23, 2011 Good Morning, So, after a very long battle yesterday with my SMTP PHP issue (10 hours solid) I decided to give it another go this morning after some sleep. I have modified my code to support SMTP which now works perfect but only one of my fields are appearing on the body of the e-mail. Here is my new code: <?php //Do not edit below this line. if ( trim($_POST['names']) != '' ) { $names = stripslashes(strip_tags($_POST['names'])); } else { $names = 'No name entered'; } if ( trim($_POST['emails']) != '' ) { $emails = stripslashes(strip_tags($_POST['emails'])); } else { $emails = 'No email entered'; } if ( trim($_POST['phone']) != '' ) { $phone = stripslashes(strip_tags($_POST['phone'])); } else { $phone = 'No phone number entered'; } if ( trim($_POST['comments']) != '' ) { $comments = nl2br(stripslashes(strip_tags($_POST['comments']))); } else { $comments = 'No comments entered'; } $headers = 'MIME-Version: 1.0' . "\r\n"; $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; require_once "Mail.php"; // Header & body of the mail. $from = $emails; $to = "Web Support Team <[email protected]>"; $subject = "Website Message"; $body = $names; $body = $emails; $body = $comments; // SMTP Authentication Details $host = "localhost"; $username = "[email protected]"; $password = "REMOVED_FOR_SECURITY"; // Code Segment $headers = array ('From' => $emails, '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>Thank You for your message, we will contact with within 24 hours.</p>"); } ?> Does anybody have any suggestions as to why I am only getting the "$comments;" field to appear on the body my my e-mails? Any help appreciated. Thanks-in-adance. Liam Quote Link to comment https://forums.phpfreaks.com/topic/234461-php-nightmare/#findComment-1205204 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.