PHP Mailform dosen't work anymore

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

oliver23
Forum Newbie
Posts: 7
Joined: Sat Jul 15, 2006 6:49 am

PHP Mailform dosen't work anymore

Post by oliver23 »

feyd | 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]


Hi

I have a problem with my simple PHP Mailform. Since I moved it on a new server it doesen't work anymore. Maybe it's another PHP Version, I don't know. The mail sending works, but I don't get the content. Can someone help?

It looks like that:

Code: Select all

<?
   	/* Brief generieren und versenden */
	
	  $z = 1;
	  $z++;$b[$z]="firma  :".$firma."\n";
	  $z++;$b[$z]="realname  :".$realname."\n";
	  $z++;$b[$z]="senderemail  :".$senderemail."\n";
	  $letter = implode($b,"");
	
	   /* Email an Betreiber der Site */
	 mail("name@email.ch","Kontaktformular",$letter,"from:$senderemail"); 
	?>

feyd | 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]
User avatar
aerodromoi
Forum Contributor
Posts: 230
Joined: Sun May 07, 2006 5:21 am

Re: PHP Mailform dosen't work anymore

Post by aerodromoi »

oliver23 wrote:Hi

I have a problem with my simple PHP Mailform. Since I moved it on a new server it doesen't work anymore. Maybe it's another PHP Version, I don't know. The mail sending works, but I don't get the content. Can someone help?

It looks like that:

Code: Select all

<?
   	/* Brief generieren und versenden */
	
	  $z = 1;
	  $z++;
          $b[$z]="firma  :".$firma."\n";
	  $z++;
          $b[$z]="realname  :".$realname."\n";
	  $z++;
          $b[$z]="senderemail  :".$senderemail."\n";
	  $letter = implode($b,"");
	
	   /* Email an Betreiber der Site */
	 mail("name@email.ch","Kontaktformular",$letter,"from:$senderemail"); 
	?>
You're relying on register_globals on. You'll have to switch to the superglobal post array.

Code: Select all

$firma = $_POST['firma'];
btw:
+ How about using the pre-increment operator (++$z) instead of filling multiple lines?
+ Don't put the email address of the sender in the mail header unless you've validated it.
+ Even though implode accepts the arguments in any order, I'd recommend using implode("",$b).
Last edited by aerodromoi on Sat Jul 15, 2006 7:19 am, edited 1 time in total.
xeno439
Forum Newbie
Posts: 15
Joined: Tue Nov 22, 2005 12:48 am

Post by xeno439 »

Is the new server running a different version of PHP? PHP 5 or something?
User avatar
aerodromoi
Forum Contributor
Posts: 230
Joined: Sun May 07, 2006 5:21 am

Post by aerodromoi »

xeno439 wrote:Is the new server running a different version of PHP? PHP 5 or something?
The default value for register_globals has been "off" since 4.2.0 (which means a few years back).
oliver23
Forum Newbie
Posts: 7
Joined: Sat Jul 15, 2006 6:49 am

Re: PHP Mailform dosen't work anymore

Post by oliver23 »

aerodromoi wrote:
You're relying on register_globals on. You'll have to switch to the superglobal post array.

Code: Select all

$firma = $_POST['firma'];
btw:
+ How about using the pre-increment operator (++$z) instead of filling multiple lines?
+ Don't put the email address of the sender in the mail header unless you've validated it.
+ Even though implode accepts the arguments in any order, I'd recommend using implode("",$b).
Thanks for your help. Yes it is PHP5 now!
But I still doesen't get it work. How should it look the way you recommend? Sorry, I'm a PHP Newbie.
User avatar
aerodromoi
Forum Contributor
Posts: 230
Joined: Sun May 07, 2006 5:21 am

Re: PHP Mailform dosen't work anymore

Post by aerodromoi »

Code: Select all

<?php
// fetch the variables from the post array
$firma = $_POST['firma'];
$letter = $_POST['letter'];
$realname = $_POST['realname'];
$senderemail = $_POST['senderemail'];
?>
oliver23
Forum Newbie
Posts: 7
Joined: Sat Jul 15, 2006 6:49 am

Re: PHP Mailform dosen't work anymore

Post by oliver23 »

Sorry, I still don't get it. I can't believe it, the old PHP was much easier. Even I search for google I find only 2 pages forms :(

What I have now ist the following, and the good thing is I see the from email now. But not the content.

Code: Select all

<?php 
// fetch the variables from the post array 
$firma = $_POST['firma']; 
$letter = $_POST['letter']; 
$realname = $_POST['realname']; 
$senderemail = $_POST['senderemail'];

	
/* Email an Betreiber der Site */ 
mail("name@domain.ch","Kontaktformular",$letter,"from:$senderemail");  
?>
Last edited by oliver23 on Sat Jul 15, 2006 9:46 am, edited 1 time in total.
User avatar
aerodromoi
Forum Contributor
Posts: 230
Joined: Sun May 07, 2006 5:21 am

Re: PHP Mailform dosen't work anymore

Post by aerodromoi »

oliver23 wrote:Sorry, I still don't get it. I can't believe it, the old PHP was much easier.
And potentially exploitable.
You might want to take a look at this article:
http://www.jellyandcustard.com/2006/02/ ... on-in-php/
oliver23 wrote:What I have now ist the following, and the good thing is I see the from email now. But not the content.
Looks like a misspelt variable. What's the html sourcecode?
oliver23
Forum Newbie
Posts: 7
Joined: Sat Jul 15, 2006 6:49 am

Re: PHP Mailform dosen't work anymore

Post by oliver23 »

feyd | 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]


