php sending email - php

mailer.php
<?php
if(isset($_POST['submit'])) {
$to = "abc#gmail.com";
$subject = "Contact via website";
$name_field = $_POST['name'];
$email_field = $_POST['email'];
$message = $_POST['message'];
$body = "From: $name_field\n E-Mail: $email_field\n Message:\n $message";
echo "1";
echo "Data has been submitted to $to!";
mail($to, $subject, $body);
} else {
echo "0";
}
?>
jquery code
$('.submit').click(function(){
$('span.msg').css({'visibility': 'visible'}).text('Sending...');
$.post("mailer.php", $(".contactPage").serialize(),
function(data){
$('span.msg').text((data == "1") ? 'Your Message has been received. Thank you' : "Could not send your message at this time").show();
});
return false;
});
I am always getting
Could not send your message at this time
I have almost no knowledge in php, whats i am doing wrong?

Your php script never prints just 1 it prints 0 or 1Data has been submitted to... thus (data == "1") can never be true.
btw: Your script could at least use the return value of mail() to decide whether it signals success or failure.

First of all, check server's mail settings. If your script resides on a server, which you don't administer, the simplest way is to do this is creating a simple PHP file with the following content:
<?php
mail("youraccount#example.com","test","works"); <br>
?>
Run it and check your e-mail.

Related

Problems with contact form and input validation

