After form submit Make a alert with Success message PHP - php

I have a fully working form that sends emails on submit, I would like to have some kind of alert if the email is sent successfully. I saw a lot of solutions with jquery or other libraries but I wanna do it with php
<?php
if(isset($_POST['name']) && $_POST['email'] !='') {
$userName = $_POST['name'];
$lastName = $_POST['surname'];
$email = $_POST['email'];
$phoneNumber = $_POST['phonenumber'];
$messageSubject =$_POST['subject'];
$catalog =$_POST['select_catalog'];
$inputsValue =$_POST['radioSet1'];
$inputsValue2 =$_POST['radioSet2'];
$inputsValue3 =$_POST['radioSet3'];
$inputsValue4 =$_POST['radioSet4'];
$to = "levchegochev#gmail.com";
$body = "";
$body = "Akademija Za dizajn"."\r\n";
$body .= "Ime: ".$userName. "\r\n";
$body .= "Prezime: ".$lastName. "\r\n";
$body .= "Email: ".$email. "\r\n";
$body .= "Telefonski broj:".$phoneNumber."\r\n";
$body .= "Nacin na plakanje: ".$catalog. "\r\n";
$body .= "Opcii: ".$inputsValue. "\r\n".$inputsValue2. "\r\n" .$inputsValue3. "\r\n" .$inputsValue4."\r\n";
mail($to,$messageSubject, $body);
if(mail = true ) {
echo 'test'
}
}
?>

You can write:
if(mail == true ) {
echo '<script>alert("Email send!");</script>';
}

you can try this
if(mail) echo '<script>alert("message")</script>';

Related

Contact form submission that send email and records data in csv

I have a contact form that on submit displays a thank you message, takes the users entries and emails them to an address but should also take four fields and place it into a CSV. Unfortunately I can't get the last part to work no matter what I try.
The thank you message works, an email is sent, but the part I've added at $output (before the if/else) doesn't.
Here's the code I'm using, thanks in advance to anyone who can help with this.
<?php
$subject = 'Submission received';
$mailto = '';
$firstName = $_POST['firstName'];
$lastName = $_POST['lastName'];
$email = $_POST['email'];
$telephone = $_POST['telephone'];
$company = $_POST['companyName'];
$country = $_POST['country'];
$about = $_POST['hearAbout'];
$enquiry = $_POST['enquiry'];
$body = "
<br>
<p>The following information was submitted through the contact form on your website:</p>
<p><b>Name</b>: $firstName $lastName<br>
<b>Email</b>: $email<br>
<b>Phone number</b>: $telephone</br>
<b>Company name</b>: $company<br>
<b>Country</b>: $country<br>
<b>Heard about company via</b>: $about<br>
<b>Enquiry</b>: $enquiry<br></p>
";
// Success Message - PAD THIS OUT
$success = "
<div class=\"\">
<div class=\"\">
<h3>Submission successful</h3>
<p>Thank you.</p>
</div>
</div>
";
$headers = "From: $firstName $lastName <$email> \r\n";
$headers .= "Reply-To: $email \r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$message = "<html><body>$body</body></html>";
$output = $firstName . "t";
$output .= $lastName . "t";
$output .= $email . "t";
$output .= $telephone . "n";
$fp = fopen("data/enquiry.csv", "a");
fwrite($fp, $output);
fclose($fp);
if (mail($mailto, $subject, $message, $headers)) {
echo "$success"; // success
} else {
echo 'Form submission failed. Please try again...'; // failure
}
?>
Maybe this will help:
$fp = fopen("data/enquiry.csv", "a");
fwrite($fp,"," . $firstName . "," . $lastName . "," . $email . "," . $telephone . "\n");
fclose($fp);

My server's name shows up as submitter's Reply-to email address from PHP contact Form

I'm not versed in PHP and had this form built for me. It works 95% of the time, but sometimes the reply-to email will be johnsmith#hadrian.lunarpages.com
I'm reading up a similar issue on this page, but not exactly sure what to change in my form...
<?php
if (isset($_REQUEST['btnSubmit-group'])) {
$date = $_REQUEST['date'];
$time = $_REQUEST['time'];
$count = $_REQUEST['count'];
$name = $_REQUEST['name'];
$organization = $_REQUEST['organization'];
$email = $_REQUEST['email'];
$grouptype = $_REQUEST['grouptype'];
$phone = $_REQUEST['phone'];
$upgrademus = $_REQUEST['upgrademus'];
$upgradeowo = $_REQUEST['upgradeowo'];
$comments = $_REQUEST['comments'];
$ip = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '';
if ($count == "" || $name == "" || $email == "" || $phone == "") {
echo "All fields are required, please fill out again. ";
} else {
$to = 'info#example.com';
$subject = "Group Tour Inquiry from $name ($date, $count people)";
$from = $to;
$ip = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '';
$body = nl2br("Private Tour:<br>" .
"<b>Name:</b> $name\r\n" .
"<b>Email:</b> $email \r\n" .
"<b>Phone:</b> $phone \r\n" .
"<b>Organization:</b> $organization\r\n" .
"<b>Date:</b> $date\r\n" .
"<b>Time:</b> $Time\r\n" .
"<b>Count:</b> $count\r\n" .
"<b>Include:</b> $upgrademus\r\n" .
"<b>Include:</b> $upgradeowo\r\n" .
"<b>Comments:</b> $comments\r\n" .
"");
// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= "From: $name<$email> \r\n";
$headers .= "Reply-To: $email \r\n";
if (mail($to, $subject, $body, $headers)) {
echo "<div class=\"thankyou\"></div><meta http-equiv='refresh' content='0;url=http://www.example.com/private-group-tours'>";
}
}
} else {} ?>
You're trying to send e-mail's to your self FROM the person whose entering this in, essentially attempting to spoof their email, and sometimes this will(should) get blocked. more can be read here - problem with php mail 'From' header
Change:
$headers .= "From: $name<$email> \r\n";
$headers .= "Reply-To: $email \r\n";
to
$from = 'Admin#mysite.com';
$headers .= "From: $from\r\n";
$headers .= "Reply-To: $from \r\n";
and then edit that static $from variable. Then take the users email out of the BODY of the email you receive, and send them an email.

