Redirecting from html to php - php

I am trying to write a website from scratch and try to add a form.
but if i click on the Submit button (which redirects to the contact.php) it should send a mail without opening any email program, but it wont redirect properly... I googled and didnt find anything...
the code of that form is:
<!--Some code over this code-->
<form action="contact.php" method="post">
<font color="ffffff">
<center>
<div>
<label for="name">Name:</label>
<input type="text" id="name" />
</div>
<div>
<label for="Username">Username(s):</label>
<input type="text" id="name" />
</div>
<div>
<label for="Job">Job:</label>
<select name="Job">
<option value="Artist">Artist</option>
<option value="Musican">Musican</option>
<option value="Props">Props</option>
<option value="Voice">Voice</option>
<option value="Effects">Effects</option>
</select>
</div>
<div>
<div>
<label>THE THING</label><br><br>
<input type="checkbox" name="Program" value="Skype">I have SKYPE<br>
<input type="checkbox" name="Program" value="Steam">I have STEAM <br>
<input type="checkbox" name="Program" value="Origin">I have ORIGIN <br>
<input type="checkbox" name="Program" value="Teamspeak">I have TEAMSPEAK <br>
</div>
</div>
<div>
<label for="Message">Message:</label>
<textarea id="Message"></textarea>
</div>
<br>
<div class="button">
<button type="submit">Send your message</button> <button type="reset">Clear the fields</button>
</div>
</center>
</font>
</form>
<!-- More code from my html -->
but when i now click on that button which should redirect from the index.html to contact.php it just gives an error (I tried Google drive and Github to host and on Dropbox it just wants to download the contact.php...) Gdrive error: 404 Page not found, Github: 405 no permission and like i said, dropbox just wants to download the file...
The code of the PHP:
<?php
$ToEmail = 'mymail#domain.com';
$EmailSubject = 'NEW CONTACT! READ IT STOOPID';
$mailheader = "From: ".$_POST["name"]."\r\n";
$mailheader .= "Reply-To: ".$_POST["name"]."\r\n";
$mailheader .= "Content-type: text/html; charset=iso-8859-1\r\n";
$MESSAGE_BODY = "Name: ".$_POST["name"]."";
$MESSAGE_BODY .= "Username: ".$_POST["Username"]."";
$MESSAGE_BODY .= "Job: ".$_POST["Job"]."";
$MESSAGE_BODY .= "Program".$POST["Program"]."";
$MESSAGE_BODY .= "Message: ".nl2br($_POST["Message"])."";
mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $mailheader) or die ("Failure");
?>
<center><h1>Thank you :D!</h1></center>
Is it because i use such cheap websites to host? I just dont want to spent thousands of bucks into a little site i created for planning, as well as learning HTML...

Welcome to StackOverflow.
Your error message says "gdrive and github cannot find the file". That is because, although they have your file, they are not set up to execute your files as PHP. Your issue here is that you are trying to run a php script in a non-server environment. You can set up a server environment on your own PC using XAMPP or WAMP Server. If you're using a Mac, you can use MAMP (free version or Pro).
Once you have installed the server bundle, you will have the most common Apache/PHP/MySQL setup. You will need to install your code in the X:/xampp/htdocs 1 folder (by default). To run your scripts, open your browser and go to http://localhost.
Also, domain name and hosting can cost as little as £16 per year for a .co.uk domain. .coms cost more. Other Top-Level Domains (TLDs) are available.
'X' is the drive letter for the drive in which you installed XAMPP or similar. For WAMP, use X:/wamp/www.

I think your «websites to host» does not support PHP. You need web server like «apache» for supporting PHP scripts. You can test this by creating a file called 'test.php' (or give it another name, just make sure it has .php as extention) and put in the following code:
<?php phpinfo() ?>
If your host supports PHP, you should get a confirmation while opening test.php with the PHP version they are running. If you do not see this confirmation, your host does not support PHP.

Related

PHP Email Form Issue: Cannot POST

