php message problems need help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tikalacarnes
Forum Newbie
Posts: 7
Joined: Sat May 05, 2007 6:53 pm

php message problems need help

Post by tikalacarnes »

Weirdan | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Ok here is my problem. I am fairly new to php and I have written a script that will be used to send me an email from a person on my site. the problem is that when i try to load the page it loads blank and I get nothing in an error file. I have no access to the .htaccess file do to the site being on a remote server not my own. so I am going to give the code to both files that are being used. there permissions are also set to 774.

message.php file:

Code: Select all

<?php
	include "message.inc";
	$contact = new contactform();
	$contact->DisplayForm();
?>
message.inc file:

Code: Select all

<?php
// First, make sure the form was posted from a browser.
// For basic web-forms, we don't care about anything
// other than requests from a browser:   
	if(!isset($_SERVER['HTTP_USER_AGENT'])){
	die("Forbidden - You are not authorized to view this page");
	exit;
	}
	// Attempt to defend against header injections:
	$badStrings = array("Content-Type:",
					 "MIME-Version:",
					 "Content-Transfer-Encoding:",
					 "bcc:",
					 "cc:");
	// Loop through each POST'ed value and test if it contains
	// one of the $badStrings:
	foreach($_POST as $k => $v){
	foreach($badStrings as $v2){
	   if(strpos($v, $v2) !== false){
		   logBadRequest();
		   header("HTTP/1.0 403 Forbidden");
			   exit;
	   }
	}
}   

// Made it past spammer test, free up some memory
// and continue rest of script:   

unset($k, $v, $v2, $badStrings);

class contactform
{
	var $toaddress;
	var $name;
	var $email;
	var $subject;
	var $message;
	var $errmsg;
	var $error;
	var $outmessage;
	var $headers;
	
	function contactform()
	{
		$this->toaddress = "webmaster@newberryautomotive.com";
//		$this->toaddress = "webmaster@newberryautomotive.com,rnewby22@yahoo.com";

		if(isset($_POST['sendmail']))
			$this->parseform();
		$this->debugLog();
		$this->outmessage = "";
		$this->headers = "";
		$this->email = "";

	function parseform()
	{
		if ($_POST['email'])
		{
			$this->email = $_POST['email'];
		}
		else
		{
			$this->error = 1;
			$this->errmsg = "You must enter your email addres.";
		}
		if ($_POST['name'])
		{
			$this->name = $_POST['name'];
			if (!$this->error)
			{
				$this->headers .= "From: " . $this->email . "\r\n";
//				$this->senders .= "From: " . $this->email . " (" . $this->name . ")\r\n";
//				$this->headers .= "From: " . $this->name . " <" . $this->email . ">\r\n";
				$this->outmessage .= "From: " . $this->name . "\n";
//				$this->outmessage .= "Email: " . $this->email . "\n";
			}
		}
		else
		{
			$this->error = 1;
			$this->errmsg = "You must enter your name.";
		}
		if ($_POST['message'])
		{
			$this->subject .= "Message From: " .$this->name;
			$this->message = $_POST['message'];
			$this->outmessage .= "Message:\n" . $this->message;
		}
		else
		{
			$this->error = 1;
			$this->errmsg = "You must enter your message.";
		}
		if (!$this->error)
		{
			if (!preg_match("/Content\-Type\:/i", $this->headers))
//			if ($this->subject)
//			if (!strstr($this->subject,$this->toaddress))
			{
//				if (mail($this->email, $this->toaddress, $this->subject, $this->outmessage)) //doesn't work!
				if (mail($this->toaddress, $this->subject, $this->outmessage, $this->email)) //this works but uses my email for the From: header.
//				if (mail($this->toaddress, $this->subject, $this->headers, $this->outmessage))
//				if (mail($this->headers, $this->toaddress, $this->subject, $this->outmessage))
				{
					$this->errmsg = "Your message was sucessfully sent. Do NOT Refresh this page!";
				}
				else
				{
					$this->errmsg = "Your message was <b>NOT</b> sent. " . $this->email . " Was not accepted. Please try again later.";
				}
			}
		}
		$this->debugLog();
	}
	
	function DisplayForm()
	{
?>

<br>
<table width="90%" border="0" cellspacing="0" cellpadding="0" align="center">
	<tr>
		<td colspan="2" align="left" valign="top"><?php
		if (isset($this->errmsg))
		{
			print '<h3><center><b>' . $this->errmsg . '</h3></center></b><p><br>';
		}
?>
			<form action="feedback2.php" method="post" name="FormName" enctype="application/x-www-form-urlencoded">
				<font face="arial" color="black" size="3"><center>

				<b>* Name:</b><br>
				<input type="text" name="name" size="34" border="0">
				</dl>
				
				<dl>
				<b>* Email Address:</b><br>
				<input type="text" name="email" size="34" border="0"><p>
				</dl>

				<dl>
				<b>* Message</b>:<br>
				<textarea name="message" rows="12" cols="54"></textarea><p><p>
				</dl>

				<input type="submit" name="sendmail" value="Send Message" border="0">
				<input type="reset" value="Clear Form">
				</dl>
				</form><br>
				<center>* A required field.</center>
			</td>
		</tr>
	</table>
	<br>
	<br>
<?php
}

	function debugLog()
	{
		if (!($fp = fopen('/tmp/debug.txt', 'w')))
			return;

		$this->showvalues("ENV", &$_ENV, $fp);
        $this->showvalues("SESSION", &$_SESSION, $fp);
        $this->showvalues("COOKIE", &$_COOKIE, $fp);
        $this->showvalues("SERVER", &$_SERVER, $fp);
        $this->showvalues("POST", &$_POST, $fp);
        $this->showvalues("GET", &$_GET, $fp);

		fclose($fp);
	}

    function showvalues($prefix, $array, $file)
    {
            foreach($array as $key => $value)
            {
                    fwrite($file, sprintf("%s: %s => %s\n", $prefix, $key, $value));
//                  fwrite($file, sprintf("%s: %s %s (%s)%s-%s", $toaddress, $name, $email, $phoneareacode, $phoneprefix, $phonesuffix));
//                  fwrite($file, sprintf("%s: %s %s %s %s \n", $messagetype, $subject, $comment, $errmsg, $error));
            }
    }
}

?>
so if there is anyone that understands .inc files along with php then please help.

Weirdan | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Thanks
email@removed.com 

[quote="[url=http://forums.devnetwork.net/viewtopic.php?t=30037]Forum Rules[/url] Section 1.1"][b]7.[/b] Do not divulge anyone's personal information in the forum - [b]including your own[/b]. This includes [b]e-mail addresses[/b], IP addresses, age, house address, and any other individual information.[/quote]
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

add:

Code: Select all

error_reporting(E_ALL);
ini_set('display_errors', true);
to the top of your php file (after <?php, but before include).
tikalacarnes
Forum Newbie
Posts: 7
Joined: Sat May 05, 2007 6:53 pm

Post by tikalacarnes »

cool that lets me know where the error is but unfortuneately the line that it says is:

Parse error: parse error, unexpected ';', expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /message.inc on line 186

line 186 ?>

and that is the end of the file
tikalacarnes
Forum Newbie
Posts: 7
Joined: Sat May 05, 2007 6:53 pm

Post by tikalacarnes »

Thanks a lot I was able to find the error in the file and now it works like a champ. You are a life saver.
tikalacarnes
Forum Newbie
Posts: 7
Joined: Sat May 05, 2007 6:53 pm

Post by tikalacarnes »

ok it doesn't work like a champ for some reason I cannot send the message it gives me this error when I try to send it:

Code: Select all

Your message was NOT sent. "my email" Was not accepted. Please try again later.
and I think it has something to do with this code:

Code: Select all

//				if (mail($this->email, $this->toaddress, $this->subject, $this->outmessage)) //doesn't work!
				if (mail($this->toaddress, $this->subject, $this->outmessage, $this->email)) //this works but uses my email for the From: header. // does not work now.
//				if (mail($this->toaddress, $this->subject, $this->headers, $this->outmessage))
//				if (mail($this->headers, $this->toaddress, $this->subject, $this->outmessage))
				{
					$this->errmsg = "Your message was sucessfully sent. Do NOT Refresh this page!";
				}
				else
				{
					$this->errmsg = "Your message was <b>NOT</b> sent. " . $this->email . " Was not accepted. Please try again later.";
				}
			}
