Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
i just wanted to ask if there is any way possible for the details inputted in a form field to be forwarded to a cell phone number as a text message.
Here is the code i have for form to email, so if there are any changes i have to make to this code, you can let me know.
<?php
if(isset($_POST['email'])) {
$email_to = "you#yourdomain.com";
$email_subject = "Your email subject line";
function died($error) {
// your error code can go here
echo "We are very sorry, but there were error(s) found with the form you submitted. ";
echo "These errors appear below.<br /><br />";
echo $error."<br /><br />";
echo "Please go back and fix these errors.<br /><br />";
die();
}
// validation expected data exists
if(!isset($_POST['first_name']) ||
!isset($_POST['last_name']) ||
!isset($_POST['email']) ||
!isset($_POST['telephone']) ||
!isset($_POST['comments'])) {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
$first_name = $_POST['first_name']; // required
$last_name = $_POST['last_name']; // required
$email_from = $_POST['email']; // required
$telephone = $_POST['telephone']; // not required
$comments = $_POST['comments']; // required
$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+#[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp,$email_from)) {
$error_message .= 'The Email Address you entered does not appear to be valid.<br />';
}
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$first_name)) {
$error_message .= 'The First Name you entered does not appear to be valid.<br />';
}
if(!preg_match($string_exp,$last_name)) {
$error_message .= 'The Last Name you entered does not appear to be valid.<br />';
}
if(strlen($comments) < 2) {
$error_message .= 'The Comments you entered do not appear to be valid.<br />';
}
if(strlen($error_message) > 0) {
died($error_message);
}
$email_message = "Form details below.\n\n";
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message .= "First Name: ".clean_string($first_name)."\n";
$email_message .= "Last Name: ".clean_string($last_name)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Telephone: ".clean_string($telephone)."\n";
$email_message .= "Comments: ".clean_string($comments)."\n";
// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
#mail($email_to, $email_subject, $email_message, $headers);
?>
<!-- include your own success html here -->
You will need to use an external provider to be able to send text messages to cellphones.
A few examples are Twilio and plivo
You will need to connect to their API. However, it may depend on your country what service you are able to use.
There are several API providers that let you send SMS to cellphones over the internet.
For example
https://www.twilio.com/sms
They have a simple API that you can use with your preferred programming language to send SMS over the web to cellphones.
Just use the values submitted from your form and feed it to the API objects and submit them to Twillio.
Related
I have a form that uses an HTML form, http post request, and PHP backend to automatically send me the data that a user inputs into the form. The submission works as expected, but I am not getting an email. My email is also run by outlook. Not sure if it's an encryption thing.
HTML Code
<form action="mail.php" method="POST">
<div class="email-box">
<input class="tbox" id="email_box" name="email" type="email" style="cursor: pointer;" placeholder="Enter your email">
<button class="btn" id="email_btn" type="submit" name="button">Subscribe</button>
</div>
</form>
<!-- Snipped JavaScript validation -->
PHP
<?php
$errors = '';
$myemail = 'me#website.com';
if (empty($_POST['email'])) {
$errors .= "\n Error: all fields are required";
}
$email_address = $_POST['email'];
if (!preg_match(
"/^[_a-z0-9-]+(\.[_a-z0-9-]+)*#[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i",
$email_address)) {
$errors .= "\n Error: Invalid email address";
}
if (empty($errors)) {
$to = $myemail;
$email_subject = "New Vrify subscription: $email_address";
$email_body = "You have a new subscriber. " .
"Here are the details:\n Email: $email_address \n " .
$headers = "From: subscriber#website.com";
mail($to, $email_subject, $email_body, $headers);
//redirect to the 'thank you' page
header('Location: thank-you.html');
}
?>
This may not be the full solution, but as far as I'm aware
"New Vrify subscription: $email_address"
is not valid. Instead, concatenate them using the . operator
"New Vrify subscription:".$email_address
Do the same to the other variables you have, I had the same issue when working on the following php:
if (($_SERVER["REQUEST_METHOD"] ?? 'GET') == "POST") {
$msg ="New message from mysite!\n".$_POST["message"]."\nReply Email:".$_POST["email"];
mail("me#gmail.com", time(), $msg,"From: contact#mysite");
}
I am doing a registration form. When i enter an x number and click submit, 2 fields open up x number of times that I have to fill. After that I am using php to get the post values but I do not know how to use all the array values. I have to send the all the values of each students class number and name in the message content via an email. In my email I am only getting the last input of the fields not all of them. Not sure how to get all of the array values from the javascript and pass them into my php file.
a section of html file's javascript
var i;
for (i = 0; i < ax; i++) {
parent.insertAdjacentHTML(
"beforeend",
"<div class=input>Enter student" +[i]+ "'s name: <input type='text ' name='s_name[]' required></div>" +
"<div class=input>Enter student" +[i]+ "'s class: <input type='text ' name= 's_class[]' required></div>"
);
}
php code
$s_name=$_POST["s_name"];
$s_class=$_POST["s_class"];
$from = "zasas.com";
$to = $_POST["email"];
$subject = "asasas";
$message = "Name: ".$username."\r\n";
$message .= "student name: $s_name[i] \r\n";
$message .= "class: $s_class[i] \r\n";
$message .= "Your super id: aiorouehr \r\n";
$headers = "From: " . $from;
The keys in $_POST are the names of your input.
E.g.
<input type="text" name="myCustomKey" value="Micky Mouse">
<?php echo $_POST["myCustomKey"]; //returns Micky Mouse ?>
Errors in console
I am using a Mailchimp Ajax Submit jQuery Plugin to submit an email using a PHP file on a contact form in a website template I imported to my Angular app. When I go to enter my details on the form and submit, the page refreshes on the POST request.
The console then throws the following two errors.
[Deprecation] Synchronous XMLHttpRequest
[Deprecation] Synchronous XMLHttpRequest
on the main thread is deprecated because of its detrimental effects to the
end user's experience. For more help, check https://xhr.spec.whatwg.org/.
(anonymous) # polyfills.c5fb4ffca9c225046d5a.js:1
polyfills.c5fb4ffca9c225046d5a.js:1
Uncaught Error: Zone already loaded.
Uncaught Error: Zone already loaded.
at polyfills.c5fb4ffca9c225046d5a.js:1
at polyfills.c5fb4ffca9c225046d5a.js:1
at Object.0TWp (polyfills.c5fb4ffca9c225046d5a.js:1)
at p (<anonymous>:1:507)
at Object.hN/g (polyfills.c5fb4ffca9c225046d5a.js:1)
at p (<anonymous>:1:507)
at Object.5 (polyfills.c5fb4ffca9c225046d5a.js:1)
at p (<anonymous>:1:507)
at n (<anonymous>:1:376)
at e (<anonymous>:1:248)
I am finding it difficult to understand the cause of the problem. I have tried to prevent the page from refreshing on submit using <button type="button">, this does not do a page refresh however it does not call a POST request as expected.
I have also tried un-commenting import 'zone.js/dist/zone'; in polyfills.ts
I wish to accomplish sending the email on the same page without a refresh in the Angular app unless there is a better alternative. (I am learning)
main.js
var clContactForm = function() {
/* local validation */
$('#contactForm').validate({
/* submit via ajax */
submitHandler: function(form) {
var sLoader = $('.submit-loader');
$.ajax({
type: "POST",
url: "inc/sendEmail.php",
data: $(form).serialize(),
beforeSend: function() {
sLoader.slideDown("slow");
},
success: function(msg) {
// Message was sent
if (msg == 'OK') {
sLoader.slideUp("slow");
$('.message-warning').fadeOut();
$('#contactForm').fadeOut();
$('.message-success').fadeIn();
}
// There was an error
else {
sLoader.slideUp("slow");
$('.message-warning').html(msg);
$('.message-warning').slideDown("slow");
}
},
error: function() {
sLoader.slideUp("slow");
$('.message-warning').html("Something went wrong. Please try again.");
$('.message-warning').slideDown("slow");
}
});
}
});
};
sendEmail.php
<?php
// Replace this with your own email address
$siteOwnersEmail = 'my-email';
if($_POST) {
$name = trim(stripslashes($_POST['contactName']));
$email = trim(stripslashes($_POST['contactEmail']));
$subject = trim(stripslashes($_POST['contactSubject']));
$contact_message = trim(stripslashes($_POST['contactMessage']));
// Check Name
if (strlen($name) < 2) {
$error['name'] = "Please enter your name.";
}
// Check Email
if (!preg_match('/^[a-z0-9&\'\.\-_\+]+#[a-z0-9\-]+\.([a-z0-9\-]+\.)*+[a-z]{2}/is', $email)) {
$error['email'] = "Please enter a valid email address.";
}
// Check Message
if (strlen($contact_message) < 15) {
$error['message'] = "Please enter your message. It should have at least 15 characters.";
}
// Subject
if ($subject == '') { $subject = "Contact Form Submission"; }
// Set Message
$message .= "Email from: " . $name . "<br />";
$message .= "Email address: " . $email . "<br />";
$message .= "Message: <br />";
$message .= $contact_message;
$message .= "<br /> ----- <br /> This email was sent from your site's contact form. <br />";
// Set From: header
$from = $name . " <" . $email . ">";
// Email Headers
$headers = "From: " . $from . "\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";
if (!$error) {
ini_set("sendmail_from", $siteOwnersEmail); // for windows server
$mail = mail($siteOwnersEmail, $subject, $message, $headers);
if ($mail) { echo "OK"; }
else { echo "Something went wrong. Please try again."; }
} # end if - no validation error
else {
$response = (isset($error['name'])) ? $error['name'] . "<br /> \n" : null;
$response .= (isset($error['email'])) ? $error['email'] . "<br /> \n" : null;
$response .= (isset($error['message'])) ? $error['message'] . "<br />" :
null;
echo $response;
} # end if - there was a validation error
}
?>
On a Linux server, I have an ajax form that gets posted to a js script, which in turn posts to a php routine for sending a web site email contact form (Styleshout-Puremedia template). I know the post is occurring to js, and to the php page. I get an httpd server error on the php page that "$Error is undefined". However if I define it as NULL or '' it exists and therefore doesn't pass the test "if (!$Error)". If I create a hard-coded php script using the mail function, the e-mail works so php and sendmail are all configured properly. Can someone help as to why this php script doesn't work, or how to fix the "$Error" error?
PHP code:
<?php
// Replace this with your own email address
$siteOwnersEmail = 'info#mydomain.com';
if($_POST) {
$fname = trim(stripslashes($_POST['contactFname']));
$lname = trim(stripslashes($_POST['contactLname']));
$email = trim(stripslashes($_POST['contactEmail']));
$subject = trim(stripslashes($_POST['contactSubject']));
$contact_message = trim(stripslashes($_POST['contactMessage']));
// Check First Name
if (strlen($fname) < 2) {
$error['fname'] = "Please enter your first name.";
}
// Check Last Name
if (strlen($lname) < 2) {
$error['lname'] = "Please enter your last name.";
}
// Check Email
if (!preg_match('/^[a-z0-9&\'\.\-_\+]+#[a-z0-9\-]+\.([a-z0-9\-]+\.)*+[a-z]{2}/is', $email)) {
$error['email'] = "Please enter a valid email address.";
}
// Check Message
if (strlen($contact_message) < 15) {
$error['message'] = "Please enter your message. It should have at least 15 characters.";
}
// Subject
if ($subject == '') { $subject = "Contact Form Submission"; }
// Set Name
$name = $fname . " " . $lname;
// Set Message
$message = "Email from: " . $name . "<br />";
$message .= "Email address: " . $email . "<br />";
$message .= "Message: <br />";
$message .= $contact_message;
$message .= "<br /> ----- <br /> This email was sent from your site's contact form. <br />";
// Set From: header
$from = $name . " <" . $email . ">";
// Email Headers
$headers = "From: " . $from . "\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";
if (!$error) {
ini_set("sendmail_from", $siteOwnersEmail); // for windows server
$mail = mail($siteOwnersEmail, $subject, $message, $headers);
if ($mail) { echo "OK"; }
else { echo "Something went wrong. Please try again."; }
} # end if - no validation error
else {
$response = (isset($error['fname'])) ? $error['fname'] . "<br /> \n" : null;
$response .= (isset($error['lname'])) ? $error['lname'] . "<br /> \n" : null;
$response .= (isset($error['email'])) ? $error['email'] . "<br /> \n" : null;
$response .= (isset($error['message'])) ? $error['message'] . "<br />" : null;
echo $response;
} # end if - there was a validation error
}
?>
Thanks for your help!!
-Rob
You can always just initialize $error as
$error = [];
and then check for errors by
if(count($errors) === 0){
//no errors
}
Or, just use if(!isset($error)){//no errors}
So right now I have a basic contact.html with a php script set to email me if someone on my website contacts me. When they click submit, it takes them to contact_sent.php, which is a blank page. I want it so that when they click the submit, it keeps them on the same page, as well as display a success message from SweetAlert, and then clear the form so they can't spam the "Send Message" button.
<?php
if ($_POST['submit']) {
$name=$_POST['name'];
$email=$_POST['email'];
$subject=$_POST['subject'];
$message=$_POST['msg'];
$to_add="***********#gmail.com";
$from_add="form#***********.net";
$email_message="<br /><br /><b>Name:</b><br />$name <br />";
$email_message.="<br /><br /><b>Reply Email:</b><br />$email <br />";
$email_message.="<br /><br /><b>Regarding:</b><br />$subject <br />";
$email_message.="<br /><br /><b>Message:</b><br />$message <br />";
$headers="From: $from_add \r\n";
$headers.="Reply-To: $from_add \r\n";
$headers.="Return-Path: $from_add\r\n";
$headers.="X-Mailer: PHP \r\n";
$headers.="MIME-Version: 1.0\r\n";
$headers.="Content-Type: text/html;\r\n";
mail($to_add,$subject,$email_message,$headers);
}
alert("Sent!");
?>
Edit: The alert at the end doesn't work.
alert is javascript code.So you need your php script to produce that javascipt. Hope it will help you
<?php
if($_POST['submit']) {
//your other codes here
mail($to_add,$subject,$email_message,$headers);
//becareful keep the whole javascript code on mail function's success.
//I just added how to do It.It will alert too if your mail function return false.
?>
<script type="text/javascript">
alert("Sent!");
</script>
<?php
}
?>
<?
if ($_POST['submit'])
{
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['msg'];
$to_add = "***********#gmail.com";
$from_add = "form#***********.net";
$email_message = "<br /><br /><b>Name:</b><br />$name <br />";
$email_message .= "<br /><br /><b>Reply Email:</b><br />$email <br />";
$email_message .= "<br /><br /><b>Regarding:</b><br />$subject <br />";
$email_message .= "<br /><br /><b>Message:</b><br />$message <br />";
$headers = "From: $from_add \r\n";
$headers .= "Reply-To: $from_add \r\n";
$headers .= "Return-Path: $from_add\r\n";
$headers .= "X-Mailer: PHP \r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html;\r\n";
mail($to_add,$subject,$email_message,$headers);
//echo js alert
}else{ // form not submitted yet
//display form code
}
?>
What you're describing is essentially what AJAX is. If you are using jquery you can use its $.ajax function on the button press and send the data via its data property. Based on your current PHP code it might look something like this:
$.ajax({ url: '/route/to/php/function',
data: {name = nameVal,
email = emailVal,
subject = subjectVal,
message = messageVal},
type: 'post',
success: function(output) {
//do whatever after the POST is successful
}
});
You'd put the form clearing code and SweetAlert code in the same event listener for the submit button, either before or after the $.ajax call.