This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 5 years ago.
I am a complete novice using php. I just need to get some information from a web form sent to my email address. I wrote this php a year ago and it worked, but I have come back to re-use it now and it is not working. Can anyone help? Its for a simple wedding RSVP form.
<body>
<center>
<div id="logo"><img src="../content/Logo.png" width="400" height="100" /></div>
</center>
<div id="WebsiteContainer">
<center>
<div id="Menu">
<a href="home.html" >Home</a>
Venue
RSVP
The Day
Gifts
</div>
<div id="Content">
<p> </p>
<h1><strong>RSVP</h1>
<p>
<center>
<p>Please use the form below to RSVP.
If we don't receive an RSVP we will presume that you will not be attending. If you have any requests for our DJ, please let us know in the message box below. And should you have any particular dietary needs, please let us know these too.
<p>
<?php
// PHP creates a $_GET variable automatically which contains any URL parameters passed. We check whether the 'submit' param
// exists using isset(), which is where our form is submitting to. If it does exist, we know the form has been submitted
// and we can process it.
if( isset( $_GET['submit'] ) ){
// Get the variables from the form
// PHP automatically creates the $_POST variable for form data, and the keys are the names we gave the form fields
$name = $_POST['Name'];
$email = $_POST['Email'];
$RSVP = $_POST['RSVP'];
$guests = $_POST['ReplyFor'];
$message = $_POST['message'];
// Lets build the email body so it includes the name too
// PHP concatenates strings with .
$emailBody = $name . " has RSVP'd! \r\n\r\n";
$emailBody .= "Name: " . $name . "\r\n";
$emailBody .= "Email: " . $email . "\r\n";
$emailBody .= "Status: " . ( $RSVP == 'not' ) ? "Not attending" : "Attending" . "\r\n";
$emailBody .= "Guests: " . ( $guests > '1' ) ? "Themselves and " . ( $guests - 1 ) . " guest(s)" : "Themselves only" . "\r\n";
$emailBody .= "Message: " . $message . "\r\n";
// Lines in emails must be under 70 characters, so we can use PHP's wordwrap() function to add line breaks
$emailBody = wordwrap( $emailBody, 70, "\r\n" );
// Create a subject that's easy to browse in your email client
$extraSubject = ( $guests > '1' ) ? "(plus " . ( $guests - 1 ) . " guest(s) )" : "";
$subject = $name . " is " . ( ( $RSVP == 'not' ) ? ' not attending' : ' ATTENDING ' . $extraSubject );
// Send the mail using PHP's built-in mail() function
// See http://php.net/manual/en/function.mail.php
mail( mymail#somewhere.com', $subject, $emailBody );
// Now we can print out a message to them, depending on what they said
$outputMessage = ( $RSVP == 'not' ) ? "We're really sorry you can't make it!" : "We can't wait to see you at the venue in August!";
?>
<div>
<strong>Thanks so much for RSVPing!</strong><br>
<?=$outputMessage?><br><br>
- Sarah & Gavin
</div>
<?
}
else
{
?>
<form id="RSVP_Form" name="form1" method="post" action="?submit=true">
<div>
<label for="Name">Name:</label>
<input name="Name" type="text" id="Name" size="30">
</div>
<div>
<label for="Email">Email:</label>
<input name="Email" type="email" id="Email" size="30">
</div>
<div>
<label>RSVP:</label>
<input name="RSVP" type="radio" value='attending'>Attending
<label></label>
<br><input name="RSVP" type="radio" value='not'>Not Attending
</div>
<div>
<label for="ReplyFor">Replying For:</label>
<select name="ReplyFor" id="ReplyFor">
<option value="1" selected="selected">Yourself Only</option>
<option value="2">Yourself + 1 other invited guest</option>
<option value="3">Yourself + 2 other invited guest</option>
<option value="4">Yourself + 3 other invited guest</option>
<option value="5">Yourself + 4 other invited guest</option>
</select>
</div>
<div>
<label for="message">Message:</label>
<textarea name="message" id="message" rows="3" cols="26"></textarea>
</div>
<div></div>
<div id="submit">
<input name="Submit" type="submit" value=" Submit ">
</div>
</form>
<?
}
?>
</div>
</div>
</body>
</html>
Check out this line:
mail( mymail#somewhere.com', $subject, $emailBody );
You are missing one single quote before email adress.
Related
This question already has answers here:
Reference - What does this error mean in PHP?
(38 answers)
Closed 2 years ago.
Is there an efficient way to pass variables from one page to another after form submission? I'm struggling to maintain the variables that are submitting on the form page to display them on the confirmation page after being redirected on submission.
Could it be the way i'm 'storing' the data with $_POST? Should I be using sessions? If I should how would I go about storing the $_POST in $_SESSION and being able to call it in the email template as a $variable-name format? Is using header(); to redirect inefficient in this manner and maybe redirecting via ajax? Not sure how I would approach that if so.
Form:
<form id="pricing-form-inquiry" action="<?php echo get_stylesheet_directory_uri(); ?>/pricing-form/form-handler.php" method="POST" role="form">
<div class="pricing-modal" id="modal1" data-animation="slideInOutLeft">
<div class="modal-dial">
<header class="modal-head">
<a class="close-modal" aria-label="close modal" data-close></a>
</header>
<section class="modal-body">
<div class="row">
<div class="col">
<input type="text" class="" placeholder="First Name" name="first-name" required data-error="First Name is required.">
</div>
<div class="col">
<input type="text" class="" placeholder="Last Name" name="last-name" required data-error="Last Name is required.">
</div>
</div>
<input type="email" class="" placeholder="Email Address" name="email" required data-error="Email Address is required.">
<input type="text" class="" placeholder="Company" name="company" id="company">
<input type="text" class="" placeholder="Phone Number" name="phone" id="phone">
<div class="row">
<div class="col text-center"></div>
</div>
</section>
<footer class="modal-foot">
<input type="submit" class="pricing-form-submit" value="Calculate" name="submit">
</footer>
</div>
</div>
</form>
form-handler.php
if(isset($_POST['submit'])) {
$first_name = filter_var($_POST['first-name'], FILTER_SANITIZE_STRING);
$last_name = filter_var($_POST['last-name'], FILTER_SANITIZE_STRING);
$email = filter_var($_POST['email'], FILTER_SANITIZE_EMAIL);
$company = filter_var($_POST['company'], FILTER_SANITIZE_STRING);
$phone = filter_var($_POST['phone'], FILTER_SANITIZE_STRING);
$to = "email#email.com"; // Email Address to send lead to
$subject = "Subject Goes Here!"; // Subject of generated lead email
// HTML Message Starts here
$message = "<html><body><table style='width:600px;'><tbody>";
$message = "<tr><td style='width:150px'><strong>Name: </strong></td><td style='width:400px'>$first_name $last_name </td></tr>";
$message = "<tr><td style='width:150px'><strong>Email Address: </strong></td><td style='width:400px'>$email</td></tr>";
$message = "<tr><td style='width:150px'><strong>Company Name: </strong></td><td style='width:400px'>$company</td></tr>";
$message = "<tr><td style='width:150px'><strong>Phone Number: </strong></td><td style='width:400px'>$phone</td></tr>";
$message = "</tbody></table></body></html>";
// HTML Message Ends here
// Always set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
// More headers
$headers .= 'From: Company <from#email.com>' . "\r\n"; // User will get an email from this email address
// $headers .= 'Cc: from#email.com' . "\r\n"; // If you want add cc
if(mail($to,$subject,$message,$headers)){
// Message if mail has been sent
echo "<script>
alert('Mail has been sent Successfully.');
</script>";
header("Location: /pricing-confirm/");
} else {
// Message if mail has been not sent
echo "<script>
alert('EMAIL FAILED');
</script>";
}
}
To pass your variables onto the pricing-confirm page, you could pass the variables in your header() function like so
header("Location: /pricing-confirm.php?name=" . $first_name);
Once on your pricing-confirm.php page, you can grab the variables from the query string
if(isset($_GET['name']) {
$name = $_GET['name'];
}
If you want to pass multiple variables at once, you can either use & in the query string, or use urldecode with an array like this
$arr = [
"firstname" => $first_name,
"lasttname" => $last_name,
]
header("Location: /pricing-confirm.php?userdetails=" . urlencode(serialize($arr)));
If you have used serialize, you can get the values in the array like this
$queryArr = unserialize(urldecode($_GET['userdetails']));
you can then access them with their array key, like so
if(isset($_GET['userdetails']) {
$arr = $_GET['userdetails'];
if(isset($arr['firstname']) {
$firstName = $arr['firstname'];
}
}
I was mistaken that I was missing anything from my PHP file. The only thing which makes the contact form working is if I use a way older version of the jQuery but that cannot be the case because I need SSL on my site and Google determines the site unsafe which wants to load unsafe scripts if they are older.
Here is the old version of the scripts:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
window.jQuery || document.write('</script>')
<script type="text/javascript" src="js/jquery-migrate-1.2.1.min.js"></script>
And here is what I was trying to use. I downloaded js files and uploaded to the server but I thinks because of the many function changes my PHP file cannot do the work anymore.
Can somebody help me to update my PHP file because I don't really do coding :o. Tried to understand how it works however I got confused.
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>window.jQuery || document.write('<script src="js/jquery-3.3.1.min.js"><\/script>')</script>
<script type="text/javascript" src="js/jquery-migrate-3.0.0.min.js"></script>
I use a template from Styleshout called Kreative101 and modified it in many ways but I didn't touch the contact form nor the PHP file (only inserting the error reporting and the email address).
I don't know if it needs any jQuery script in order to work because I did change that and the Modal Popup stopped working. Now it is back to the original (at least the footer with the scripts). The modal works the contact form doesn't.
Any help will be highly appreciated;).
If a reload the page says the action I took will be repeated
The contact form worked before, I tested it.
Here is the HTML code
<section id="contact">
<div class="row section-head">
<div class="col full">
<h2>Contact</h2>
<p class="desc">Get in touch with us</p>
</div>
</div>
<div class="row">
<div class="col g-7">
<!-- form -->
<form name="contactForm" id="contactForm" method="post" action="">
<fieldset>
<div>
<label for="contactName">Name <span class="required">*</span></label>
<input name="contactName" type="text" id="contactName" size="35" value="" />
</div>
<div>
<label for="contactEmail">Email <span class="required">*</span></label>
<input name="contactEmail" type="text" id="contactEmail" size="35" value="" />
</div>
<div>
<label for="contactSubject">Subject</label>
<input name="contactSubject" type="text" id="contactSubject" size="35" value="" />
</div>
<div>
<label for="contactMessage">Message <span class="required">*</span></label>
<textarea name="contactMessage" id="contactMessage" rows="15" cols="50" ></textarea>
</div>
<div>
<button class="submit">Submit</button>
<span id="image-loader">
<img src="images/loader.gif" alt="" />
</span>
</div>
</fieldset>
</form> <!-- Form End -->
<!-- contact-warning -->
<div id="message-warning"></div>
<!-- contact-success -->
<div id="message-success">
<i class="icon-ok"></i>Your message was sent, thank you!<br />
</div>
</div>
And the PHP code (I inserted the error reporting but I don't know if it's correct.)
<?php
error_reporting(-1);
ini_set('display_errors', 'On');
set_error_handler("var_dump");
// Replace this with your own email address
$siteOwnersEmail = 'info#virtualpropertyreview.com';
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
}
?>
In the comment I suggested to redirect after sending the mail - not sure if you understood what I meant but like this.
$mail = mail( $siteOwnersEmail, $subject, $message, $headers );
header('Location: ?mailsent=' . $mail ? 'true' : 'error' );
That should prevent the form being submitted if the page is reloaded accidentally etc
You could use that GET variable to display a message to report on status of the mail send.
if( !empty( $_GET['mailsent'] ) ){
echo $_GET['mailsent']=='true' ? "your message was sent" : "Sorry, there was an error"; /* etc */
}
This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 6 years ago.
so I'm making a website for a customer and he wants me to make a contact us page, now I'm trying to make it say in the email message: "You have been contacted by $name $name2 and his email is $from. \n The message he wanted to say was in the subject of $message and the message is $message2" help?
my html code:
<form action="contactus.php" method="post">
<div class="form-group">
<label for="Subject">Subject*</label>
<input name="subject" class="form-control" id="subject1" placeholder="Subject">
</div>
<label for="sabject">Why do you want to contact us?</label>
<br>
<select name="select">
<option value="General Inquiry">General Inquiry</option>
<option value="hampion Guide Request">Champion Guide Request</option>
<option value="League of Legends Show(s) Request">League of Legends Show(s) Request</option>
<option value="Podcast - League of Legends">Podcast - League of Legends</option>
</select>
<label for="fname">First Name*</label>
<input type="text" name="firstname" class="form-control" id="namef" placeholder="First Name">
<label for="lname">Last Name</label>
<input type="text" name="lastname" class="form-control" id="namel" placeholder="Last Name">
<label for="email_adress">Email Adress*</label>
<input type="email" name="email_adress" class="form-control" id="email_adress" placeholder="Email Adress">
<label for="message">Message*</label>
<input name="message2" class="form-control" id="message" placeholder="message">
<input style="margin-top: 50px;" value="Send Form" name="submit" type="submit">Submit</button>
</form>
php:
<html>
<head>
<title> PHP script</title>
</head>
<body>
<?php
$to ="sudaiguy1#gmail.com";
$from = isset($_POST['email_adress']) ? $_POST['email_adress'] : '';
$email_subject = isset($_POST['subject']) ? $_POST['subject'] : '';
$message = isset($_POST['select']) ? $_POST['select'] : '';
$message2 = isset($_POST['message2']) ? $_POST['message2'] : '';
$name = isset($_POST['firstname']) ? $_POST['firstname'] : '';
if (empty($name)||empty($from))
{
echo "Name and email are mandatory!";
exit;
}
elseif (empty($email_subject)||empty($message)) {
echo "Your email or subject are blank, please write something in them";
exit;
}
elseif (empty($name)||empty($message2)) {
echo "Your name or message are empty";
exit;
}
$name2 = isset($_POST['lastname']) ? $_POST['lastname'] : '';
$email_from = "sudaiguy1#gmail.com";
$body = $_POST['You have been contacted by $name $name2 and his email is $from. \n The message he wanted to say was in the subject of $message and the message is $message2'];
$headers = "From: $body";
mail($to , $email_subject , $body ,$headers);
?>
</body>
</html>
My problem is that it says unidentified variables index or something like that
The undefined index comes from $body = $_POST['You have be..]
Try this for $body:
$body = 'You have been contacted by ' . $name . ' ' . $name2 . ', and his email is ' . $from . 'The message he wanted to say was in the subject of' . $message . ' and the message is ' . $message2;
I am new to this and trying to figure out how to add a few extra features to this code.
On submit the page needs to redirect to an external URL i.e. www.google.com
The checkbox must be automatically checked and when the client gets emailed the details it must return TRUE/FALSE for subscriptions to the newsletter (so client knows whether they want to opt in or not)
This is my PHP code:
<?php
// Enter the email where you want to receive the notification when someone subscribes
$emailTo = 'nissberry#gmail.com';
$subscriber_email = addslashes(trim($_POST['email']));
if (!isEmail($subscriber_email)) {
$array = array('valid' => 0, 'message' => 'Insert a valid email address!');
echo json_encode($array);
} else {
$array = array('valid' => 1, 'message' => 'Thanks for your subscription!');
echo json_encode($array);
// Send email
$subject = 'New Subscriber (Free eBook)!';
$body = "You have a new subscriber!\n\nEmail: " . $subscriber_email;
// uncomment this to set the From and Reply-To emails, then pass the $headers variable to the "mail" function below
// $headers = "From: ".$subscriber_email." <" . $subscriber_email . ">" . "\r\n" . "Reply-To: " . $subscriber_email;
mail($emailTo, $subject, $body);
}
?>
And this is my HTML:
<form class="form-inline" role="form" action="assets/subscribe.php" method="post">
<div class="form-group">
<label class="sr-only" for="subscribe-email">Email address</label>
<input type="text" name="email" placeholder="Enter your email..." class="subscribe-email form-control" id="subscribe-email">
</div>
<button type="submit" class="btn">Receive your free eBook</button>
<br>
<div class="checkbox">
<label>
<input type="checkbox" class="checked"> Receive Our Monthly Newsletter
</label>
</div>
</form>
<div class="success-message"></div>
<div class="error-message"></div>
If there was no output before mail() function (no echo or html) you
could use this just before closing PHP tag:
header("Location: www.google.com");
exit;
So to redirect with PHP you need to remove all echo's from your code. If this can't be done, use javascript:
window.location.replace("www.google.com");
Add 'checked' attribute to your checkbox. It should have a name too,
so you can access it with global $_POST variable:
<input name="newsletter" type="checkbox" class="checked" checked>
Then you can extend your message:
$newsletter_subscription = isset($_POST['newsletter']) ? 'TRUE' : 'FALSE';
$body .= 'Newsletter subscription: '.$newsletter_subscription;
[Edit - added a complete form snippet] In my html - I have a single checkbox - which looks like this:
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.2/css/bootstrap.min.css" rel="stylesheet" />
<form class="contact-form row" id="feedbacks" method="POST" action="feedback.php">
<div class="col-xs-10 col-xs-offset-1">
<fieldset class="form-group">
<label for="full_name"></label>
<input type="text" class="form-control" placeholder="Your name and surname" name="full_name" id="full_name">
</fieldset>
<fieldset class="">
<label for="feedback_email"></label>
<input type="text" class="form-control" placeholder="Your Email address" name="feedback_email" id="feedback_email">
</fieldset>
<div class="checkbox">
<label class="c-input c-checkbox">
<input type="checkbox" name="subbed" id="subbed">
<span class="c-indicator"></span>
<span class="text-muted m-l-1">subscribe to <abbr class="msa"></abbr> notifcation service.</span>
</label>
</div>
<fieldset>
<label for="message"></label>
<textarea class="form-control" rows="9" placeholder="Your message here.." name="message" id="message"></textarea>
</fieldset>
<div class="row m-t-1">
<fieldset class="col-xs-4 col-xs-offset-4">
<button class="btn btn-primary btn-block btn-lg" name="submit" type="submit" id="send_feedback">Send <i class="ion-android-arrow-forward"></i>
</button>
</fieldset>
</div>
</div>
</form>
<input type="checkbox" name="subscribed" id="subscribed" value="sub_me">
In my PHP, I've created a variable $subscribe which links to the subscribed checkbox.
The PHP is supposed to send an email of "I would not like to recieve news emails" when the checkbox is left alone. To achieve this I have opted to use the following ternary inside the PHP form validation code:
[Edit - Supplying all the PHP]
<?php
$value = '';
$error = "";
$error_message = "";
$info = "";
if($_SERVER['REQUEST_METHOD'] == "POST"){
$subscribe = 'Would ' . (isset($_POST['subbed']) && $_POST['subscribed'] == 'sub_me' ? 'like to ' : 'not like to ') . 'receive news emails.';
$admin_email = "myemail_address#gmail.com";
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= 'From: <noreply#domain.ac.za>' . "\r\n";
$headers .= 'Reply-To: ' . $feedback_email . "\r\n";
'X-Mailer: PHP/' . phpversion();
$full_name = $_POST['full_name'];
$feedback_email = $_POST['feedback_email'];
$feedback = $_POST['message'];
$rep_message = "some thank you message to " . $full_name;
$message = 'another message which references ' . $subscribe;
$reply = 'some message consisting of ' . $full_name;
mail($admin_email,"Feedback",$message,$headers,"-fforwardguy#gmail.com");
mail($feedback_email,"Feedback",$reply,$headers);
}
?>
[Edit] The Problem is that the form is only acknowledged as having been sent, but no data is received along with it.
[After edit extra info - Could the following things be affecting the form submission?
`
There are 2 forms on the page in question.
Both forms are submitted via AJAX.
The first form functions as expected.
Two mail functions are being used in this single PHP file (handling only one form).
After taking taking a closer look - I have found that the email which gets sent contains none of the text entered into the form.
(The AJAX function works and the success functions are run.)
Try this:
$subscribe = 'Would ' . (isset($_POST['subscribed']) && $_POST['subscribed'] == 'sub_me' ? 'like to ' : 'not like to ') . 'receive news emails.';
Here I am checking if the $_POST variable is actually set and then we are doing a loose comparison check to see if sub_me is the submitted value.
You could also try leaving the value attribute blank on the form element, then just check if $_POST['subscribed'] is actually set by just using isset($_POST['subscribed']). If the checkbox is NOT checked, isset($_POST['subscribed']) will return false.
Ok - so after I noticed what the actual problem was - I found out that My AJAX function was collecting data from the wrong form.