lugos Posted January 8, 2007 Share Posted January 8, 2007 Hello,I am fairly new to PHP development and I'm trying to send e-mails using the following code:<?php$to = "myemailaddress@myisp.com";$subject = $_POST['subject'];$body = $_POST['message'];$headers = "From: " . $_POST['emailAddress'] ."\n";if (mail($to,$subject,$body,$headers)) { echo("<p>Message sent</p>");} else { echo("<p>Failed to send message</p>");}?>For some reason it does not work. I keep getting the failure message. I have verified that all of the values are being passed.Any help would be greatly appreciated.Thanks in advance. Link to comment Share on other sites More sharing options...
jlhaslip Posted January 8, 2007 Share Posted January 8, 2007 Can you do a phpinfo() on the hosting account and see if the mail() is enabled or disabled? Link to comment Share on other sites More sharing options...
magic24688 Posted January 8, 2007 Share Posted January 8, 2007 Ok, I ran your script and didnt get a Failed to send message, but I do see some things wrong that you dont need.Try to change the:$headers = "From: " . $_POST['emailAddress'] ."\n";To:$headers = "From: $_POST['emailAddress']";The varible will still be called with a $_POST that way to.ALso the echo() the () are not required seens it is not a function like if () so you can write it as:echo "<p>Message sent</p>";But the way you had it it still ran fine for me. Link to comment Share on other sites More sharing options...
lugos Posted January 8, 2007 Author Share Posted January 8, 2007 Ok, I ran your script and didnt get a Failed to send message, but I do see some things wrong that you dont need.Try to change the:$headers = "From: " . $_POST['emailAddress'] ."\n";To:$headers = "From: $_POST['emailAddress']";The varible will still be called with a $_POST that way to.ALso the echo() the () are not required seens it is not a function like if () so you can write it as:echo "<p>Message sent</p>";But the way you had it it still ran fine for me.I'm trying to run it locally using Apache on Win XP . Is there anything I have to do to the php.ini file? Do I have to set the SMTP server? I also read somewhere that PHP does not support SMTP authentication. Is that correct?Also, I am hosting my site using a Linux web server that I set up myself. The distribution of Linux is Ubuntu. It fails to send messages on the web server also.Thanks. Link to comment Share on other sites More sharing options...
justsomeguy Posted January 8, 2007 Share Posted January 8, 2007 ALso the echo() the () are not required seens it is not a function like if () so you can write it as:'if' is not a function, it's a statement, like echo. Parens are optional for echo, it will not do anything differently if they are or are not there.You need a mail server configured with PHP. The mail server should be installed on the same machine as the web server is installed on, and there are some options in php.ini that you need to set up to specify details about the mail server or the path to sendmail (a mail program). Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now