Trying to create a contact form using PHP with Bluehost [duplicate] - php

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 6 years ago.
I'm new to PHP, but I found some sample code for a web form. I adapted it like this, in a file called contact.php:
<?php
mail('contact#mywebsite.com', $_POST['name'], $_POST['email'], $_POST['subject'], $_POST['message']);
?>
<p>Your message has been sent.</p>
and here's the HTML:
<form action="contact.php" method="post" enctype="text/plain">
Name<br>
<input type="text" name="name" value=""><br><br>
Email<br>
<input type="text" name="email" value=""><br><br>
Subject<br>
<input type="text" name="subject" value=""><br><br>
Message<br>
<textarea name="message" rows="10" cols="50"></textarea><br><br>
<input type="submit" value="SUBMIT">
</form>
It wasn't working, so I did some research and discovered Bluehost requires you to use their own Bluemail for this. So I ditched the PHP and followed their tutorial. That didn't work either, so I did some more research and discovered they discontinued Bluemail. So I went back to the PHP method and changed the email to a bluehost email address (apparently that's required as well).
Long story short, I checked my junk mail folder and found some of the test emails I had tried to send from the form, but they are all blank. No subject, no message content.
So it seems like the contact form is working (in that it sends an email), but the actual information inputted in the form is not coming through. I assume there's a problem with my PHP code?
Any help would be greatly appreciated!

Seems you are using wrong syntax for mail
Try
<?php
$msg = $_POST['name']."<br/>". $_POST['email']."<br/>". $_POST['message'];
mail("contact#mywebsite.com",$_POST['subject'],$msg);
?>

You can try this code (perhaps will work for you):
<?php
$msg = $_POST['name']."<br/>". $_POST['email']."<br/>". $_POST['message'];
if (isset($_POST["email"])) {
mail($_POST["email"],$_POST['subject'],$msg);
echo "Your message has been sent.";
}else{
echo "N0, mail is not set";
}
?>

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

Browser loads up my PHP file rather than sending an email [duplicate]

This question already has answers here:
PHP code is not being executed, but the code shows in the browser source code
(35 answers)
Closed 5 years ago.
I want the form to send the contents to my email, instead it's just displaying my php code in a new window, can anyone help? here is the code:
Index
<form action="send.php" method="POST">
Name: <br><input type="text" name="name"><br><p>
Request: <br><textarea name="request"></textarea><p>
<input type="submit" name="submit" value="Send!">
</form>
PHP:
<?php
$name = $_POST['name'];
$request = $_POST['request'];
$to = "myemail#email.co.uk";
$subject = "It's just a test";
$body = "This is just a test to see if things are working okay \n\n $request";
mail ($to,$subject,$body);
echo "Message sent!"
?>
Few tips for beginners
First Configure your Xampp form send email How to configure XAMPP to send mail from localhost?
Check if the data is coming to send.php by print_r($_POST)
Follow this tutorial for sending email from here

How can I set up a contact form on my website? [duplicate]

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 6 years ago.
I've done my research on this but can't seem to get it to work.
I'm looking to add a contact form to my website that sends an email directly to me. I've watched videos and used code I've found online but nothing works. I even temporarily disabled my website and uploaded just a blank contact form (code below) and a php file (code below) with my only results being that the echo command at the end of the PHP file DOES show up. The email, though, still does not send.
What am I missing? Thank you!
HTML:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Temp</title>
</head>
<body>
<form method="post" action="send.php" name="contact_form">
<p>
<input name="name" type="text" />
</p>
<p>
<input name="email" type="text" />
</p>
<p>
<textarea name="message"></textarea>
</p>
<p>
<input type="submit" name="submit" id="submit" value="Submit" />
</p>
</form>
</body>
</html>
PHP:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$to = "email#myemail.co";
$subject = "Contact Form Submission";
mail ($to, $subject, $message, "From: " . $name);
echo "Your message has been sent. You can expect to hear from us soon.";
?>
I'm sure this is a duplicate, but I just wanted to mention a couple of things to check.
Does your mail() call return true? I would check that. It's possible that your PHP installation is not set up correctly to send mail. There are countless posts on how to check and configure that, which I would suggest reviewing if you haven't already (here's one, for example).
Depending on where you're hosting this, your host's configurations may restrict any outgoing mail that is not from the domain you're hosting. (I've had this problem myself on shared hosts.) Here you're setting the "from" header as the name of the person submitting the form (which would look something like: "From: John Doe"). This may be a problem either on the sending or receiving end of the email (either side rejecting it because it doesn't come from an email address, or a valid email address, etc). Try setting the "from" value to an email address valid on your host (e.g., "myname#mydomain.com"). Then, just include the person's name and email address in the $message of the email.
Hope that helps.

Form php in email [duplicate]

This question already has answers here:
Html form in email
(3 answers)
Closed 7 years ago.
I create many newsletters. Is it possible to add a php form in email ? I explain, the customer open this email and on this email he see an input to add his email. Is it possible for me to keep the data ? For example we can imagine a simple input to subscribe newsletter.
ya its possible... you need have a form in your email template and in form action you need to specify the php file which is there in your domain
E.x:
in your email template
<form method="GET" action="http://www.yourdomain.com/back_end_file.php">
<label>Email id</label>
<input type="text" name="email" />
<input type="submit" value="submit" />
</form>
In your server file i.e : http://www.yourdomain.com/back_end_file.php
<?php
echo $email = $_GET['email'];
?>
If it gives some cross domain issue then paste below code in first line in http://www.yourdomain.com/back_end_file.php this file
<?php header('Access-Control-Allow-Origin: *'); ?>
I hope this works...

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.

Categories