Ok so I am trying to create a contact form that validates user input to keep hackers from submitting codes and trying to require number, text, and email only. I have already styled the form and imported my php file. The contact from will send it to my gmail account. but everytime I test the php It allows for any type of data to be entered no matter if it is supposed to be a number and letters are submitted and the other way around. If I could get some help in telling me where I went wrong that would be great. I am a beginner at programming and only have the knowledge I recieved from school but I'm pretty good at html and css but having problems with the php validation. The form sends the email but like I said it allows any and all input.
<?php
$name = $_POST['name'];
$tel = $_POST['tel'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: www.webdesignheros.com';
$to = 'heenanwrk#gmail.com';
$subject = 'Service Email for HeenanTech';
$tel = filter_input(INPUT_POST, 'tel', FILTER_SANITIZE_INT);
$name = filter_input(INPUT_POST, 'name', FILTER_SANITIZE_STRING|FILTER_FLAG_NO_ENCODE_QUOTES);
$email = filter_input(INPUT_POST, 'email', FILTER_VALIDATE_EMAIL);
$message = filter_input(INPUT_POST, 'message', FILTER_SANITIZE_STRING|FILTER_FLAG_NO_ENCODE_QUOTES);
$body = "From: $name\n Phone: $tel\n Email: $email\n Message: $message\n";
?>
<?php
if ($_POST['submit']){
if(mail($to, $subject, $body, $from)){
echo'<p>Thank you for your email!</p>';
} else {
echo '<p> Oops! Something went wrong, try sending your message again</p>';
}
}
?>
Additionally the form can be found at [http://webdesignheros.com/Contact.html][1]
[1]: http://webdesignheros.com/Contact.html and if someone could tell me how to reject certain input before the submit that would be awesome too. like if an invalid entry was input and they move on to the next input it would reject it and not let the submit button be pushed. would i use the pattern="a-z" in the html or would i need to add javascript for that?
<?php
if (isset($_POST["submit")){
$name = $_POST["name"];
$tel = $_POST["tel"];
$email = $_POST["email"];
$message = $_POST["message"];
$from = "From: www.webdesignheros.com";
$to = "heenanwrk#gmail.com";
$subject = "Service Email for HeenanTech";
$body = "From: $name\n Phone: $tel\n Email: $email\n Message: $message\n";
mail($to, $subject, $body, $from);
if(mail($to, $subject, $body, $from)){
echo'<p>Thank you for your email!</p>';
} else {
echo '<p> Oops! Something went wrong, try sending your message again</p>';
}
}
?>

My web hosting company says there is something wrong with PHP code

This is the PHP it suppose to connect to a contact.html page and then a thank you page after afterwards.I just wanted a contact form. it wont recognize files as .php.I did save it like that.
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$number = $_POST['number'];
$message = $_POST['message'];
$from = 'From:you';
$to = 'me#hotmail.com';
$subject = 'Hello';
$human = $_POST['human'];
$body = "From: $name\n E-Mail: $email\n Number: $number\n Message:\n $message";
if ($_POST['submit']) {
if ($name != '' && $email != '') {
if ($human == '4') {
if (mail ($to, $subject, $body, $from)) {
echo '<p>Your message has been sent!</p>';
} else {
echo '<p>Something went wrong, go back and try again!</p>';
}
} else if ($_POST['submit'] && $human != '4') {
echo '<p>You answered the anti-spam question incorrectly!</p>';
}
} else {
echo '<p>You need to fill in all required fields!!</p>';
}
header("Location: thanks.html");
}
?>
Assuming that you wish to run this from a form, you will need to set your HTML form tag as follows:
<form action="contact.php" method="post">
You should then rename contact.html to contact.php (any text editor should be able to do this easily).
Finally, you're using PHP's header() function, which will cause errors if you have output to the browser before it is called. This includes using PHP's echo struct. Your contact.php file should look like this (and be in the same directory as your HTML file containing the form):
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$number = $_POST['number'];
$message = $_POST['message'];
$from = 'From:you';
$to = 'me#hotmail.com';
$subject = 'Hello';
$human = $_POST['human'];
$body = "From: $name\n E-Mail: $email\n Number: $number\n Message:\n $message";
if ($_POST['submit']) {
if ($name != '' && $email != '')
{
if ($human == '4')
{
if (mail ($to, $subject, $body, $from))
{
header("Location: thanks.html");
}
else
{
echo '<p>Something went wrong, go back and try again!</p>';
}
}
else
{
echo '<p>You answered the anti-spam question incorrectly!</p>';
}
}
else
{
echo '<p>You need to fill in all required fields!!</p>';
}
}
?>
Note: I fixed your layout a little and changed some of the conditions that you were using. The first elseif was actually redundant, and an else will suffice.
You wrote that it's a file named contact.html or you try to connect to contact.html - is this correct? You should rather use contact.php.
PHP will not execute in .html file extensions without server configuration (for example, a directive in a .htaccess file if you're using Apache).
Since you appear to have a managed web hosting account, you may not be able to set this yourself. If you would like it, I would suggest asking your hosting provider. If not, renaming the file to have a .php file extension should work.
Try to check out if php is working:
<?php phpinfo(); ?>
If you see no output, your php-code won't be parsed, maybe because your contact-site has an *.html ending, it seemingly needs a *.php file ending, to be parsed,
if you want to use the extension '*.html', you have to add some lines to your webserver - configuration file,
have a look at this: maybe a solution

php with form alert box

I would like this php form to pop up an alert box on completion or if there is error.
What do I need to add or change to this to make that happen, or is that added javascript?
<?php
$name = $_POST['fullname'];
$email = $_POST['email'];
$message = $_POST['comment'];
$from = 'From: Contact Form';
$to = 'email#domain.com';
$subject = 'Hello';
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
if (mail ($to, $subject, $body, $from)) {
echo '<p>Your message has been sent!</p>';
} else {
echo '<p>Something went wrong, go back and try again!</p>';
}
?>
To do this you will need to add some more technologies, specifically Javascript and AJAX.
PHP is a sever-side language, which means that by the time the browser is rendering the page PHP has completed running your code.
In the page that would display the error, you would add javascript to pop up the message using the alert() function. You could use php to conditionally output this javascript. Oy vey.
Example:
<script language='javascript'>
<?php if ($error) { ?>
window.onload = function() {
alert('<?php echo $error?>');
}
<?php } ?>
</script>
Something like that.

display error from jquery post

i am posting to a mailer.php file.
mailer.php
<?php
if(isset($_POST['submit'])) {
$to = "testabc#gmail.com";
$subject = "Contact via website";
$name_field = $_POST['name'];
$email_field = $_POST['email'];
$message = $_POST['message'];
$body = "From: $name_field\n E-Mail: $email_field\n Message:\n $message";
echo "Data has been submitted to $to!";
mail($to, $subject, $body);
} else {
echo "blarg!";
}
?>
here is my js code
$('.submit').click(function(){
$('span.msg').css({'visibility': 'visible'}).text('Sending...');
$.post("mailer.php", $(".contactPage").serialize(),
function(data){
$('span.msg').text('Your Message has been received. Thank you').show();
});
return false;
});
I am getting message of success but email is not received. What i am doing wrong? How to get error detail from mailer.php file and showing in span.msg?
if(mail($to, $subject, $body)){
echo "success";
}else{
echo "fail";
}
JS:
$('.submit').click(function(){
$('span.msg').css({'visibility': 'visible'}).text('Sending...');
$.post("mailer.php", $(".contactPage").serialize(),
function(data){
if(data == 'success'){
$('span.msg').text('Your Message has been received. Thank you').show();
}else{
$('span.msg').text('Your Message has failed. Thank you').show();
}
});
return false;
});
On this page under the Return Values section it says:
Returns TRUE if the mail was successfully accepted for delivery, FALSE otherwise.
It is important to note that just because the mail was accepted for delivery, it does NOT mean the mail will actually reach the intended destination.
So just check if it is returning true or false.
EDIT: Also try checking your spam folder. Perhaps someone on your shared hosting provider is sending spam.

php simple contact form not sending mail even after confirmation

I have been trying to get a php contact form working on my portfolio site (currently on a free megabyet.net account), but on testing it(on the uploaded site) even though i get the thankyou/confirmation message, I still don't receive any message on my mail account (specified in the code), I can't seem to understand the problem here....help needed!
can it be something related to SMTP??
Here's the code :
<?php
if(isset($_POST['submit'])) {
$to = "vishu_unlocker#yahoo.com";
$subject = "Portfolio Contact";
$name_field = $_POST['name'];
$email_field = $_POST['email'];
$message = $_POST['message'];
$headers = "From: $email_field";
$body = "From: $name_field\n E-Mail: $email_field\n Message:\n $message";
echo "Mail has been sent, thankyou!";
mail($to, $subject, $body, $headers);
} else {
echo "blarg!";
}
?>
HTML Code:
<form id="contact_frm" action="mail.php" method="POST">
<h4>Name :</h4>
<input type="text" id="f_name" name="name"/><br/><br/>
<h4>E-Mail Address :</h4>
<input type="text" id="f_email" name="email"/><br/><br/>
<h4>Message :</h4>
<textarea id="f_msg" name="message" cols="22" rows="5"/></textarea><br/><br/>
<input id="send_btn" type="submit" value="Send >>" name="submit" /><br/>
</form>
Firstly you should be checking if mail() returns true or not to determine if mail has been sent successfully:
<?php
if(isset($_POST['submit'])) {
$to = "vishu_unlocker#yahoo.com";
$subject = "Portfolio Contact";
$name_field = $_POST['name'];
$email_field = $_POST['email'];
$message = $_POST['message'];
$headers = "From: $email_field";
$body = "From: $name_field\n E-Mail: $email_field\n Message:\n $message";
$success = mail($to, $subject, $body, $headers);
if ($success) {
echo "Mail has been sent, thankyou!";
// redirect to thank you page here
}
else {
echo "message failed";
}
} else {
echo "blarg!";
}
?>
Try that and let us know if that works.
Also, have you tried sending to a different email address? It may be that Yahoo is blocking that web host for spam. Being a free host it is a very likely scenario.
If you are looking for something related to sending email via SMTP. I would recommend you use Code Igniters mailer class.
http://codeigniter.com/user_guide/libraries/email.html
This also allows for debugging and handling SMTP errors gracefully.
can it be something related to SMTP??
Probably. Why don't you check your mailq and the log files from your MTA?
#John .. checked with that if condition with the code below and i get a failed output =/ ...so my mail() function is returning false =( ...and yea i've tried gmail but with the mail function not running fine on the first place.... it doesn't work...
<?php
if(isset($_POST['submit'])) {
$to = "vishu_unlocker#yahoo.com";
$subject = "Portfolio Contact";
$name_field = $_POST['name'];
$email_field = $_POST['email'];
$message = $_POST['message'];
$headers = "From: $email_field";
$body = "From: $name_field\n E-Mail: $email_field\n Message:\n $message";
$success = mail($to, $subject, $body, $headers);
if($success) {
echo "Mail has been sent, thankyou!";
} else {
echo "message sending failed!";
}
} else {
echo "blarg!";
}
?>
output- message sending failed!
so, do I need to define some extra params here?...also i saw that my host has given the path to sendmail as -- /usr/sbin/sendmail does it has anything to do with my mail function acting bad?...i mean do I need to define the sendmail param in it?
#unknown- hmm codeigniter may help, but i've never used it before...let's see...
#symcbean- sorry i don't know how to do that :P...probabaly cuz i'm not very well versed with SMTP yet?.... still a learner/beginner...
If the E-Mail goes out correctly, but never arrives, it could be that it gets caught by a spam filter. A few bullet points I wrote in reply to an similar question a few months ago:
Does the sender address ("From") belong to a domain on your server? If not, make it so.
Is your server on a blacklist (e.g. check IP on spamhaus.org)? This is a remote possibility with shared hosting.
Are mails filtered by a spam filter? Open an account with a freemailer that has a spam folder and find out. Also, try sending mail to an address without a spam filter.
Do you possibly need the fifth parameter "-f" of mail() to add a sender address? (See mail() command in the PHP manual)
If you have access to log files, check those, of course, as suggested above.
Do you check the "from:" address for possible bounce mails ("Returned to sender")? You can also set up a separate "errors-to" address.

Categories