HTML5 form with PHP action on Heroku - php

I have a contact form on my site that is hooked up to a Mailgun server to send email upon submit.
All is working fine locally, but when hosted on Heroku, the form doesn't act as expected and instead triggers a download. Any idea what needs to be done to get this to behave properly?
I've attempted making my index.html an index.php and putting the PHP script in <?php ?> tags at the top of the page - both of which worked locally - but neither has worked on Heroku.
Thank you!
form from index.html:
<form method="post" name="contact_form" action="form.php">
<h1>want to chat?</h1>
<input type="text" name="name" placeholder="name" required>
<input type="email" name="email" placeholder="email" required>
<textarea name="message" cols="30" rows="10" placeholder="drop a message..." required></textarea>
<input type="submit" value="send off" name="submit">
<h1 class="email-thanks"></h1>
</form>
form.php file:
<?php
require '../vendor/autoload.php';
use Mailgun\Mailgun;
session_start();
if(isset($_POST['submit'])){
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
# Instantiate the client.
$mgClient = new Mailgun('key-xxxxxxxx');
$domain = "mail.laurenfazah.com";
# Make the call to the client.
$result = $mgClient->sendMessage($domain, array(
'from' => $name . ' <' . $email . '>',
'to' => 'Lauren <example#gmail.com>',
'subject' => 'Portfolio Message',
'text' => $message
));
}
header( 'Location: /' ) ;
session_destroy();
?>

Sounds like the server it is hosted on doesn't have PHP installed.

Related

Trying to send an email with PHP from HTML form resulting in an HTTP Error 405

So I wanted to make a contact form for a website that can actually send emails, but ended up with this error :
Here's my html code:
<div class="contact-fast">
<div class="contact-form">
<form id="contact-form" method="POST" action="contact-form-handler.php">
<input name="name" type="text" class="form-control" placeholder="Your Name" required> <br>
<input name="email" type="email" class="form-control" placeholder="Your Email" required> <br>
<textarea name="message" class="form-control" placeholder="Message" rows="8" required></textarea> <br>
<input type="submit" class="form-control submit" value="SEND MESSAGE">
</form>
</div>
</div>
And here's my PHP code:
<?php
$name = $_POST['name'];
$visitor_email = $_POST['email'];
$message = $_POST['message'];
$email_from = 'skeremobiel#gmail.com';
$email_subject = 'New Mail From Website';
$email_body = "Username: $name.\n".
"User Email: $visitor_email.\n".
"User Message: $message.\n";
$to = "said444b#gmail.com";
$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $visitor_email \r\n";
if ($visitor_email!=NULL) {
mail($to,$email_subject,$email_body,$headers);
}
header("Location: http://127.0.0.1:5500/social.html");
die()
?>
Here's how my page looks like before and after submitting the form:
I think the problem is that when i click on the submit button i get directed to 127.0.0.1:5500/contact-form-handler.php (as you can see in the last image in my question). It doesn't execute the php file, but it just opens it.
Any help would be appreciated!
Many things can go wrong.
Make shure php error reporting is on. Google for that.
Try to find where the error code is coming from by reducing your problem.
For instance, comment out mail() and see if it works.
Or replace your php code by a simple text to see if the code runs, if the text appears on your browser screen.
Or Google for error 405. You will learn it is an http error, something in the data exchange between your browser and the server.
Do you have characters before <?php because if you have, the server will not be able to output the HTTP header any more.
Is your php file located at the webserver root and called contact-form-handler.php?

Simple form using HTML and PHP - page not found

I'm looking to send data from an HTML form to an email address using PHP. I have the following code in index.html:
<form class="container" name="rsvp" method="POST" action="form-to-email.php">
<input type="text" class="name" name="name" placeholder="Your Name" />
<input type="email" class="email" name="email" placeholder="Your Email" />
<input type="submit" class="btn btn-primary" value="Send"/>
In the same directory I have form-to-email.php:
<?php
if(isset($_POST['submit'])) {
$to = "myemail#gmail.com";
$subject = "RSVP";
$name_field = $_POST['name'];
$email_field = $_POST['email'];
$body = "From: $name_field\n E-Mail: $email_field\n";
echo "<h1>Your form has been submitted</h1>";
mail($to, $subject, $body);
} else {
echo "<h1>Error</h1>";
}
?>
When I click the submit button, I get a 'Page not working' error.
I am extremely new to PHP so I can't understand what is going wrong. I'm using Live Server with VS code and the website is currently being hosted on port 5500.
Should I have another server running in order for PHP to work? I believe I have PHP installed on my machine (Mac), but I don't know if I need to start up XAMMP or something similar even for a simple form like this to work? And if this is the case, how would the form then function on a live server? I usually use Netlify to host my projects.

Can I use action='POST' to PHP from contact form in React JS?

