How to validate an email address with php? [duplicate] - php

This question already has answers here:
How to validate an Email in PHP?
(7 answers)
How can I validate an email address using a regular expression?
(79 answers)
How to validate an email address in PHP
(15 answers)
Closed 8 years ago.
I started trying to write a php script to validate the email address that the user enters in my form.
Can anybody please help me to finish it? Thanks in advance. Your help will be greatly appreciated :)
NOTE: Please do not tell me to use javascript or jQuery. I need to do this with php :/
<?php
$mail = $_POST['mail'];
$formcontent = "Email: $mail";
$recipient = "email#example.com";
$subject = "Mail that uploaded picture";
$mailheader = "From: my website";
if ($mail == ""){
echo "Please enter a valid email address. We want to contact you using your email address. Do not worry, nobody will be able to see it.";
}
else{
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo 'Your email address has been successfully added to your photo !<br>We will contact you later to tell you how to win the $50 000 :)<br><br>';}
?>

Like this:
$email = 'email#example.com';
if (filter_var($email, FILTER_VALIDATE_EMAIL)){
echo 'Email OK';
}
In your source code:
$mail = $_POST['mail'];
$formcontent = "Email: $mail";
$recipient = "email#example.com";
$subject = "Mail that uploaded picture";
$mailheader = "From: my website";
if (!filter_var($mail, FILTER_VALIDATE_EMAIL)){
echo "Please enter a valid email address. We want to contact you using your email address. Do not worry, nobody will be able to see it.";
} else {
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo 'Your email address has been successfully added to your photo !<br>We will contact you later to tell you how to win the $50 000 :)<br><br>';
}

Ideally you should also be checking to see if $_POST['mail'] is defined because you will get an error / notice depending on your error_reporting level and display_errors.
Updated code:
if (!isset($_POST['mail']) || !filter_var($_POST['mail'], FILTER_VALIDATE_EMAIL)) {
echo "Please enter a valid email address. We want to contact you using your email address. Do not worry, nobody will be able to see it.";
} else {
$mail = $_POST['mail'];
$formcontent = "Email: $mail";
$recipient = "email#example.com";
$subject = "Mail that uploaded picture";
$mailheader = "From: my website";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo 'Your email address has been successfully added to your photo !<br>We will contact you later to tell you how to win the $50 000 :)<br><br>';
}

Related

How to send mail from bootstrap form on completition and submission to a mail address?

I have created a bootstrap form .. lets say it is for a survey and it generally has a lot of yes no questions . with radio buttons and some text fields for the name,time, date and remarks.. here i want to send the completely filled form to the mail address of admin .. what should be done i am using bootstrap for this..
I tried this but it is not much applicable to me .. it sends the mail but all the forms are not sent by it.. please help..
<?php $name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$formcontent="From: $name \n Message: $message";
$recipient = "emailaddress#here.com";
$subject = "Contact Form";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "Thank You!";
?>
Use ini_set to change smtp, port, Authorization details, sendmailfrom etc in this script and then use mail function. Hope it will help.
Ex:-
ini_set("SMTP", "smtp.google.com");

How do I send an email to an email address with an custom message [duplicate]

This question already has answers here:
How can I send an email using PHP?
(20 answers)
Closed 5 years ago.
I wanted to send an email such as an confirmation email to an email address that the user has alredy specified in an input tag. Exampe:
I type in my email in an input tag and then I would recive an email at the address I entered in the input tag. Thanks, and If you know the answer could you possibly send me an code to paste in. (Im an php noob)
<?php
$recipient = $_POST['email']; //recipient
$email = "your#domain.com"; //senders e-mail adress
if((filter_var($email, FILTER_VALIDATE_EMAIL)) && ($robotest == "")) {
$Name = ($_POST['name']); //senders name
$mail_body = !!!----> WHAT DO I PUT HERE <----!!!!
$subject = "Porter Inquiry"; //subject
$header = "From: ". $Name . " <" . $email . ">\r\n"; //optional headerfields
mail($recipient, $subject, $mail_body, $header); //mail command :)
} else {
print "You've entered an invalid email address!";
}
?>

