php, check if mail is valid - php

I know that there is a ton of solutions to this problem but i need to validate email in a contact form.
this is my php mailer so far:
<?php
if (isset($_POST['send'])){
$to = "mail#mail.something";
$subject = "new message";
$firstname = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$headers = "From: $email";
$sent = mail($to, $subject, $message, $headers) ;
if($sent)
{echo "<script>alert('thanks for the message:) ');</script>";
}else
{echo "<script>alert('sorry, message wasn't send');</script>"; }
}
?>
I have tried with filter_var
$result = filter_var( 'something#something.something', FILTER_VALIDATE_EMAIL );
but, doesn't work.
If my way to sent mail through contact form isn't the "best practice way" feel free to correct me and send me on the right path :D

If you show us your HTML we can help further, but with just your PHP we don't have much to work with.
Add error_reporting(E_ALL); and ini_set('display_errors', 1); to the top of your php page to print your errors.
I am not sure what is causing your errors. But i would like you to try the following code. Replace your if statement with this.
Future note, to add a javascript alert in php you should format it this way,
if(mail($to, $subject, $message, $headers))
{
echo '<script language="javascript">'; //echo open tag
echo 'alert("Your message has been sent")'; // echo alert
echo '</script>'; //echo close tag
}else{
echo '<script language="javascript">';
echo 'alert("Your message was not successful")';
echo '</script>';
}

Related

PHP mail() function hosted on iPage [duplicate]

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 7 years ago.
<?php
$su="";
if(isset($_POST['submit']))
{
$name=$_POST['name'];
$email=$_POST['email'];
$subject=$_POST['subject'];
$message=$_POST['message'];
$to='jikkas#gmail.com';
$subject = 'the subject';
$message = 'Subject: '.$subject.'</br></br></br>'.$message;
$headers = 'Name: '.$name.'</br>'.'Email: '.$email;
$send= mail($to, $subject, $message, $headers);
if ($send)
{
$su="Thanks for being with us...";
}
}
?>
This my email function. But it's not working. My site is hosted on ipage server.
For starters, your code needs tidying up, so I have done this for you. Also, what do you mean it's not working? You won't know if it is working because you haven't told it to tell you. You have just set the variable $su and not done anything with it. Here is what your code should look like:
<?php
$su="";
if(isset($_POST['submit']))
{
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$to = 'jikkas#gmail.com';
$subject = 'the subject';
$message = 'Subject: '.$subject.'</br></br></br>'.$message;
$headers = 'Name: '.$name.'</br>'.'Email: '.$email;
$send = mail($to, $subject, $message, $headers);
if($send)
{
$su = "Thanks for being with us...";
echo $su;
}
else
{
$su = "Error: unable to send";
echo $su;
//See note below
}
}
?>
I have included an else statement in there just as an example if you want to use your own error handling function. However, error_reporting should ideally be set to E_ALL to ensure you are capturing any errors that may be occurring. Also, ensure you have the correct mail settings in your php.ini. Look here for more details on mail settings in php.ini.
I would recommend running the following script:
<?php
// Set error reporting level to all errors
error_reporting(E_ALL);
// View php.ini and look for the mail settings
phpinfo();
?>
Hope this helps!
I think you had to create a Mail Account in the Panel.

PHP form to returning all the information

I am a complete novice when it comes to php, I have got the below php which sends me back the 'message' and sends an auto response to the user, as well as redirecting them to the 'thank you' page. Problem I am having is that it won't return the users name that they fill in on the form, any ideas?
<?php
$youremail = "ally.baird81#gmail.com"; //this is where the email will be sent to
#extract($_POST);$name = filter_var($name, FILTER_SANITIZE_STRING);
$message = filter_var($message, FILTER_SANITIZE_STRING);
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
if (mail($youremail, 'Message from website.', $message, "From: Krew Kut Hair<$email>")) {
$autoreply = "Thank you for enquiring at Krew Kut Hair, we will be in contact shortly";
$subject = "Thank you for your enquiry!";
mail($email, $subject, $autoreply, "From: Krew Kut Hair<$email>");
}
} else {
echo "Please enter a valid email address";
}
header("Location: thanks.html");
Assuming the name is in one of the form fields, you should be able to retrieve it. As Barmar says - all you have to do is use it somewhere in the body or the message. How can you tell the name is missing if you don't echo it out somewhere.
Try this:
$autoreply = "Thank you ".$name." for ...
If the name is still "missing" - you can try to see all the post variables like this:
echo "<PRE>Post Vars\n"; print_r($_POST);
If you have an input named "name" like:
<input type="text" name="name" value="" />
Check if it's containing data with e.g. :
echo 'The value of name is ['.$name.']';
If it is containing data you just can use the $name variable in your message. If it isn't there is probably something wrong in your HTML form.
<?php
$youremail = "ally.baird81#gmail.com"; //this is where the email will be sent to
#extract($_POST);$name = filter_var($name, FILTER_SANITIZE_STRING);
$message = filter_var($message, FILTER_SANITIZE_STRING);
$content = "<strong>Name:<strong><br />".$name."<br />";
$content .= "<strong>Message:<strong><br />".$message."<br />";
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
if (mail($youremail, 'Message from website.<br />', $content, "From: Krew Kut Hair<$email>")) {
$autoreply = "Hi ".$name.". Thank you for enquiring at Krew Kut Hair, we will be in contact shortly";
$subject = "Thank you for your enquiry!";
mail($email, $subject, $autoreply, "From: Krew Kut Hair<$email>");
}
} else {
echo "Please enter a valid email address";
}
header("Location: thanks.html");
Also read the comments on your question. I strongly recommend to find an other way instead of using extract().