I've been working on getting a PHP email form set up on my personal webpage, and I am running into an issue where when I hit submit, the page displays "Cannot post /mail.php".
I've seen several similar questions on this site before, but I haven't found any approaches that have solved the issue. I am new to PHP so I'm not sure how deep the error could be, or if I'm possibly missing something obvious.
I'm using Brackets Live Preview on Mac OS Big Sur.
Things I've tried:
Made sure that the mail.php file is in the same directory as my HTML files
Made sure I'm setting the name attribute in my HTML file so the PHP can pick up on the relevant entered information
Switched POST with GET (POST errors, GET download the PHP file instead of running it)
Downloaded PHP with Homebrew
Redownloaded Brackets
For reference this is my code:
HTML File:
<form action="mail.php" method="post">
<p>Name</p> <input type="text" name="name">
<p>Email</p> <input type="text" name="email">
<p>Message</p><textarea name="message" rows="6" cols="40"></textarea><br>
<input type="submit" value="Send"><input type="reset" value="Clear">
</form>
PHP File:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$formcontent="From $name \n Message: $message";
$recipient = "email.redacted#gmail.com";
$subject = "Contact Form";
$mailheader = "From $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error");
?>
Any insight into what may be going wrong or some debugging steps to take would be much appreciated!
Here <? php you have whitespace after the ?. Remove it, and see what happens.
It should be <?php to be valid syntax. Otherwise the script breaks.
Edit: I'm not claiming this will solve any logical errors in your code, but that space in there is making the script not even PHP.
Is your form you have method="get". For you to access the information using $_POST you'll need to change it to method="post".
<form action="/mail.php" method="post">
<p>Name</p> <input type="text" name="name">
<p>Email</p> <input type="text" name="email">
<p>Message</p><textarea name="message" rows="6" cols="40"></textarea><br>
<input type="submit" value="Send"><input type="reset" value="Clear">
</form>
With regards to why your Bracket Live Preview is downloading the PHP file instead of running it. It sounds like your Bracket Live Preview isn't configured correctly.
You might be able to get some help from this question.
Using Brackets for PHP files

How to run php files on netlify?

I build a website which has a form. Contact form redirects to contact.php on submit. For some reason whenever I submit, it says page not found.
index.html
...
<form action="contact.php" method="post" enctype="text/plain">
Name:<br>
<input type="text" name="name" class="form-control" required><br>
E-mail:<br>
<input type="email" name="mail" class="form-control" required><br>
Message:<br>
<input type="text" name="comment" size="50" class="form-control" required><br><br>
<button type="submit" value="Send"> Send Message </button>
</form>
...
contact.php
<?php
if($isset($_POST['submit']))
{
$name = $_POST['name'];
$mailFrom = $_POST['mail'];
$message = $_POST['comment'];
$mailTo = "sample#email.com";
$headers = "From: ".$mailFrom;
mail($mailTo, $name, $message, $headers);
header("Location: index.html");
}
?>
I added a build.sh file containing:
#!/bin/bash
php contact.php
I also added ./build.sh in build command. I feel my script is wrong. Please suggest me alternatives to solve this problem.
A Netlify site is deployed to a CDN and serves up static content and although you can run PHP at the time of a deploy, you cannot execute PHP during a page request.
To submit a form, you can use Netlify Forms or some other serverless forms solution.
I recommend using InfinityFree for hosting the PHP site for free. or if your need is just to send an email form and the rest is static pages then i recommend emailjs that will let you send emails for free. so that you can deploy your site in netlify or vercel
You can use 000webhost.com hosting for uploading your php projects.

PHP Submission Error [duplicate]

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 7 years ago.
I'm having a classic PHP form submission error. I tested the code earlier and it seemed to be working now when testing it again, the email never arrives.
This is the HTML (I changed the site/email address for security reasons of course)
<!-- Contact Form -->
<form method="post" action="http://example.net/assets/mail/mail.php">
<div class="row 50%">
<div class="6u 12u(mobile)"><input type="text" name="name" placeholder="Name" />
</div>
<div class="6u 12u(mobile)"><input type="email" name="email" placeholder="Email" />
</div>
</div>
<div class="row 50%">
<div class="12u"><textarea name="message" placeholder="Message" rows="6"></textarea>
</div>
</div>
<div class="row">
<div class="12u">
<ul class="actions">
<li>
<input type="submit" value="Send Message" />
</li>
</ul>
</div>
</div>
</form>
This is the PHP file:
<?php $name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$formcontent="From: $name \n Message: $message";
$recipient = "es#example.net";
$subject = "Contact Form";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "Thank You!";
?>
The PHP file is located at the same place the HTML links to: http://example.net/assets/mail/mail.php and the email itself is working fine (use it daily). When I test the form out I get the echo/message saying thank you so is definitely finding the PHP file however, I simply don't get anything on my inbox.
Is such a simple code yet I'm really confused and unsure what is going on.
Any help is truly appreciate it.
The email could be blocked in the server's mail queue or blocked as spam by the email host. For linux based system, the email is usually logged in the /var/logs directory and there should be an equivalent on windows based servers. From here you should be able to see whether or not the server sends the email out into the world.
The code appears to be correct.
The real problem is the use of the mail function of php. If you look at the official documentation, you can see how it is necessary that the parameters passed to the function must meet special requirements.
In addition, there are some difference using email() with Windows or Linux.
My advice is to not use direct mail() but a library to send as PHPMailer https://github.com/PHPMailer/PHPMailer
Your code is right! It seems like a mail server problem. Try to change the recipient mail address and check if your server is in black list: http://mxtoolbox.com/blacklists.aspx

