9 Replies - 3492 Views - Last Post: 02 December 2009 - 11:03 AM Rate Topic: -----

#1 Daily.matters   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 42
  • Joined: 20-November 09

Help in php simple contact form

Posted 01 December 2009 - 10:42 AM

So i'm starting in php, i'm trying to do a simple contact form, that sends the information to my email, but when i hit the submit button i get this error:

Warning: mail() [function.mail]: "sendmail_from" not set in php.ini or custom "From:" header missing in C:\xampp\htdocs\ContactForm\mailer.php on line 12

Currently my php code is looking like this:

<?php
	if(isset($_POST['submit'])){
		$to = "[email protected]";
		$subject = "Message From Contact Form";
		$name_field = $_POST['name'];
		$email_field = $_POST['email'];
		$message = $_POST['message'];

		$body = "From: $name_field\n E-Mail: $email_field\n Message: $message";
	
		echo "Data has been submitted to [email protected]";
		mail($to, $subject, $body);
	}
	
	else{
		echo "Please submit data using the contact form";
	}
?>



I don't understand why that error is appearing, i'm using xampp to run the php and it seems that is everything al with it.

Can you guys help me ?

Thx in advance, ClŠudio

Is This A Good Question/Topic? 0
  • +

Replies To: Help in php simple contact form

#2 Zel2008   User is offline

  • D.I.C Addict

Reputation: 17
  • View blog
  • Posts: 893
  • Joined: 06-January 09

Re: Help in php simple contact form

Posted 01 December 2009 - 10:47 AM

Hi Claudio,
Your problem is how the body is structured. If you want to have a "From" field in your mail, you have to have it separate from your body. Basically, use the mail function like this:

mail( $to, $subject, $body, "From: whoever you want." );



Hope that helps,
Zel2008
Was This Post Helpful? 0
  • +
  • -

#3 RudiVisser   User is offline

  • .. does not guess solutions
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,566
  • Joined: 05-June 09

Re: Help in php simple contact form

Posted 01 December 2009 - 10:48 AM

Make sure that the server you're using (the SMTP Server) allows sending of outgoing mail from the address you use, otherwise it will just throw another error.
Was This Post Helpful? 0
  • +
  • -

#4 Daily.matters   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 42
  • Joined: 20-November 09

Re: Help in php simple contact form

Posted 01 December 2009 - 11:22 AM

View PostRudiVisser, on 1 Dec, 2009 - 09:48 AM, said:

Make sure that the server you're using (the SMTP Server) allows sending of outgoing mail from the address you use, otherwise it will just throw another error.


When i went to see the status of my xampp and the SMTP services are not active. Is that the problem ?
Was This Post Helpful? 0
  • +
  • -

#5 hadi_php   User is offline

  • D.I.C Regular
  • member icon

Reputation: 10
  • View blog
  • Posts: 382
  • Joined: 23-August 08

Re: Help in php simple contact form

Posted 01 December 2009 - 11:26 AM

<?php
	if(isset($_POST['submit'])){
$to	  = '[email protected]';
$subject = 'Message From Contact Form';
$message = $_POST['message'];
$headers = 'From: [email protected]' . "\r\n" .
	'Reply-To: [email protected]' . "\r\n" .
	'X-Mailer: PHP/' . phpversion();

mail($to, $subject, $message, $headers);
echo "Data has been submitted to [email protected]";

	}
	
	else{
		echo "Please submit data using the contact form";
	}
?>


u can make it more secure by giving some condition like

after isset then

if(!empty($_POST['messege']))
{ 
	 your code;
}
else die();

Was This Post Helpful? 0
  • +
  • -

#6 Daily.matters   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 42
  • Joined: 20-November 09

Re: Help in php simple contact form

Posted 01 December 2009 - 11:44 AM

Quote

Warning: mail() [function.mail]: Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set() in C:\xampp\htdocs\ContactForm\mailer.php on line 10


The problem seems to be with the SMTP port, i'm going to try and see if i can activate the SMTP services.
Was This Post Helpful? 0
  • +
  • -

#7 hadi_php   User is offline

  • D.I.C Regular
  • member icon

Reputation: 10
  • View blog
  • Posts: 382
  • Joined: 23-August 08

Re: Help in php simple contact form

Posted 01 December 2009 - 12:05 PM

if u are trying to send mail via your pc i mean localhost then you can not. Upload this code in any server[paid] then u can. from home u can not.
Was This Post Helpful? 1
  • +
  • -

#8 Daily.matters   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 42
  • Joined: 20-November 09

Re: Help in php simple contact form

Posted 01 December 2009 - 12:09 PM

Thanks, i had no idea about that, i was trying to use init_set() but i was still having problems.

Thank you all for the help.
Was This Post Helpful? 0
  • +
  • -

#9 hadi_php   User is offline

  • D.I.C Regular
  • member icon

Reputation: 10
  • View blog
  • Posts: 382
  • Joined: 23-August 08

Re: Help in php simple contact form

Posted 02 December 2009 - 01:09 AM

<?php
echo 'Max execution time = ' . ini_get('max_execution_time') . "\n";
//look in ur php.ini there u ll see max_execution_time=60
if (!ini_get('max_execution_time')) {
	ini_set('max_execution_time', 120); // here max_execution_time is 120 which is set via ini_set
}
// in this way you can change ini variable value
?>
 

<?php
//more example
ini_set('zlib.output_compression', 'Off');
?> 

This post has been edited by hadi_php: 02 December 2009 - 01:10 AM

Was This Post Helpful? 0
  • +
  • -

#10 s-grind   User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 86
  • Joined: 20-November 09

Re: Help in php simple contact form

Posted 02 December 2009 - 11:03 AM

View Posthadi_php, on 1 Dec, 2009 - 11:05 AM, said:

if u are trying to send mail via your pc i mean localhost then you can not. Upload this code in any server[paid] then u can. from home u can not.


That is nonsence.

You can sent mail from your own pc, unless you made the right modifications to php.ini:

from php.ini:

[mail function]
; For Win32 only.
; http://php.net/smtp
SMTP = smtp.YOUR_PROVIDER.com (works for me!!)
; http://php.net/smtp-port
smtp_port = 25

; For Win32 only.
; http://php.net/sendmail-from
sendmail_from = [email protected]_domain.net (I used my own emailadress here)

when you set these settings right, you can send mail from localhost without any problem, I tested it with this code:

$to = "[email protected]"; // I used an real testaccount
        $subject = "Message From Contact Form";
        $name_field = $_POST['name']; //I changed $name_field = "my name";
        $email_field = $_POST['email']; //I changed $email_field = "[email protected]";
        $message = $_POST['message']; // I changed $message = "notification from me";

        $body = "From: $name_field\n E-Mail: $email_field\n Message: $message";
    
        echo "Data has been submitted to [email protected]";
        mail($to, $subject, $body);


Works like a charm.
(despite the security risks etc.

By the way; if you make some modifications to php.ini, don't forget to stop and start apache...
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1