localhost contact form not working?

It saying i have a mistake and I've done everything right i hope. I'm using localhost for now.. may that be the problem? if not what is.
I get an error on this row
$name = $_POST['name'];
code:
<?php
$name = $_POST['name'];
$to = "Lennyperez#mail.com";
$subject = "add this";
mail ($to, $subject, $name);
?>
<form action="submit.php" method="post" name="contact_form">
<input name="name" id="name" type="text">
<input type="submit" name="submit" id="sumitbtn" value="submit">
</form>
There could be a number of factors as to why you are having difficulties with your mail form.
Check to see if mail() is installed and properly configured on your system.
Use additional headers(); one being a From: which is missing in your code, although not mandatory.
Mail will still attempt to send, however it could end up being sent to SPAM or ignored.
Use conditional statements for your submit button and mail() success, including checking if the field is left empty or not.
The following has been successfully tested, sent and received in my INBOX.
PHP
<?php
if (isset($_POST['submit']) && !empty($_POST['name'])){
$name = $_POST['name'];
$to = "email#example.com";
$subject = "add this";
$headers="From: $name <email#example.com>";
$sent = mail($to, $subject, $name, $headers);
if($sent) {
echo "Success.";
} else {
echo 'Sorry, your message could not be sent.';
}
} // brace for submit conditional statement
?>
You can also use the following:
<?php
if (isset($_POST['submit']) && !empty($_POST['name'])){
$name = $_POST['name'];
$from = "user#example.com";
$to = "your#example.com";
$subject = "add this";
$headers="From: $name <$from>";
$sent = mail($to, $subject, $name, $headers);
if($sent) {
echo "Success";
} else {
echo 'Sorry, your message could not be sent.';
}
} // brace for submit conditional statement
?>
Footnotes:
Check your server logs.
mail() doesn't work on localhost without an external library.
Also, you need to do an isset on $_POST['name'];
if (isset($_POST)){
$name = $_POST['name'];
$to = "email#email.com";
$subject = "this";
mail ($to, $subject, $name);
}
Also don't forget to look at headers!
Make this work for you first:
<?php
mail("Lennyperez#mail.com", "add this", "name");
, and ask question again.
Do the following: until the form is not submitted, the $_POST['name'] variable is not set
<?php
if (isset($_POST)) {
$name = $_POST['name'];
$to = "Lennyperez#mail.com";
$subject = "add this";
mail ($to, $subject, $name);
}
?>
When the page is loaded , it doesn't find post variables and that's normal because you have not clicked the submit button,your $_POST['name'] will exist only after the submit button is pressed.
Solution 1:
Add a condition , mail will be sent only if a button submit is clicked.
<?php
if (isset($_POST['name'])) {
$name = $_POST['name'];
$to = "Lennyperez#mail.com";
$subject = "add this";
mail ($to, $subject, $name);
}
?>
Solution 2:
Separate HTML file containing the form from Your action page
I am assuming you are receiving undefined notice if so please fallow one of these steps.
Add #$name= $_POST['name'];
OR
error_reporting(E_ALL ^ E_NOTICE);

How to redirect to success / fail pages after php form submission? [duplicate]