PHP scripts not running on my website

I am currently going through elithecomputerguy's youtube playlist for php programming. I am on part 7 of his 11 part series, this section talks about sending email with php. He writes out all the php code on notepad++ and uploads it to a standard godaddy web hosting account. I am using the same exact method, copying his code line by line, setting up the files in the same location and when I upload my email_form.php, it displays correctly
<HTML>
<HEAD>
</HEAD>
<BODY>
<form action="email_script.php" method="POST">
<p>Email Address: <input type="text" name="email" size="30"></p>
<p>Subject: <input type="text" name="subject" size="30"></p>
<p>Message: </p>
<p><textarea rows="10" cols="20" name="message"></textarea></p>
<input type="submit" name="submit" value="Submit">
</BODY>
</HTML>
Now I uploaded my email_script.php file just fine and it looks like so:
<?PHP
$from="test#mikesmtgadvice.com";
$email=$_POST['email'];
$subject=$_POST['subject'];
$message=$_POST['message'];
mail($email, $subject, $message,"From:".$from);
print "Your message has been sent: </br?$email</br>$subject</br>$message</p>
?>
so when I go to mysite.com/test/email_form.php, it works and displays this form the way it should. However, when I fill in the form and hit submit, when it tries running the script, I get an 500 Internal Server Error. I have no idea where to look for this error and cannot find the error log, and therefore cannot figure this out. In the video, he does this exact same process and it works, is there a setting I need to change in my server or?
Thank you for your help in advance, I'm sorry for the long winded version but I wanted to be as specific as possible.
You are missing a double quote and ; at the end of the print statement:
print "Your message has been sent: <br />$email<br />$subject<br />$message</p>";
Replace that with your print line and it will fix it.

PHP emailing failing on WIMP

I've got a WordPress site with a contact form that works fine on my MAMP environment, but when I publish to my clients WIMP server I get a failure.
I am not at all familiar with WIMP environments- how does one go about checking PHP error logs
Offhand, are there issues with PHP emailing on WIMP that would be causing this?
Code:
<?php
if ($_POST["contact_name"]<>'') {
$ToEmail = 'me#domain.com';
$EmailSubject = 'New contact message';
$mailheader = "From: ".$_POST["contact_email"]."\r\n";
$mailheader .= "Reply-To: ".$_POST["contact_email"]."\r\n";
$mailheader .= "Content-type: text/html; charset=iso-8859-1\r\n";
$MESSAGE_BODY = "<b>Name:</b> ".$_POST["contact_name"]."<br>";
$MESSAGE_BODY .= "<b>Email:</b> ".$_POST["contact_email"]."<br>";
mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $mailheader) or die ("Failure");
?>
<h4>Your message was sent. We will be in touch with you shortly.</h4>
<?php
} else {
<form id="contact-form" name="contact" method="post" action="#">
<label for="contact-name">Name *</label>
<input type="text" id="contact-name" name="contact_name" tabindex="1" class="required"/>
<label for="contact-email">Email</label>
<input type="text" id="contact-email" name="contact_email" tabindex="2" class="email" />
<input type="submit" id="contact-submit" name="contact_submit" value="" tabindex="8" />
</form>
<?php
};
?>
Windows does not have a built in email server like unix type OSs tend to have. You need to configure php.ini to add SMTP server information through which to relay email.
The PHP manual page for the `mail()' function details a number of Windows-specific points. However, the main points which could affect you are in this section: (to quote)
The Windows implementation of mail() differs in many ways from the Unix implementation. First, it doesn't use a local binary for composing messages but only operates on direct sockets which means a MTA is needed listening on a network socket (which can either on the localhost or a remote machine).
Second, the custom headers like From:, Cc:, Bcc: and Date: are not interpreted by the MTA in the first place, but are parsed by PHP.
As such, the to parameter should not be an address in the form of "Something <someone#example.com>". The mail command may not parse this properly while talking with the MTA.
There are a few other things to consider as well; please read the manual page for more.
Hope that helps.

Categories