I need to send user a copy of the placed order in the form

Iam using the following php code in my order form
I need to send user a copy of his/her placed order other than the owner mail. I don't know how to write code in $recepient for sending a copy to user also. I need to use "$Email" for the user email id.
Please give me suggestion for writing code.
My php code for order form is as follows:
<?php
$Email = #trim(stripslashes($_POST['Email']));
$Name = $_POST['Name'];
$Email = $_POST['Email'];
$Business = $_POST['Business'];
$phone= $_POST['phone'];
$productid= $_POST['productid'];
$producttype= $_POST['producttype'];
$Productquantity= $_POST['Productquantity'];
$Shippingaddress= $_POST['Shippingaddress'];
$Message = $_POST['Message'];
$formcontent=
"$Name is sending you a request for the product enquiry. \n
Details of the Product Enquiry person are as follows- \n
Name: $Name \n
Contact Number: $phone \n
Email: $Email \n
Business Name/Company Name: $Business \n
Product Id: $productid \n
Product Type: $producttype \n
Product Quantity: $Productquantity \n
Comment: $Message \n
Shipping Address: $Shippingaddress";
$recipient = "info#domain.com";
$subject = "Request for Product Enquiry";
$mailheader = "From: $Name \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "<h4><font color='#2A007D'><strong><center>Thank You $Name !
We have received your Order details and
we will contact you as soon as possible.
<br>Your Order details has been sent to your Email also.
</center></strong></font></h4>";
?>
You could call mail() again with the user's email address, so:
mail($Email, $subject, $formcontent, $mailheader);
although you may want to change the message text too.
$mail = mail($recipient, $subject, $formcontent, $mailheader);
if(!$mail)
{
echo <h4><font color='red'>Failed</font></h4>;
}
else
{
echo "<h4><font color='#2A007D'><strong><center>Thank You $Name ! We have received your Order details and we will contact you as soon as possible. <br>Your Order details has been sent to your Email also.</center></strong></font></h4>";
}
?>

Contact form sends everything to spam

My contact form works fine but sends everything to spam when I set the recipient as Gmail account and sends nothing if I set it as my domain email client (eg info#mydomain.com). Is there something wrong in the code? What do I need to do?
<?php $name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$formcontent="From: $name \n Message: $message";
$recipient = "info#mydomain.com";
$subject = "Contact Form";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
header('Location: message-sent.html#contact'); exit();
?>
Second question. I set up the location to redirect a user to a thank-you page. How can I set it to open in a new tab instead?

PHP reply-to error - need person's email, not admin

I have the below PHP contact form that has a CAPTCHA code to ensure is correct. However, when I reply to the email from the website it puts a random email which i believe is the server admin, however, I want it to be the persons email who sent the form in. below is the code, could you possibly be able to help me?
<?php session_start();
if(isset($_POST['Submit'])) { if( $_SESSION['chapcha_code'] == $_POST['chapcha_code'] && !empty ($_SESSION['chapcha_code'] ) ) {
$youremail = 'info#example.com';
$fromsubject = 'www.example.co.uk';
$title = $_POST['title'];
$fname = $_POST['fname'];
$mail = $_POST['mail'];
$phone = $_POST['phone'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$to = $youremail;
$mailsubject = 'Message from Website'.$fromsubject.' Contact Page';
$body = $fromsubject.'
The person that contacted you is: '.$fname.'
Phone Number: '.$phone.'
E-mail: '.$mail.'
Subject: '.$subject.'
Message:
'.$message.'
|---------END MESSAGE----------|';
echo "Thank you for your message. I will contact you shortly if needed.<br/>Go to <a href='/index.html'>Home Page</a>";
mail($to, $subject, $body);
unset($_SESSION['chapcha_code']);
} else {
echo 'Sorry, you have provided an invalid security code';
}
} else {
echo "You must write a message. </br> Please go to <a href='/contact.html'>Contact Page</a>";
}
?>
You'll need some headers so the from address is the users mail.
Also refer to the mail docs
try this
$headers = "From: $mail\r\n";
$headers .= "Reply-To: $mail\r\n";
mail($to, $subject,$body,$headers);

Categories