This question already has answers here:
php redirect after form success
(3 answers)
Closed 9 years ago.
I have successfully integrated the following form submission code into my site, and it works great. However I would like to have the code redirect the user to one page if the form submission is successful, and a different page if it fails. How could I adapt the following code to do that? It's really starting to get on my nerves! :-P
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$enquiry = $_POST['enquiry'];
$formcontent=" From: $name \n Phone: $phone \n Message: $enquiry";
$recipient = "email#email.co.uk";
$subject = "Contact Form";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "Thank You!";
?>
EDIT:
Ok I have changed the code to:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$enquiry = $_POST['enquiry'];
$formcontent=" From: $name \n Phone: $phone \n Message: $enquiry";
$recipient = "email#email.co.uk";
$subject = "Contact Form";
$mailheader = "From: $email \r\n";
if(mail($recipient, $subject, $formcontent, $mailheader)){
header("Location: mailer-success.htm");
}else{
header("Location: mailer-fail.htm");
}
exit;
?>
This works however it never goes to the fail page. I'm guessing that's because the email is always sent even if fields are empty. I have jquery verification in place (which I have disabled for testing purposes) but that obviously only works for users with javascript enabled. How could I alter the code to only show the success page if the form fields contain data? Any help is appreciated.
add a redirect to the page:
// Test to see if variables are empty:
if(!empty($name) && !empty($email) && !empty($phone) && !empty($enquiry)){
// Test to see if the mail sends successfully:
if(mail($recipient, $subject, $formcontent, $mailheader)){
header("Location: success.php");
}else{
header("Location: error.php");
}
}else{
header("Location: back_to_form.php");
}
mail function returns true/false when succeeds/fails. So it's rather simple:
if (mail($recipient, $subject, $formcontent, $mailheader)) {
header('location: success.php');
} else {
header('location: fail.php');
}
Add
header("Location: successpage.html");
to the bottom of your code and remove echo "Thank you!";
Get rid of the echo and then use a header:
header("Location: success.php");
If it fails, redirect to error.php
header("Location: error.php");
If you want to go to the page the form was on, but show an error or success message, do this:
header("Location: original.php?status=error")
Or change error to success if appropriate, you can then use $_GET['status'] to determine whether or not the form failed / succeeded.
Rather than send the client a redirect, resulting in another call to your web server, I believe a better way of doing this would be to use a PHP include.
if (mail($recipient, $subject, $formcontent, $mailheader))
include 'success.php';
else
include 'fail.php';
if (failure) {
header("Location: success.php");
exit;
} else if (success) {
echo "thank you";
}

php header location attribute not working

I have a header location problem despite following all the advice I can find online. The mailer script sends the email(s) but I get a 'headers already sent' error relating to Line 29 which is {header("Location: $thanksURL");} and no redirect to the confirmation page. If I replace the header location code with an instruction to print a confirmatory message instead, it works, so there must be something about the header location code that the server doesn't like.
Here is the code:
<?php
ob_start();
$to = "msheath#btinternet.com" ;
$from = $_REQUEST['email'] ;
$name = $_REQUEST['name'] ;
$headers = "From: $from";
$subject = "Request for Library Document";
$thanksURL = "http://www.postalhistory.org.uk/newsite/php/thankyou.php"; //confirmation page
$fields = array();
$fields{"name"} = "Name";
$fields{"address"} = "Address";
$fields{"email"} = "Email";
$fields{"tel"} = "Telephone No";
$fields{"author1"} = "First Author";
$fields{"title1"} = "First Title";
$fields{"author2"} = "Second Author";
$fields{"title2"} = "Second Title";
$body = "I would like to borrow the undermentioned book(s) from the PHS Library:\n\n"; foreach($fields as $a => $b){ $body .= sprintf("%20s: %s\n",$b,$_REQUEST[$a]); }
$headers2 = "From: The Librarian, Postal History Society";
$subject2 = "Thank you for contacting the Postal History Society";
$autoreply = "Thank you for your request. Somebody will get back to you as soon as possible, usually within 48 hours.";
if($from == '') {print "You have not entered an email, please go back and try again";}
else {
if($name == '') {print "You have not entered a name, please go back and try again";}
else {
$send = mail($to, $subject, $body, $headers);
$send2 = mail($from, $subject2, $autoreply, $headers2);
if($send)
{header("Location: $thanksURL");}
else
{print "We encountered an error sending your mail, please notify webmaster#YourCompany.com"; }
}
}
ob_end_flush()
?>
Go to http://www.postalhistory.org.uk/newsite/php/library.php to try it out for yourself.
Can anyone suggest what is wrong?
Mike
'headers already sent' means you've already sent something to the browser. This could be a whitespace somewhere. It could also be that your file is encoded in UTF-8 with BOM which means you've sent the BOM to the browser
Warning: Cannot modify header information - headers already sent by (output started at /home/users/uks52804/html/postalhistory.org.uk/newsite/php/contact.php:1) in /home/users/uks52804/html/postalhistory.org.uk/newsite/php/contact.php on line 29
This means that you have output prior to your ob_start() call. ob_start should be the first instruction of the page that is including that code.
I faced the same problem a while back and found that i was echoing something before setting the headers. I removed the echo statement and also cleared some whitespaces that removed the problem

Categories