Simple php form = too simple to actually work? - php

I'm sitting here wondering wheather this php contact form solution is too simple to actually work. Or, well it does work, but will it always work?
Also, atm when you recieve the email, it says FROM: zsf34o6.mywebsite.com#web09.b-one.com
which means that most mail clients will put it straight in the junkbox. How can I change it to the entered email address?
Thanks!
<form method="POST" action="mailer.php">
<input type="text" name="name" size="19">
<input type="text" name="phone" size="19">
<input type="submit" value="Submit" name="submit">
</form>
<?php
if(isset($_POST['submit'])) {
$to = "you#you.com";
$subject = "From website";
$name_field = $_POST['name'];
$phone_field = $_POST['phone'];
$body = "From: $name_field\n E-Mail: $phone_field\n";
echo "Data has been submitted to $to!";
mail($to, $subject, $body);
} else {
echo "Error!";
}
?>

It will work, but your recipient is going to get spammed heavily after it has been out for a while. You can cut back on that a lot by putting another field in the form that is hidden with CSS and then checking that it is still empty before sending your email. As for adjusting the return address, use the forth parameter to the PHP mail function. It will look something like this:
mail($to, $subject, $body, "From:$fromAddress\n");
Here is some quick and dirty code that I have used for a similar form for a website to relay a message to someone's smartphone in a way that makes it easy to give a call back while on the go:
http://rietta.com/stackoverflow/sample-form.txt

Related

HTML PHP email submit form

I'm trying to create a PHP form that allows users to insert their email address and it automatically sends me an email with their email address. something like a subscription.
what I have so far is this:
<form action="" method="post">
<input type="email" name="email" placeholder="Enter your email address" /><br>
</form>
I found this PHP sample that I believe answers my problem, but I have no idea how to call it from my HTML.
<?php
if(isset($_POST['email'])){
$email = $_POST['email'];
$to = 'myemail#something.com';
$subject = 'new subscriber';
$body = '<html>
<body>
<p>Email:<br>'.$email.'</p>
</body>
</html>';
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset-utf-8";
$send = mail($to, $subject, $body, $headers);
if($send){
echo '<br>';
echo 'thanks';
}else{
echo 'error';
}
}
?>
There's insufficient code for me to be able to answer completely, but the one thing that comes immediately to my mind is not leaving action="" empty. Try $_SERVER['PHP_SELF'] variable, it should print the path to the file that is currently running so you'll be presented with the same page, but with data in $_POST you'll send. You can try it like this:
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<input type="email" name="email" placeholder="Enter your email address" /><br>
</form>
If you wish to send data to the same file like this, please make sure your PHP code is in the same file as the HTML structure of your form. It may make things easier if you put your PHP code first, so you can exit; from the file (not displaying the form anymore) telling the user that the message has been sent or that the error has occured.

PHP Form issues with action and echoing result