So if anyone can help me out with this one to it would be great.
sentback
Forum Newbie
Posts: 24
Joined: Fri May 04, 2007 9:46 am

Post by sentback »

Not sure but I think you just can't have spaces in you email address. Didn't really look into the code though :D
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Try using the Swiftmailer package. It is great for handling mails and was written by our very own d11wtq.

And FYI, when you get a blank page that is almost a sure sign of a fatal error (usually a parse error) in your code. The Server error logs will typically show this error.
tikalacarnes
Forum Newbie
Posts: 7
Joined: Sat May 05, 2007 6:53 pm

Post by tikalacarnes »

Everah wrote:Try using the Swiftmailer package. It is great for handling mails and was written by our very own d11wtq.

And FYI, when you get a blank page that is almost a sure sign of a fatal error (usually a parse error) in your code. The Server error logs will typically show this error.
I don't know anything about this package I downloaded it and opened it and it was just a single file with no extension. I have no idea of how to use the file you have to remember I am very new to php I wrote this email script and it use to work I don't know what happened to change it but now all of my pages that I wrote don't work. There were a total of three for different sites and they were just changed around a little to reflect what site it was for and for some reason none are working now.

Code: Select all

<?php
	error_reporting(E_ALL);	// add this to print the error that is causing the page to not be viewable.
	ini_set('display_errors', true);

	include "message.inc";
	$contact = new contactform();
	$contact->DisplayForm();
?>
this is the debugging script that i am using and it isn't giving me any script error now.

as far as my email addresses they do not have spaces in them. the email addresses haven't changed from one site to the other. here is the web address of the simple message page if you are wanting to check it out for yourself. I am at a loss and it is driving me crazier than what I am already.

http://www.newberryautomotive.com/message.php

I have an error message that I have displayed after you click on the send button and I see no reason why it shouldn't go through.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Swift is a package, so the download should have had a file called Swift.php, once called EasySwift.php and a collection of folders, the most important of which is the /Swift folder. If you read the documentation for swift on the Swift site, you will find that send a mail is as simple as including the EasySwift file, setting a few vars and mailing.

The problem you are experiencing with your code as it was originally posted is that the recipient mailbox is rejecting the mail for some reason. mail() will not tell you that reason, but Swift will.
tikalacarnes
Forum Newbie
Posts: 7
Joined: Sat May 05, 2007 6:53 pm

Post by tikalacarnes »

Well you must have given me a bad link then cause all I get even when I enter it manually is a file to be downloaded and it is only 1kb in size after it is downloaded. I have none of the stuff that you say is there. When I try to open it in my editor I get some weird functions.

Webmaster at NewberryAutomotive dot Com
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Let me get in touch with d11 about this. Give it until tomorrow.

In the meantime, visit the sourceforge project page for more... http://sourceforge.net/projects/swiftmailer
Post Reply