Contact Form to email

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
alwayslearning
Forum Newbie
Posts: 3
Joined: Sat Oct 01, 2011 11:32 pm

Contact Form to email

Post by alwayslearning »

Hello all. I am a new member to this forum and also new to php. Hopefully this community of programers can help me with my basic needs.

When I submit my form, I recieve this error:
Warning: mail() [function.mail]: "sendmail_from" not set in php.ini or custom "From:" header missing in C:\xampp\htdocs\formTest.php on line 23

I get this ^ warning because of this line of code: ini_set ("sendmail_from","myemailaddress@yahoo.com");

Ive read through other forums about my php.ini file and I don't know if i should change any of these defaults. Currently I am testing on localhost but when I get everything running I will put it on my websites server. My question is, how can I get this to send the email through localhost first, and then what setting need to be changed when I decide to put it on a server?

Thanks for any help possible.

My php.ini for the mail()function is this:

[mail function]
; For Win32 only.
; http://php.net/smtp
SMTP = localhost
; http://php.net/smtp-port
smtp_port = 25

; For Win32 only.
; http://php.net/sendmail-from
;sendmail_from = postmaster@localhost

Just for the heck of it, heres all my php code. its only 30 lines or so.

<?php


if (array_key_exists('send', $_POST)) {
// mail processing script
$to = 'myemailaddress@yahoo.com';
$subject = 'Feedback from form';

// Process the $_POST variables
$name = $_POST['name'];
$email = $_POST['email'];
$comments = $_POST['comments'];

// Build the message
$message = "Name: $name\r\n\r\n";
$message .= "Email: $email\r\n\r\n";
$message .= "Comments: $comments";

// Limit the line length
$message = wordwrap($message, 150);

// Send it
$mailSent = mail($to, $subject, $message);
ini_set ("sendmail_from","myemailaddress@yahoo.com");
}
?>
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Contact Form to email

Post by Celauran »

Move the ini_set to before you try sending the mail.
alwayslearning
Forum Newbie
Posts: 3
Joined: Sat Oct 01, 2011 11:32 pm

Re: Contact Form to email

Post by alwayslearning »

So I moved ini_set('sendmail_from','myemailaddress@yahoo.com'); to the first line in my php block and I recieve this error:

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\dcservices\send_contact.php on line 26

line 26 is this: $send_contact = mail($to,$message,$header);

any ideas? Thanks
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Contact Form to email

Post by Celauran »

Is your mail server up and running?
alwayslearning
Forum Newbie
Posts: 3
Joined: Sat Oct 01, 2011 11:32 pm

Re: Contact Form to email

Post by alwayslearning »

How could I check that and if It is not how do I set up it up. I believe i ran a info(); and it said yes but I will do some research until I hear again. Thank you
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Contact Form to email

Post by Celauran »

I'm not familiar with XAMPP, but Google has plenty of info.
User avatar
Pazuzu156
Forum Contributor
Posts: 241
Joined: Sat Nov 20, 2010 9:00 pm
Location: GA, USA
Contact:

Re: Contact Form to email

Post by Pazuzu156 »

If you are using something such as Yahoo or Gmail, you need to enable SMTP and IMAP settings through their mail settings before you can do that on a local server. XAMPP or not.

Also it would help to have the headers in there too.

Example:

Code: Select all

<?php

$to = "email@domain.com";
$header = "FROM: email@localhost";
$subject = "My Subject";
$message = "This is the message I am sending to you!";

@mail($to,$subject,$message,$header);

?>
Let me know if that works.
- Kaleb Klein
------------------------------------
Web Developer | Software Developer
https://kalebklein.com
PGP Key: https://keybase.io/pazuzu156
Post Reply