I am trying to get a simple two-field form to submit to an email address and then echo a "thanks for registering your interest" below the form (or instead of the form).
FYI, this is on a WordPress template file.
Here is the code, including the form:
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"
method="POST" autocomplete="on" id="register-form">
<input type="text" name="name" placeholder="Name"/>
<input type="email" name="email" placeholder="Email address"/>
<button type="submit" name="submit" class="button">Send
<img src="<?= get_image('icon-arrow-right-tiny.svg'); ?>"/></button>
</form>
<?php
if (isset($_POST['submit'])) {
// validate the email address first
$email = filter_input(INPUT_POST, 'email', FILTER_VALIDATE_EMAIL);
// process the form only if the email is valid
if ($email) {
$to = 'info#example.com'; // where you want to send the mail
$from = 'info#mydomain.com';
$subject = 'Website Submission';
$message = 'Name: ' . $_POST['name'] . "\r\n\r\n";
$message .= 'Email Address: ' . $_POST['email'] . "\r\n\r\n";
$headers = "From: $from\r\nReply-to: $email";
$sent = mail($to, $subject, $message, $headers);
} ?>
<p style='color: #fff; font-size: 14px;'>Thank you for registering your interest.</p>
<?php
}
?>
At the present time, the form does get sent, and the page does echo "Thank you for registering your interest" underneath the form, however it does not seem to be returning us to the correct page when you click the submit button.
Any ideas?
Thank you for all of your contributions. I have worked out the problem, and will share here for anybody else who comes here to find the answer.
WordPress has something important reserved for the "name" parameter, and thus you can't use it in PHP-based forms. Changing the parameter name from "name" to something else resolved the issue.
Additionally, WordPress also has the following names reserved and you cannot use them in forms - "day" "month" and "year".
I have check your code i think you have use color code #fff i.e. for the message.
Please try to make black or any other color rest of code are working.
:)
Thank you for registering your interest.
You have to put a php code below your Thank you message.
header("location:$_SERVER["PHP_SELF"]);exit;

Getting my php file to work with my html file

So I want my contact form to work on my site so I wrote some php to make it work. Here is the code: (form_process.php)
<?php
$name = $_POST('name');
$company = $_POST('company');
$email = $_POST('email');
$message = $_POST('message');
$to ="arp2222#yahoo.com";
$subject="New Message from Kincentive";
mail($to, $subject, $message, "From: ".$name);
echo "Your Message has been sent";
?>
I want to know how I can make this php work with my html file. I put the php file in the root folder with the index.html file and I believe I need to set up a form tag. I believe I need to use the action or method attribute? to setup as
for example.
I am using MAMP PRO as a local host since my site is not live yet and I want to test the contact form and recieve the test to my email.
Any help please i am new to php
in sendEmail.html you should write code as given
<form name="frmEmail" id="frmEmail" action="sendEmail.php" method="post">
<input type="text" name="fName" id="fName">
<input type="text" name="email" id="email">
<input type="text" name="company" id="company">
<textarea name="message" id="message"></textarea>
<input type="submit">
</form>
this form redirect to sendEmail.php
<?php
$name=$_POST['fName'];
$company=$_POST['company'];
$message=$_POST['message'];
$to =$_POST['email'];
$subject="New Message from Kincentive";
mail($to, $subject, $message, "From: ".$name);
echo "Your Message has been sent";
?>
$_POST is an array, so you should reference it like this, using [ brackets instead of curly ones.
$name = $_POST['name'];
$company = $_POST['company'];
$email = $_POST['email'];
$message = $_POST['message'];
In your HTML, wrap your inputs in a form like this, pointing to your Php file:
<form action="form_process.php" method="POST">
<-- input elements here !-->
</form>

PHP contact form returns successful, but doesn't send anything [duplicate]

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 5 years ago.
I'm working on a personal portfolio website (www.corybolles.com) and I'm attempting to create a contact from based off of PHP. I have very little knowledge of PHP compared to HTML and CSS, and I was wondering if anyone could help me figure out why this is not working correctly.
HTML
<div id="contact">
<form action="contact.php" method="post">
<label>Name</label><br>
<input class="forminput" type="text" name="cf_name" width="50px"><br>
<label>Email</label><br>
<input class="forminput" type="text" name="cf_email" width="50px"><br>
<label>Message</label><br>
<textarea class="forminput" name="cf_message" cols="18" rows="10"></textarea><br>
<input class="formbutton" type="submit" name="submit"value="Send">
<input class="formbutton" type="reset" name="clear" value="Clear">
</form>
</div>
PHP
<?php
$name = $_POST['cf_name'];
$email = $_POST['cf_email'];
$message = $_POST['cf_message'];
$from = 'From: www.corybolles.com';
$to = 'jcbollesjr#gmail.com';
$subject = 'Message from user via www.corybolles.com';
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
?>
<?php
if ($_POST['submit']) {
if (mail ($to, $subject, $body, $from)) {
echo '<p>Thanks! Your message has been sent!</p>';
} else {
echo '<p>Sorry, something appears to have broken. Please try again</p>';
}
}
?>
You can test it yourself, but whenever I fill out the form to test it, it returns a successful message, however doesn't actually send anything.
Try this in a seperate file to check if mail is actually 'working', as your current code looks fine.
<?php
echo mail('your#address.com', 'your#address.com', 'test');
?>
This script tries to send an email to your#address.com, from your#address.com, and echoes the return value of the function. If it doesn't echo 'TRUE' or '1', there is a weird problem. If it does, and you don't receive any mails in your mailbox, you should contact your server administrator.
It may be your server. If you don't own it, contact your system administrator. Possible causes could be: closed ports, packet filtering or security systems (such as SELinux) in your server or within the local network.
Look into PHPMailer which allows you to send mail via SMTP. This will mean that you have a store of all the emails that have been sent out, and will lower the risk of your mail being sent into user's spam folders!

php contact form email address

I have searched for this answer on Google and Youtube but have not found an answer.
I have created a very simple contact form on my website.
(http://www.torontoimc.com/contact)
I have attached a seperate PHP file to process the contact form elements.
When someone sends me an inquiry through the form on my site, I'm receiving all of the information except the person's email address input section info.
It's very important that I receive that email address otherwise I won't be able to reply to whom ever sent it to me.
I have tried setting the form to be sent to my gmail and outlook email but it just sends it as:
It just shows that the sender's email address as some random name "#hemi.websitewelcome.com"
I'm not sure if this is a server side issue or an issue with PHP file.
I have attached the code for both my Contact form and PHP file.
Here is the contact form code:
<form action="formprocess.php" method="post" name="contact">
<fieldset>
<label>First Name</label>
<input name="first_name" type="text"/>
<label>Last Name</label>
<input name="last_name" type="text"/>
<label>Email</label>
<input name="email_add" type="text"/>
<label>Please write your inquiry below.</label>
<textarea name="message" rows="15" cols="40">
</textarea>
<input type="submit" name="submit" id="submit" value="Submit"/>
<input type="reset" name="reset" id="reset" value="Reset"/>
</fieldset>
</form>
Here is the php code:
<?php
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$email_add = $_POST['email_add'];
$message = $_POST['message'];
$to = "torontoimc#gmail.com";
$subject = "Inquiry for TorontoIMC";
mail ($to, $subject, $message, "From: " . $first_name . $last_name . $email_add);
header('Location: thanks.html');
exit();
?>
I apologize if this is a repeat question but I'm a newbie to php and have not really been able to find an answer, if someone can please help me out, I would really appreciate it.
Thanks,
Latish Vohra
Try this:
$headers = "From: $first_name $last_name <$email_add>\r\n";
mail ($to, $subject, $message, $headers );
I added "\r\n" to From
I understand what you are trying to do on the FROM line, but you should use the "real" FROM sender. Many servers will not allow you to use a FROM line from a domain that is not the host's (think about it, the truth is you are lying, the person that filled the form did not actually send that email).
Secondly, follow user4035's way of styling the email in the FROM, if you are going to use a name last name <email>, it may not only be the way to do it for normal practice, also in the way that you wrote it, joining the fields without spaces, you might end up with a FROM line such as this FrançoisMARTIN MELLIERfmartinm#gmail.comwhich may contain illegal characters and/or spaces that may cause some servers to choke on or discard. Instead 'FROM: '. $first_name . ' '. $last_name. ' <'. $email_add. '>' will produce FROM: François Martin Mellier <fmartinm#gmail.com>. But again, you should use a real account from the same domain you are sending (maybe something like 'FROM: New enquiry <info#torontoimc.com>' ?). As it's been pointed out, include the email_add field in the body of the message or the subject line in order to receive it; those are the proper places.
Using the -f parameter like mti2935 pointed out is always a good idea, but only if that email coount belongs to the same domain as the server's.
As a side note, try to take errors into account, a minimum:
if (mail ($to, $subject, $message, $headers )) {
header('Location: thanks.html');
} else {
header('Location: error.html');
}
might go a long way.

Categories