PHP Error upon Form Send

Hi, my form on my web site works except for two things. When the form is sent I receive a PHP error instead of a ‘thank-you’ message and the message that you write into the message box area doesn’t send me the email message.

It would help if you showed some php and the error message. I wasn’t at school the day we were taught to read minds :wink:

Try to send something on the form in the link I provided, smart @ss :slight_smile: :slight_smile:

Pointless unless we can see php code. You are afterall asking the sitepoint community for help not vice versa.

Error Message;

require "phpmailer/class.phpmailer.php"; session_name("fancyform"); /* starting the session */ session_start(); foreach($_POST as $k=>$v) { /* if magic_quotes is enabled, strip the post array */ if(ini_get('magic_quotes_gpc')) $_POST[$k]=stripslashes($_POST[$k]); 

Thats a bit minimalist. All you’ve shown is that you’ve required another class. Thats not going to solve anything.

Again and this is my final request, would you please post the error message and the appropriate code.

While I’m going to avoid ultimatum talk, we do need the error message in order to diagnose the PHP issue. The not-receiving-mail issue might be something different/separate.

What i should say is that that isnt an error message, that’s your code that you’ve forgotten to put <?php ?> tags around.

I placed the PHP tags around the code, but here is the submit.php form code and below that is the current error message I get.

<?php
require "phpmailer/class.phpmailer.php";

session_name("fancyform");	/* starting the session */
session_start();

foreach($_POST as $k=>$v)
{
	/* if magic_quotes is enabled, strip the post array */
	if(ini_get('magic_quotes_gpc'))
	$_POST[$k]=stripslashes($_POST[$k]);

	$_POST[$k]=htmlspecialchars(strip_tags($_POST[$k]));
	/* escape the special chars */
}

$err = array();

/* some error checks */
if(!checkLen('name'))
	$err[]='The name field is too short or empty!';

if(!checkLen('email'))
	$err[]='The email field is too short or empty!';
else if(!checkEmail($_POST['email']))
	$err[]='Your email is not valid!';

if(!checkLen('subject'))
	$err[]='You have not selected a subject!';

if(!checkLen('message'))
	$err[]='The message field is too short or empty!';

/* compare the received captcha code to the one in the session array */
if((int)$_POST['captcha'] != $_SESSION['expect'])
	$err[]='The captcha code is wrong!';

/* if there are errors */
if(count($err))
{
	/* if the form was submitted via AJAX */
	if($_POST['ajax'])
	{
		echo '-1';
	}

	/* else fill the SESSION array and redirect back to the form */
	else if($_SERVER['HTTP_REFERER'])
	{
		$_SESSION['errStr'] = implode('<br />',$err);
		$_SESSION['post']=$_POST;

		header('Location: '.$_SERVER['HTTP_REFERER']);
	}

	exit;
}

/* the email body */
$msg=
'Name:	'.$_POST['name'].'<br />
Email:	'.$_POST['email'].'<br />
IP:	'.$_SERVER['REMOTE_ADDR'].'<br /><br />

Message:<br /><br />

'.nl2br($_POST['message']).'

';

$mail = new PHPMailer();	/* using PHPMailer */
$mail->IsMail();

$mail->AddReplyTo($_POST['email'], $_POST['name']);
$mail->AddAddress($emailAddress);
$mail->SetFrom($_POST['email'], $_POST['name']);
$mail->Subject = "A new ".mb_strtolower($_POST['subject'])." from ".$_POST['name']." | contact form feedback";

$mail->MsgHTML($msg);

$mail->Send();

unset($_SESSION['post']);	/* unsetting */

/* the form was successfully sent */
if($_POST['ajax'])
{
	echo '1';
}
else
{
	$_SESSION['sent']=1;

	if($_SERVER['HTTP_REFERER'])
		header('Location: '.$_SERVER['HTTP_REFERER']);

	exit;
}

/* some helpful functions */
function checkLen($str,$len=2)
{
	return isset($_POST[$str]) && mb_strlen(strip_tags($_POST[$str]),"utf-8") > $len;
}

function checkEmail($str)
{
	return preg_match("/^[\\.A-z0-9_\\-\\+]+[@][A-z0-9_\\-]+([.][A-z0-9_\\-]+)+[A-z]{1,4}$/", $str);
}
?>

Warning: require(phpmailer/class.phpmailer.php) [function.require]: failed to open stream: No such file or directory in /home/thecreat/public_html/construction/submit.php on line 2

Line 2 points to a PHPMailer script, I don’t have that script after checking the form configuration file, their is no options for setting up the mailer to where the email should be sent as well as why this PHP error message is occurring instead of the javascript error message.

So where did you get this code if you don’t have the required file?

I know it probably isn’t ideal for your purpose but have you considered just using the mail() function directly or was there a reason why you wanted to use a class?

Is this why you didn’t want to post your code - because you didn’t have the class file? I hope not because i wanted to help and was trying to do so and from my POV you were trying to ignore my effort - despite asking for help!

PHP Mailer is open source and freely available, so go get it :slight_smile:

Worx International Inc.

Also, it’s simpler, more secure, and less of an overall headache to just turn magic quotes off in the .htaccess file than rely on the stripslashes function.

So where did you get this code if you don’t have the required file?

I got the PHP script from a site that was related to forms.

I know it probably isn’t ideal for your purpose but have you considered just using the mail() function directly or was there a reason why you wanted to use a class?

Are you asking why the form is in a class?

Is this why you didn’t want to post your code - because you didn’t have the class file? I hope not because i wanted to help and was trying to do so and from my POV you were trying to ignore my effort - despite asking for help!

If it relates to my previous question.

PHP Mailer is open source and freely available, so go get it

Worx International Inc.

Also, it’s simpler, more secure, and less of an overall headache to just turn magic quotes off in the .htaccess file than rely on the stripslashes function.

It appears as though I have two directions to get this to work as well as to get the email to actually send, which is disable magic quotes or install PHP Mailer, correct ?

No, to get your code to work you have one option - get php mailer :wink:

The rest is probably a good idea too though.

No, i wasn’t asking why your form was in a class, i was asking why it relies on a class you didn’t have. I was asking if you had considered just using the mail() function - a native php function that anyone can use without using a special class.

I don’t have the option of installing PHP mailer on my server I do have two other options;
Sendmail or PHP mail() but I don’t know how to install PHP mail()

[FPHP]mail/FPHP is a native function - if PHP is running, mail() is defined - now, whether it WORKS or not is up to your host, and whether they’ve configured it correctly.

PHPMailer doesnt have to be ‘installed’ in any way, it’s just a bunch of pages you upload to your server.

mail() uses sendmail by default.

mail() is a default function with php. You just call it like any other function.

Best to make sure its not been disabled first though so run this code to check:


if (function_exists('mail'))
   {
   print 'Mail() is available';
   }
else
   {
   print 'No Mail() function';
   }

The response is mail() is available.

Calling the raw mail function is viable only if you are willing to write the code to filter the inputs and prevent your script from becoming a open proxy for spam. If you don’t know how to write such filters I wouldn’t recommend trying on a production system. Just use the class which has those filters built in.

Ok, that means its available to you and your scripts.

Mike has a good point, you might want to look into filtering (EG if you have a contact form which allows someone to put in their email) but i would recommend first that you just get some working which uses mail(). Once it works, then go back and research how to secure it (eg come back here and ask).

For the time being, get your basic outgoing emails working otherwise you’ll spend ages debugging filters etc before you even get it working.

How do I get it working, I thought after what I had coded that it would have been working that is why I’m stuck !?! :slight_smile: