This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed last year.
I have been trying to build my own website and a central part of it is a form. I have set up an email address to recieve form results. I have been able to run the website on localhost and everything but the forms are working. When I click submit, it sucsessfully sends me to the PHP file and I am not recieving any emails from the file. I have checked my spam and it is empty. I am brand new to PHP and I tried to adapt some code I found online. I am not sure if there is anything wrong with my code or not. If anyone could help that would be great. I am also trying to be able to send an image file through the form. This is a screenshot of what the server log says when I try to submit the form. Server Log Screenshot
This is the code I have wrote:
<?php
$title = htmlspecialchars($_POST['title']);
$coordinates = htmlspecialchars($_POST['coordinates']);
$description = htmlspecialchars($_POST['description']);
$shelterorcampsite = htmlspecialchars($_POST['shelter-campsite']);
$distanceToWater = htmlspecialchars($_POST['howfarawayiswater']);
$image = $_POST['file'];
$email_from = 'contactbackpackingproject#gmail.com';
$email_subject = "New Form submission";
$email_body = "There has been a new campsite submission.
The title is: $title;
The type is: $shelterorcampsite;
The coordinates are: $coordinates;
The description is: $description;
The distance to water is: $distanceToWater;
An image is: $image";
$to = 'contactbackpackingproject#gmail.com';
mail($to,$email_subject,$email_body,$headers);
?>
This is a screenshot of the code is VS code: VS code screenshot
Often it's helpful to find the exact error message that is triggered by the mail() function. While the function doesn't provide an error directly, you can use error_get_last() when mail() returns false.
<?php
$success = mail('example#example.com', 'My Subject', $message);
if (!$success) {
$errorMessage = error_get_last()['message'];
}
?>
Please identify the errors as without them nobody can help you and the log that you provided is just a POST request which doesn't necessarily indicate that mail function executed successfully.
Related
This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 2 years ago.
I am trying to send a basic confirmation email after a user submits my html form using the php mail() function, but for some reason the email won't send. Here is my code:
<?php
echo "You're all set! You should be recieving a confirmation email within the next few minutes.";
$to = $_POST["email"];
$subject = "Your Appointment";
$msg = "Your appointment has been scheduled for " . $_POST["ApptDate"] . ".";
mail($to,$subject,$msg);
?>
I know my form is working correctly, because I am taken to this page after I submit the form. Also, "email" and "ApptDate" are keys I stored in my html form, and when I echo the variables they all print, so I don't think its a variable related error. Any help would be greatly appreciated.
is it localhost? or a server?
You would need an SMTP server setup in either case,
For localhost i use this one as its a one-click-never-touch-again setup
https://toolheap.com/test-mail-server-tool/users-manual.html
This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 3 years ago.
So I'm learning my way through PHP and I followed a tutorial exactly for creating a form that uses php to send me an email through the server I'm using but its not working. I checked around to see if my question has been answered in a way I can understand but I came up empty. I probably just don't know enough about this language but I want to get this feature up and running at least so I can have a way to receive emails without giving out mine. please don't suggest other options I want to fix this code.
I have checked to make sure I followed the tutorial correctly and I have. I am missing something key here I just don't know what.
<?php
if (ifset()$_POST['submit']) {
$name = $_POST['name'];
$subject = $_POST['subject'];
$mailFrom = $_POST['mail'];
$message = $_POST['message'];
$mailTo = "https://formspree.io/xoqaovaa"
$headers = "From: ".$mailFrom;
$txt = "you have recieved an E-mail from ".$name.".\n\n".$message;
mail($mailTo, $subject, $txt, $headers);
header("Location: contactForm.php?mailsend");
}
?>
I expected my contact form to accept inputs from the user in various fields and send those in a message though my server to my email routing service when the user clicks submit button.
what happens when I go to the page is this error: Parse error: syntax error, unexpected '$_POST' (T_VARIABLE) in /storage/ssd5/051/11462051/public_html/PHP/master.php on line 2
The name of the function is isset(), not ifset(). And you have the ) in the wrong place. It should be:
if (isset($_POST['submit'])) {
And then you are missing the terminating ; at this line:
$mailTo = "https://formspree.io/xoqaovaa"
This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 8 years ago.
For some reason My PHP email form does not send the html form data, the headings are there, but the imputed data is not however when the files are put on the development server, a sub domain of the main website 123.123.com, the email sends with the form data, with no issue at all.
It seems to do this on all the forms on the website, could this be server related? or am I missing something. any help would be appreciated.
<?php
mail( "Test#test.con, "Interlinks Sign Up",
"\nName: - $fulllname
\nEmail:- $email
\nContact Number: - $contactnumber
\nCountry:- $country
\nTown/City:- $city
\nPost Code:- $postcode
\nAbout Yourself: - $about
\nWork And Business: - $work
\nJob Description: - $job
\nIndustry Description: - $industry
\nWebsite: - $website
\nLanguage: - $language
\nWho To Meet:- $meet
\nAgree: - $agree ",
"From: blank#test.com" );
?>
In order to get the data from your HTML form you will need to use $_POST['form_element_name_here'] or $_GET['form_element_name_here'] depending on the type of form action that you used. If you are already doing thing and are binding these to the variables that you are using in your original code we will need to have a look at the entire PHP script and HTML front end that is processing the form.
I am a PHP newb so please bear with me for this rather simplistic question.
I have a PHP form setup like so >>
<?php
if($_POST){
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$comments = $_POST['comments'];
if($comments)
$error = "There was an error, please give us a call at ### ###-####.";
else{
if($name=="Name" || $email=="Email" || $message=="Message"){
$error = "All fields are required, please fill them out and try again.";
}else
$header = "From: $name <$email>";
$message = "Name: $name\n\nEmail: $email\n\nMessage: $message";
if(mail("email#domain.com", 'Form Submission', $message, $header))
$success = "Thanks for sending us your message, we'll get back to you shortly.";
else
$error = "There was an error, please give us a call at ### ###-####.";
}
if($error)
echo '<div class="msg error">'.$error.'</div>';
elseif($success)
echo '<div class="msg success">'.$success.'</div>';
}
?>
The basic idea is that the form has descriptive text pre-filled in each field but when you click on them they are cleared via Javascript. I want to prevent people from pressing send on the form without filling it out, hence the "if($name=="Name" || $email=="Email" || $message=="Message"){" bit. However while that message is working the form is still submitting. Why is this. Also please note that the "comments" field is in fact a honeypot. Thanks!
Because php is server-side. You need to look into javascript validation for what you want. To validate with php you HAVE to submit the form.
One tutorial but I recommend Jquery validation
"I want to prevent people from pressing send on the form without filling it out, hence the "if($name=="Name" || $email=="Email" || $message=="Message"){"
All you need to do is disable the submit button client side until the proper validation is met, then also validate server side. As #Iznogood said, that's why your gettin your error
Like lznogood said, PHP validates the form on the server, so you need to send the information to the server before PHP can process it.
If you want the form to be checked on the user side before sending it to the server you should use JavaScript to do that (jQuery is a great tool to use), and after checking it on the user side you can decide whether to send the form to the server or not.
Though this isn't an answer to your question, you might be interest in the new html5 feature placeholder. Read about it here. You can check to see which browsers it works in here (stupid internet explorer!). 5 years ago, I would put those "hints" as the value, which was a pain to validate. placeholder makes it sooooooo much easier. Your tag would look like:
<input type="text" name="email" placeholder="Enter Your Email Here" value="">
Note that value is empty. You can omit the value attribute, I left it in to show it's not needed here.
As far as an answer to your original question, everybody else is correct, javascript is the way to go.
I need a little help with sending a message to my e-mail via php. Here's the code I've got so far:
<?php
$newline = $_GET['message'];
$newline = str_replace("[N]","\n"
,"$newline");
$newline = str_replace("[n]","\n"
,"$newline");
mail($_GET['to'],$_GET[
'subject'],$newline,"From: ".$_GET['from']);
echo "<FONT FACE=\"Verdana\" SIZE=\"2\">".$_GET[
'thanks']."</FONT>";?>
I uplaoded this php code to my sever, then use the execute_shell() function to send the message to my e-mail by then defining the various variables. Here's that code:
msg_to="linera#low.com";
msg_from="celebraces#gmail.com";
msg_subject="Hey, what's up?";
msg_message="This is a test email :D";
msg_thanks="Your message has been sent :D";
execute_shell("http://yourserver.com.au/
yourgame/sendmail.php?to="+msg_to+"&from="
+msg_from+"&subject="+msg_subject+"&
message="+msg_message+"&thanks="+msg_thanks,"");
The problem is that, when the message is done, it just shows msg_thanks, which I defined as a variable. I'm new to php and dont know much. All I want to do is to remove this and rather, redirect the user to a particular thank you webpage when the message is done. How do I do this?
Try header():
header("location:../thankyou.php");