I have two scenarios for my form that will determine if the submission is successful or failed. Either outcome serves the user with a unique page. I am looking for a way that the successful submissions are emailed to group A email addresses and the failed submissions are sent to Group B email addresses.
Here is my code:
<?php session_start();
if(isset($_POST['Submit'])) {
$from=$_POST['empty'];
$to=$_POST['empty'];
$subject=$_POST['empty'];
$text=$_POST['empty'];
$html=$_POST['empty'];
$file=$_POST['empty'];
$mimetype=$_POST['empty'];
$host=$_POST['empty'];
$username=$_POST['empty'];
$password=$_POST['empty'];
$Q1=$_POST['Q1'];
$Q2=$_POST['Q2'];
$Q3=$_POST['Q3'];
$Q4=$_POST['Q4'];
$fromsubject = 'sample-subject';
$firstname = $_POST['firstname'];
$mail = $_POST['mail'];
$phonenumber = $_POST['phonenumber'];
$to = 'myemail#gmail.com';
$header = "From: <" . $mail . ">\r\n"; //optional headerfields
$mailsubject = 'Message recived from sample site';
$body = $fromsubject.'
The person that contacted you is '.$firstname.'
Phone number: '.$phonenumber.'
E-mail: '.$mail.'
Q1: '.$Q1.'
Q2: '.$Q2.'
Q3: '.$Q3.'
Q4: '.$Q4.'
|---------END MESSAGE----------|';
mail($to, $mailsubject, $body, $header);
if ($Q3 == "yes") {
header('Location: http://example.com/success.php');
} else {
header('Location: http://example.com/resources.php');
}
?>
I apologize for the confusion. My form has 4 questions in it. If they answer no to one of them, they do not qualify for our offer. If they answer yes to all, they qualify. The all yes users need to be sent to our call center email address while the other respondents who did not answer all yes go to our recovery department. –
Related
Ahoy!
Any help on this would be really appreciated! I have a contact form which is sending emails on a page refresh. I have tried a number of things and am not getting anywhere.. Heres what Ive got so far:
if(isset($_POST['email']) && $_POST['email'] != '') {
if( filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) ){
$userName = $_POST['name'];
$userEmail = $_POST['email'];
$messageSubject = $_POST['subject'];
$message = $_POST['message'];
$to = "email#gmail.com";
$body = "";
$body .= "From: " .$userName. "\r\n";
$body .= "Email: " .$userEmail. "\r\n";
$body .= "Message: " .$message. "\r\n";
header('Location: http://www.website.net/contact-thank-you.html');
exit();
mail($to, $messageSubject, $body);
}
}
?>
The easiest way to prevent this is to redirect to a new URL after the email has been sent. Often websites will redirect to a "thank you" page.
You might be able to get away with redirecting to the same URL (and this won't send a second email because $_POST will be empty).
Another way would be to use the user session ($_SESSION) to keep track of whether the email was sent, and don't send an email again if you detect that $_SESSION indicates it was already sent. You could also use cookies for this same purpose. Last but not least you could store that information in a database and check the DB before sending an email so you don't send multiple emails.
I'm fairly new to this but I've done all the digging I can do. When I submit this form rather than displaying the sender's email (which is what I want) it displays the webserver I'm using in the From part. I'm not sure what I need to put to make display anyone's email.
<?php
$email = $_POST['email'];
$recipient = "my#email.com";
$subject = "Email Notice";
$headers = 'From: '.$email."\r\n".
if(mail($recipient, $subject, $headers)){
header("Location: thankyou.html");
}else{
header("Location: sorry.html");
}
exit;
?>
I have a form on my site which users are filling with name and email, and then it generates an email to the webmaster notifying of user submitting their info. There's also a cookie placed which is for referral tracking, how can I also include that data in the email out to the webmaster?
<?php
///subscribe form
$email = $_POST['email'] ;
$firstname = $_POST['firstname'] ;
$recipient = "myemail#email.com"; /// Webmaster address
if (isset($_POST['email']))
{
//Send Mail To Webmaster
$email = $_POST['email'] ;
$subject = ' Enviralizer: Someone Just Requested More Info';
$message = 'Your new lead info is below: ';
$message .= "\r\n\nName: " .$firstname;
$message .= "\r\n\nEmail: " .$email;
mail("$recipient", $subject,
$message, "From:" . $recipient);
header("Location: http://thanks.com");
}
?>
I know the script is not the best secured thing in the world, I will take care of that later, I just need a quick fix for a bigger script tracking issue I'm trying to resolve as well. Thanks.
I guess you're looking for $_COOKIE['name_of_cookie'] ?
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>';
}
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);