Contact form php script sending spam to Gmail

I created a php script (for a contact form) to send emails to my Gmail account.
If I use the sender email in the header ($headers = "From: " . $email;), Gmail reports the received message as spam.
If I don't use the email in the header (e.g. the sender name $headers = "From: " . $name;) the message is not reported as spam.
Do you have any suggestion to let me use the email in the header?
Thanks!
<?php
/* Check if the url field is empty (antispam) */
if ($_POST['leaveblank'] != '' or $_POST['dontchange'] != 'http://') {
$name = $_POST['name'];
$faillink = "xxx.php";
header("Location: $faillink");
} else {
$name = $_POST['name'];
$email = $_POST['email'];
$subject_prefix = "[ContactForm]: ";
$subject = $subject_prefix . $_POST['subject'];
$message = $_POST['message'];
$to = "myemail#gmail.com";
$body = "From: " . $name . "\n";
$body .= "Email: " . $email . "\n";
$body .= "Message: " . $message . "\n";
$headers = "From: " . $email;
$oklink = "yyy.php";
$faillink = "xxx.php";
if ( preg_match( "/[\r\n]/", $name ) || preg_match( "/[\r\n]/", $email ) ) {
header("Location: $faillink");
}
$retmail = mail($to, $subject, $body, $headers);
if ($retmail) {
header("Location: $oklink");
} else {
header("Location: $faillink");
}
}
?>
I solved the issue as Iain suggested so I replaced the mail headers as follows:
$headers = "From: " . "noreplay#mydomain.com" . "\r\n";
$headres .= "Reply-To: " . $email . "\r\n";

PHP - need help in php form element (Header part)

I am a beginner in php.
I am trying php to send mail.
My mail goes to mail client but it does not show the header part well as I wanted.
I am using the following codes ----
<?php
//validation expected data exists
if(!isset($_POST["name"]) ||
!isset($_POST["city"]) ||
!isset($_POST["email"]) ||
!isset($_POST["phone"]) ||
!isset($_POST["message"])) {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
//Subject
$name = $_POST["name"];
$subject = "NO REPLY";
$email = $_POST["city"];
$phone = $_POST["email"];
$website = $_POST["phone"];
$message = $_POST["message"];
$header = "from: $name <$email>";
$to = 'info#mishmihillsadventure.in';
$send_contact=mail($to,$subject,$message,$header);
//Check, if message sent to your email
// Display message "We've recived your information"
if($send_contact){
echo "We've received your contact information";
}
else{
echo "ERROR";
}
?>
$email = $_POST["city"];
$phone = $_POST["email"];
Is this really what you want? Shouldn't it be:
$email = $_POST["email"];
And try the following headers:
$header = 'From: ' . $name . '<' . $email . '>' . "\r\n";
Use (atmost) following headers while sending mail via PHP -
$header = "MIME-Version: 1.0" . "\r\n";
$header .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$header .= "From: $name <$email>" . "\r\n";
//If want to `CC` someone add
$header .= 'Cc: abc#email.com' . "\r\n";
Using variables in Double quotes is fine.
You can try something like in code mentioned below,
<?php
$to = 'test#to.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: test#from.com' . "\r\n" .
'Reply-To: test#from.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
if(mail($to, $subject, $message, $headers)) {
echo 'Msg send..';
} else {
echo 'Msg Not send..';
}
?>

JQuery Ajax PHP form submit

I'm using Ajax contact form where my contactform.php is something like this :
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$subject = "Contact Form";
$message .= $_POST['name'] . "\n\r";
$message .= $_POST['email'] . "\n\r";
$message .= $_POST['message'];
mail("your#domain.com", $subject, $message, "From: ".$email);
?>
This form script works perfect But My problem is that, My value for "your#domain.com" is dynamic.. So I modified contactform.php to this :
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$emailto = $qc2_options['qc2_form_heading'];
$subject = "Contact Form";
$message .= $_POST['name'] . "\n\r";
$message .= $_POST['email'] . "\n\r";
$message .= $_POST['message'];
mail($emailto, $subject, $message, "From: ".$email);
?>
But this modified script isn't working.. I do get correct value for $qc2_options['qc2_form_heading']; on echo But this form dosn't work.
Where I'm going wrong ?

Categories