this ist my html form:

[syntax="html"]<head>
<title>Untitled Document</title>
</head>

<body>

<h2>Ihre Anfrage ist uns willkommen!</h2>
<p>Was können wir für Sie tun?</p>
<form method="post" action="senden.php" name="Kontaktformular">
<h2>Ihre Angaben</h2>
<p></p>
<table width="525" border="0" cellspacing="1" cellpadding="0">
    <tr>
    <td valign="top" width="100" class="tabdunkel">Firma
	</td>
    <td valign="top" class="tabdunkel">
	<input type="text" name="firma" class="inputfeld" />
	</td>
  	</tr>
	<tr>
    <td valign="top" width="100" class="tabdunkel">Name *
	</td>
    <td valign="top" class="tabdunkel">
	<input type="text" name="realname" class="inputfeld" />
	</td>
  	</tr>
	<tr>
    <td valign="top" width="100" class="tabdunkel">E-Mail *
	</td>
    <td valign="top" class="tabdunkel">
	<input type="text" name="senderemail" class="inputfeld" />
	</td>
  	</tr>
</table>

<p><input type="submit" name="submit" value="Senden" class="button" /></p>
</form>

</body>
</html>

feyd | Please use[/syntax]

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]
User avatar
aerodromoi
Forum Contributor
Posts: 230
Joined: Sun May 07, 2006 5:21 am

Re: PHP Mailform dosen't work anymore

Post by aerodromoi »

oliver23 wrote: What I have now ist the following, and the good thing is I see the from email now. But not the content.
Because there is no such thing as a textarea for any kind of message in your html form.
You've only got three input fields (for the name, the company and an email address).
oliver23
Forum Newbie
Posts: 7
Joined: Sat Jul 15, 2006 6:49 am

Re: PHP Mailform dosen't work anymore

Post by oliver23 »

aerodromoi wrote: Because there is no such thing as a textarea for any kind of message in your html form.
You've only got three input fields (for the name, the company and an email address).
Yes, all I want is to send an Email with this 3 Information to my emailadress:

Name: Content
Company: Content
Email: Content
User avatar
aerodromoi
Forum Contributor
Posts: 230
Joined: Sun May 07, 2006 5:21 am

Re: PHP Mailform dosen't work anymore

Post by aerodromoi »

oliver23 wrote:
aerodromoi wrote: Because there is no such thing as a textarea for any kind of message in your html form.
You've only got three input fields (for the name, the company and an email address).
Yes, all I want is to send an Email with this 3 Information to my emailadress:

Name: Content
Company: Content
Email: Content

Code: Select all

<?php
// fetch the variables from the post array
$firma       = $_POST['firma'];
$realname    = $_POST['realname'];
$senderemail = $_POST['senderemail'];

$recipient = "name@domain.ch";
$subject   = "Kontaktformular";
$letter    = "Email: " . $senderemail . "\n";
$letter   .= "Firma: " . $firma . "\n";
$letter   .= "Name: " . $realname;
$header    = "X-Mailer: PHP " . phpversion();

// check whether all fields have been filled in
if (empty($firma) || empty($realname) || empty($senderemail)){
  print("Sorry, all fields are required!");
}
else{       
  $result = @mail($recipient,$subject,$letter,$header); 
  $result ? print ("Thanks!") : print ("An error occurred!");
}
?>
I've removed the email address of the sender from the header tag as it is likely to be exploited without validation.

ps: Man kann auch aneinander vorbeireden ;)
oliver23
Forum Newbie
Posts: 7
Joined: Sat Jul 15, 2006 6:49 am

Re: PHP Mailform dosen't work anymore

Post by oliver23 »

aerodromoi wrote:ps: Man kann auch aneinander vorbeireden ;)
Vielen vielen Dank! It works now! I can't believe, I spend 6 hours for that. :oops:

Great help, great forum thanks!
User avatar
aerodromoi
Forum Contributor
Posts: 230
Joined: Sun May 07, 2006 5:21 am

Re: PHP Mailform dosen't work anymore

Post by aerodromoi »

Code: Select all

<?php
// fetch the variables from the post array
$firma       = $_POST['firma'];
$realname    = $_POST['realname'];
$senderemail = $_POST['senderemail'];

$recipient = "name@domain.ch";
$subject   = "Kontaktformular";
$letter    = "Email: " . $senderemail . "\n";
$letter   .= "Firma: " . $firma . "\n";
$letter   .= "Name: " . $realname;

if (ereg('(cc\:|bcc\:|to\:)', strtolower($senderemail))){
  $header    = "X-Mailer: PHP " . phpversion();
}
else{
  $header    = "From: " . $senderemail;
}

// check whether all fields have been filled in
if (empty($firma) || empty($realname) || empty($senderemail)){
  print("Sorry, all fields are required!");
}
else{       
  $result = @mail($recipient,$subject,$letter,$header); 
  $result ? print ("Thanks!") : print ("An error occurred!");
}
?>
Just to get you started: I've modified the code somewhat to check whether someone has tried to insert additional to/cc/bcc fields.

Gern geschehen!
oliver23
Forum Newbie
Posts: 7
Joined: Sat Jul 15, 2006 6:49 am

Re: PHP Mailform dosen't work anymore

Post by oliver23 »

aerodromoi wrote: Just to get you started: I've modified the code somewhat to check whether someone has tried to insert additional to/cc/bcc fields.
Gern geschehen!
Perfekt! :D

One more question, is it possible to forward it to another site (danke.html) if the sending was okay? A redirect instead of

Code: Select all

print ("Thanks!")
Post Reply