MyBB Community Forums

Full Version: PHP mail problem
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
ok I got a mail script here..
http://www.aqua.hostmatrix.org/contact.php

but I can't make the "Your Name" part to work!
How do you include the name in a PHP mail?
So when I receive the mail,
it would be like this:

From: Joe Smith <[email protected]>

rather than

From: <[email protected]>
Make sure there are no typo's, so, make sure $_POST['sender'] exists. Also, you should do a few checks to see if any of the forms are empty. because, if not, you're going to b getting alot of empty e-mail in your Inbox. Smile
I see!!!

which way do I put it though?

$message = $_POST['sender'], $_POST['message'];

or a new field on top
$message = $_POST['sender']

Thanks for helping! (gives rep)
I can't figure this out...

Can you tell me where and how should I add it? Thanks a lot!!! Smile

<?



if (isset($_POST['from']))

{

$ip = $_SERVER['REMOTE_ADDR'];

$resolved = gethostbyaddr($ip);

if ($resolved != $ip)

{

$footer = "This e-mail was sent by a user with the IP address $ip, which resolved to '$resolved'.";

}

else

{

$footer = "This e-mail was sent by a user with the IP address $ip, which could not be resolved to a hostname.";

}

$message = $_POST['message'];

$message = "$message\n\n------------\n$footer";



if (mail('[email protected]', $subject, $message, "From: $from\r\nReply-To: $from\r\n"))

{

echo 'Your mail was successfully sent.';

}

else

{

echo 'There was an error while sending the mail.';

}

}

else

