Help with an emailer... very new to this

So I have a contact form on a simple website I’m making for work, I bought an html template, managed to get everything working, maps etc, but the “contact us” form on the page… I feel like an idiot as I just keeping getting nothing. I have a file labeled php with the emailer seemingly working from here, and despite changing the values I get nothing, I’m thinking I’ve done something stupid, or missed something, any help would be dearly appreciated, as I’m a photoshop guy, and my boss has decided I’m now the website guy. If you could help me figure out how to make it mail to "[email protected]" (can’t put the company name here sorry), I’d be hugely happy. Or if anyone could point to me to something showing where I’ve failed I’d be happy to learn, I’m just getting a lot of work hassle for apparently not being a programmer and this not working. Thank you anybody! (If it means anything, we’ve paid for every template, tool, and service we have, we do support those who build what we use). PHP text below…
[php]

<?php $to = htmlspecialchars( $_POST['email'] ); $name = htmlspecialchars( $_POST['name'] ); $subject = "Contact Form, testsite - $name"; $from = htmlspecialchars( $_POST['email'] ); $message = htmlspecialchars( $_POST['message'] ); $error = ''; //check fields if($name == ''){ $error .= 'Please enter your name!'; } if($from == ''){ $error .= 'Please enter your email!'; }elseif(!preg_match('|^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$|i', $from) ){ $error .= 'Your email address is not valid. '; } if($message == ''){ $error .= 'You forgot to write your message!'; } //send mail or show error messages if($error != ''){ echo json_encode(array('sent' => 0, 'message' => $error )); }else{ $headers = 'From: '.$name.'<'. $from .'>'."\r\n". 'Reply-To: '.$from."\r\n" . 'X-Mailer: PHP/' . phpversion(); if(@mail($to, $subject, $message, $headers)){ echo json_encode(array('sent' => 1, 'message' => "Thank you! We will contact you as soon as possible." )); }else { echo json_encode(array('sent' => 0, 'message' => "Error: Please try again later!" )); } }[/php]

if you look at this:

[php]$to = htmlspecialchars( $_POST[‘email’] );
$name = htmlspecialchars( $_POST[‘name’] );
$subject = “Contact Form, testsite - $name”;
$from = htmlspecialchars( $_POST[‘email’] );
$message = htmlspecialchars( $_POST[‘message’] );[/php]

you can see the variables being set. their names are instantly recognizable as well (to, name, subject, from, message). htmlspecialchars formats them. $_POST is the information coming FROM the html form. look closer, the $to variable and the $from variable are BOTH being set by the EMAIL field in the html form.

what you want then, is to replace that post information with [email protected], for example. so the above code becomes this:
[php]$to = ‘[email protected]’;
$name = htmlspecialchars( $_POST[‘name’] );
$subject = “Contact Form, testsite - $name”;
$from = htmlspecialchars( $_POST[‘email’] );
$message = htmlspecialchars( $_POST[‘message’] );[/php]

you can also easily edit the subject there too. i actually came the other way and started with coding and had to learn design, and i’m still a much better coder :wink: but the thing about code is that once you recognize the logic to it the rest flows. and php is especially legible in the world of code, so if you have any interest (or no choice ;)) then it’s a great place to start in my opinion.

you see this @ in this line: if(@mail
get rid of it!

The @ symbol suppresses errors as they happen usually resulting in scratchin of heads to see what went wrong.

Red :wink:

Sponsor our Newsletter | Privacy Policy | Terms of Service