spottedpixel Posted October 19, 2013 Share Posted October 19, 2013 Hello, I am a newbie to php scripting. I have been using Larry Ullman's PHP for the Web (4th edition) to learn scripting and has been useful up to this point. I have been working on a contact page and I cannot get the script to send the email with the information, or at least I am not receiving the email. What am I doing wrong? Here is the script that I have written: <!DOCTYPE html PUBLIC "-//W3C//DTD Xhtml 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"><head><meta http=equiv="Content-Type" Content="text/html; charset=utf-8"/><title>Email Handle</title><style type="text/css" media="screen">.error { color: red' }</style></head> <body><?php // Script 1.0 - email_handle.php /* This script receives four values from contact.html: name, email, comments */ // Flag variable to track success:$okay = TRUE; // Set Variables:$name = $_POST['name'];$email = $_POST['email'];$comments = $_POST['comments']; // validate the name and strip tags:if (empty($_POST['name'])) {print '<p class="error">Please enter your name.</p>';} // validate the email address and strip tags:if (empty($_POST['email'])) {print '<p class="error">Please enter your email address.</p>';} // Validate email at:if (empty($_POST['email']) || (substr_count($_POST['email'], '@') != 1) ) {$problem = TRUE;print '<p class="error">Please enter a valid email address.</p>';} // Validate the comment and strip tags:if (empty($_POST['comments'])) {print '<p class="error">Please enter your comments.</p>';} // Send the email:if ($okay = true) {mail('someone@somewhere.com', '$comments', '$email');} // Print a message:if ($okay) {print '<p>Thank you for your inquiry.</p>';print '<p>Someone from our organization will get back to you as soon as possible.</p>';} ?></html> I have placed a fake email address for now, but when I actually use the script I am using an active email address. Any help would be greatly appreciated. Quote Link to comment Share on other sites More sharing options...
spottedpixel Posted October 19, 2013 Author Share Posted October 19, 2013 I should mention that I had my firewall off and I an running through a router. Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted October 20, 2013 Share Posted October 20, 2013 the php mail() function is not a mail server. it does not send emails (though when using smtp there's small chance that some mail servers may accept an unauthenticated email for delivery.) it is an interface function that lets php communicate with your sending mail server that has been configured to accept emails from your web server. do you have a mail server installed that will accept emails from php on your web server and is a correctly configured public mail server that can then send those emails to the receiving mail server at the to: address? if you are trying to use your ISP's mail server or a mail server like gmail, or to send to your mail account on some mail server, you will need to use smtp authentication to do so and php's mail() function doesn't support smtp authentication. you will need to use a mailer class like phpmailer to use smtp authentication to send through your mail account on a mail server or to send to your account on a mail server. Quote Link to comment Share on other sites More sharing options...
JayStabins Posted October 20, 2013 Share Posted October 20, 2013 ^this I cannot like or otherwise +1 mac_gyver but the fact of the matter is you really need a production server or setup some "advanced" setup to do this from your local machine. If you have a web host try it on their server, if not it is probably a setting in your php.ini. A lot of servers require you to have the "From" header assigned to one of their servers to help prevent spam. Quote Link to comment Share on other sites More sharing options...
spottedpixel Posted October 20, 2013 Author Share Posted October 20, 2013 Thanks both. I have spent hours today trying to get Mercery on xampp to send emails. Oh, how fustrating. I will try running it on my server instead then. How do you use the "From" header and how do I make sure that my server is set-up to send the email? Quote Link to comment Share on other sites More sharing options...
JayStabins Posted October 20, 2013 Share Posted October 20, 2013 Here is a sample of what I use for a simple email send $emailServer = 'me@myDomain.com'; $emailFrom = 'myName'; $to = "\"" . $CleanMessage->getSender() . "\" <" . $CleanMessage->getEmail() . ">"; $message = $CleanMessage->getMessage(); // To send HTML mail, the Content-type header must be set $headers = "From: \"" . $emailFrom . "\" <" . $emailServer . ">\n"; $headers .= "MIME-Version: 1.0" . "\n"; $headers .= "Content-type: text/html; charset=iso-8859-1" . "\r\n"; if(mail($to, $CleanMessage->getTitle(), $message, $headers)){ //message sent; }else { die('Message not sent die mother fucker!'); } Not complete and I have my message in a class objec called $CleanMessage, this has all my prepossessing done and has just been inserted into a MySql database but you get the point... Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.