I have tried to post form data from a React JS rendered form, calling aq php file with the action part of the form... I have uploaded the form and the attached PHP file to my website, but when the form is filled out out submitted, no email is sent, and the url changes back to the homepage with the name of the PHP file tagged on (index.js/contact.php).
Is my code below bad?
Or is this just not possible to do using a combination of React JS and PHP?
contactMe.js
import React, { Component } from 'react';
class ContactMe extends Component {
render() {
return (
<div className='contact-body'>
<div className='contact-right'>
<div className='contact-side'>
<h2 style={{ fontSize: '25px', fontFamily: 'Anton', paddingTop: '10px'}}>
Contact Me
</h2>
<hr />
<div className='contact-list'>
<div className='form'>
<form action="contact.php" method='post'>
<input type="text" name="name" placeholder="Your name..." />
<input type="email" name="email" placeholder="Your email..." />
<textarea name="message" placeholder="Message me..."></textarea>
<input type="submit" value="Submit" />
</form>
</div>
</div>
</div>
</div>
</div>
)
}
}
export default ContactMe;
contact.php
<?php
if (isset($_POST['submit'])) {
$userName = $_POST['name'];
$userEmail = $_POST['email'];
$userMessage = $_POST['message'];
$emailSubject = 'New Message From My Website Visitor';
$emailBody = 'Visitor Name: ' . $userName . '\n\n' .
'Visitor Email: ' . $userEmail . '\n\n' .
'Message: ' . $userMessage . '\n\n';
$emailTo = 'james_ross#outlook.fr';
$headers = 'From: ' . $emailFrom . '\n\n';
mail($emailTo, $emailSubject, $emailBody, $headers);
header('Location: index.js/mailsent');
}
If I need to use another technique/language in place of the PHP, please advise me of s decent source to read up on it, Thank you,
action="contact.php" is a relative link and it looks like your frontend runs as xxx/index.js/, you have to set it to an absolute url like action="/contact.php" or action="xxx/contact.php" where xxx is the folder with your php script.
If you develop with react than you don't want to switch between server side and client side pages, just send your form with ajax and display the output in your react app.
Your PHP code has a security issue:
$emailFrom is not defined, if you have some old PHP configuration with register_globals than it is possible to insert own header content and f.e. send spam emails to any email addresses.

PHP Running in some directories but not others

I have a few simple html websites with a contact form that sends emails via PHP. They all works great with no issues at all. I am trying to build another website now using the same contact form PHP script. However, when the user submits the form, instead of getting a success message, they get a "internal server error" message. The PHP script is identical (with the exception of the captcha key). The HTML is identical (with the exception of the captcha key) and some css styling.
Below is the HTML:
<form action="scripts/contact.php" method="post" target="resultMsg" style="width:300px; margin:auto;">
<input class="formInput" type="text" name="name" id="name" placeholder="Name"/>
<br/><br/>
<input class="formInput" type="text" name="email" id="email" placeholder="Email"/>
<br/><br/>
<textarea class="formInput" name="message" id="message" placeholder="Message"></textarea>
<br/><br/>
<div style="margin:auto;width:fit-content">
<div class="g-recaptcha" data-sitekey="xxxxxxxxxxxxxxxx"></div>
</div>
<br/><br/>
<input class="send" type="submit" value="Send"/>
</form>
<br /><iframe name="resultMsg" id="resultMsg" scrolling="no" style="border:0; height:30px; width:100%"></iframe>
Below is the PHP code in the scripts/contact.php file:
<?php
if(isset($_POST['g-recaptcha-response'])){
$captcha=$_POST['g-recaptcha-response'];
}
if(!$captcha){
echo("<p style='color:white; width:100%; text-align:center;'>Error: Please check the captcha box.</p>");
exit;
}
$secretKey = "xxxxxxxxxxxxxxxxxxxxxxxx";
$ip = $_SERVER['REMOTE_ADDR'];
$url = 'https://www.google.com/recaptcha/api/siteverify?secret=' . urlencode($secretKey) . '&response=' . urlencode($captcha);
$response = file_get_contents($url);
$responseKeys = json_decode($response,true);
if($responseKeys["success"]) {
$myemail = "myemail#ymail.com";
$subject = "Contact Form Submission";
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$sendMessage = "CONTACT FORM SUBMISSION
NAME:
$name
EMAIL ADDRESS:
$email
MESSAGE:
$message";
mail($myemail, $subject, $sendMessage);
echo("<p style='color:white; width:100%; text-align:center;'>Thank you for your email!</p>");
} else {
echo("<p style='color:white; width:100%; text-align:center;'>Error: message not sent, please refresh the browser.</p>");
}
exit();
?>
I have had issues with this last week, but the hosting company added AddHandler application/x-httpd-ea-php56 .php to the htaccess file and it fixed it. They also added extra htaccess files with this line all over the website directories for some reason. Then I think I may have accidentally deleted or changed some of the htaccess files and now it is not working again. What exactly is needed in which htaccess file for this to work correctly? Any ideas why it doesn't work anymore? Thanks
It turns out the issue was as simple as incorrect file permissions of the PHP script. Changing the permissions of the script to 644 fixed the problem, no more "internal server error".

Issue with contact form (html and php): 405 not allowed

I'm trying to make a very basic contact form using HTML and PHP. For some reason, however, when I click the "submit" button I get the error "405 Not Allowed". Why? How could I fix this? (I'm hosting my website on GitHub)
my HTML:
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<form action="send_form_email.php" method="POST">
<input type="text" name="name" placeholder="Full Name">
<input type="text" name="mail" placeholder="Your e-mail">
<input type="text" name="subject" placeholder="Subject">
<textarea name="message" placeholder="Message"></textarea>
<button type="submit" name="submit">Send e-mail</button>
</form>
</body>
</html>
my PHP:
<?php
if (isset($_POST['submit'])) {
$name = $_POST['name'];
$subject = $_POST['subject'];
$mailFrom = $_POST['mail'];
$message = $_POST['message'];
$mailTo = "example#gmail.com";
$headers = "From: ".$mailFrom;
$txt = "You have received an e-mail from ".$name.".\n\n".$message;
mail($mailTo, $subject, $txt, $headers);
header("Location: index.php?mailsend");
}
(PS. I wrote "example#gmail.com" just because I wanted to keep my personal e-mail private on here.
PPS. I have been trying to make a very simple contact form (you write your name mail and message and the owner of the website receives it in the inbox) but none of my (desperate) attempts seems to work. Can anyone help me out?)
Github is a hosting service for static web pages. PHP is not static. Deploying PHP to GitHub Pages - is it possible?

Categories