{

?><form method="post" action="<? echo $_SERVER['PHP_SELF']; ?>">

<p>Please be aware that your IP address (<? echo $_SERVER['REMOTE_ADDR'] ?>) will be sent along with the message 

to prevent against abuse of this mail form.</p>

<table border="0" cellspacing="0" cellpadding="3">

<tr> 

<td><b><font face="Verdana, Arial, Helvetica, sans-serif" size="2">Your Name</font></b><br>

<i><font face="Trebuchet MS, Verdana, Tahoma, Arial, sans-serif" size="1">Leave 

this field blank if you don't have a name.<br>

<input type="text" name="sender" size="70">

</font></i></td>

</tr>

<tr> 

<td><b><font face="Verdana, Arial, Helvetica, sans-serif" size="2">Your 

e-mail address:</font></b><i><font face="Trebuchet MS, Verdana, Tahoma, Arial, sans-serif" size="1"><br>

<input type="text" name="from" size="70">

</font></i></td>

</tr>

<tr> 

<td><b><font face="Verdana, Arial, Helvetica, sans-serif" size="2">Subject:</font></b>

<i><font face="Trebuchet MS, Verdana, Tahoma, Arial, sans-serif" size="1"><br>

<input type="text" name="subject" size="70">

</font></i></td>

</tr>

<tr> 

<td><font face="Verdana, Arial, Helvetica, sans-serif" size="2"><b>Message:<br>

<textarea name="message" rows="10" cols="70"></textarea>

</b></font></td>

</tr>

<tr>

<td align="right"> <font face="Verdana, Arial, Helvetica, sans-serif" size="2">Click 

&quot;Send&quot; only once.</font><br>

<input type="submit" value="Send">

</td>

</tr>

</table>

</form>

<?
Try to sneak this in there...

Quote: $from = $_POST['sender'];

EDIT I saw that you already seen what i meant

Try this:

Quote: $message = $_POST['sender'] && $_POST['message'];
Cory, I have never seen that be done.

AQuarius, try this:

<?

$sender  = $_POST['sender'];
$from    = $_POST['from'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$message = "". $message ."\n\n------------\n". $footer ."";

if (!empty($sender) && !empty($from) && !empty($subject) && !empty($message)) {
	$ip = $_SERVER['REMOTE_ADDR'];
	$resolved = gethostbyaddr($ip);

	if ($resolved != $ip) {
		$footer = "This e-mail was sent by a user with the IP address $ip, which resolved to '$resolved'.";
	} else {
		$footer = "This e-mail was sent by a user with the IP address $ip, which could not be resolved to a hostname.";
	}


	if (mail("[email protected]", $subject, $message, "From: ". $from ."\r\nReply-To:".  $from ."\r\n")) {
		echo "Your mail was successfully sent.";
	} else {
		echo "There was an error while sending the mail.";
	}
} else {
	?>
<form method="post" action="<? echo $_SERVER['PHP_SELF']; ?>">

<p>Please be aware that your IP address (<? echo $_SERVER['REMOTE_ADDR'] ?&gt<img src="http://community.mybboard.net/images/smilies/wink.gif" align="middle" border="0" alt="Wink" /> will be sent along with the message

to prevent against abuse of this mail form.</p>

<table border="0" cellspacing="0" cellpadding="3">

<tr>

<td><b><font face="Verdana, Arial, Helvetica, sans-serif" size="2">Your Name</font></b><br>

<i><font face="Trebuchet MS, Verdana, Tahoma, Arial, sans-serif" size="1">Leave

this field blank if you don't have a name.<br>

<input type="text" name="sender" size="70">

</font></i></td>

</tr>

<tr>

<td><b><font face="Verdana, Arial, Helvetica, sans-serif" size="2">Your

e-mail address:</font></b><i><font face="Trebuchet MS, Verdana, Tahoma, Arial, sans-serif" size="1"><br>

<input type="text" name="from" size="70">

</font></i></td>

</tr>

<tr>

<td><b><font face="Verdana, Arial, Helvetica, sans-serif" size="2">Subject:</font></b>

<i><font face="Trebuchet MS, Verdana, Tahoma, Arial, sans-serif" size="1"><br>

<input type="text" name="subject" size="70">

</font></i></td>

</tr>

<tr>

<td><font face="Verdana, Arial, Helvetica, sans-serif" size="2"><b>Message:<br>

<textarea name="message" rows="10" cols="70"></textarea>

</b></font></td>

</tr>

<tr>

<td align="right"> <font face="Verdana, Arial, Helvetica, sans-serif" size="2">Click

&quot;Send&quot; only once.</font><br>

<input type="submit" value="Send">

</td>

</tr>

</table>

</form>

	<?php
}

?>
Your problem was brought to my attention by eXaulz. In a moment of boredom I decided to re-write and optimize your script.

Here:

<?php

if ( !empty( $_POST['SENDER'] ) && !empty( $_POST['from'] ) && !empty( $_POST['subject'] ) && !empty( $_POST['message'] ) ) {
	
	if ( gethostbyaddr( $_SERVER['REMOTE_ADDR'] ) != $_SERVER['REMOTE_ADDR'] ) {
		
		$host = gethostbyaddr( $_SERVER['REMOTE_ADDR'] );
		
		$footer = "This e-mail was sent by a user with the IP address {$_SERVER['REMOTE_ADDR']}, which resolved to &quot;{$_POST['host']}&quot;";
		
	} else {
		
		$footer = "This e-mail was sent by a user with the IP address {$_SERVER['REMOTE_ADDR']}, which could not be resolved to a hostname.";
		
	}
	
	if ( mail("[email protected]", $_POST['subject'], $_POST['message'], "From: ". $_POST['from'] ."\r\nReply-To:". $_POST['from'] ."\r\n") ) {
		
		echo "Your mail was successfully sent.";
		
	} else {
		
		echo "There was an error while sending the mail.";
		
	}
	
} else {
	
	$output = <<<HED

<form method="post" action="{$_SERVER['PHP_SELF']}">
<p>Please be aware that your IP address ({$_SERVER['REMOTE_ADDR']} ?&gt<img src="http://community.mybboard.net/images/smilies/wink.gif" align="middle" border="0" alt="Wink" /> will be sent along with the message to prevent against abuse of this mail form.</p>
<table border="0" cellspacing="0" cellpadding="3">
	<tr>
		<td>
			<b><font face="Verdana, Arial, Helvetica, sans-serif" size="2">Your Name</font></b><br>
			<i><font face="Trebuchet MS, Verdana, Tahoma, Arial, sans-serif" size="1">Leave this field blank if you don"t have a name.<br>
			<input type="text" name="sender" size="70">
			</font></i>
		</td>
	</tr>
	<tr>
		<td>
			<b><font face="Verdana, Arial, Helvetica, sans-serif" size="2">Your e-mail address:</font></b><i><font face="Trebuchet MS, Verdana, Tahoma, Arial, sans-serif" size="1"><br>
			<input type="text" name="from" size="70">
			</font></i>
		</td>
	</tr>
	<tr>
		<td>
			<b><font face="Verdana, Arial, Helvetica, sans-serif" size="2">Subject:</font></b>
			<i><font face="Trebuchet MS, Verdana, Tahoma, Arial, sans-serif" size="1"><br>
			<input type="text" name="subject" size="70">
			</font></i>
		</td>
	</tr>
	<tr>
		<td>
			<font face="Verdana, Arial, Helvetica, sans-serif" size="2">
				<b>
					Message:<br>
					<textarea name="message" rows="10" cols="70"></textarea>
				</b>
			</font>
		</td>
	</tr>
	<tr>
		<td align="right">
			<font face="Verdana, Arial, Helvetica, sans-serif" size="2">Click&quot;Send&quot; only once.</font><br>
			<input type="submit" value="Send">
		</td>
	</tr>
</table>
</form>

HED;

echo $output;
	
}

?>

Or for a better view you can go to:
View Page
Thanks a lot for you guys' help!!!!

But I still can't get it to work Sad
I tried eXaulz's script.. but the name still wont appear in my maii

I tried Jason's script.. but it won't go to the "Send Complete" page!

Ahh I can't make it work! I guess I'll just stay with the original Sad
Have you ever tried the "Magic Form Mail"? It's basically a PHP form generator. It's really good and easy. No big extra generated pages, so really, all you have to do is copy and paste the php code into your contact.php file (where it's suitable).
I can't seem to figure out what the problem is, so, if you don't mind, I think I'm just going to re-write the entire file. I have your email, so I'll just send you a copy of the file once it's finished.

Quote: $message = $_POST['sender'] && $_POST['message'];

I don't like making a habit of flaming people, but please, don't ever speak about PHP again, the above quoted answer was nothing less than an embarrassment to anyone who may call themselves a programmer.
Pages: 1 2