This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I have a simple email form in a php file and i try to send emails but it seems that my variables come null. I have an echo after my code just to test if my variables have any values inside and they are not printed. The only thing that is printed is the 'done' and the 'email $to'. Do i do anything wrong? I followed this method from a post in youtube and he did the exact same thing and it worked for him. I also tried more email php files but still nothing.
Here is my html and php code.
php code:
<?php
$name = $_REQUEST['author'];
$from = $_REQUEST['email'];
$subj = $_REQUEST['sub'];
$message = $_REQUEST['msg'];
$headers .= "From: ".$from;
$to = "mygmail#gmail.com";
$subject = $subj;
$body = $message;
if(mail($to,$subject,$body,$headers)) {
echo "An e-mail was sent to ".$to." with the subject: ".$subject;
} else {
echo "There was a problem sending the mail. Check your code and make sure that the e-mail address ".$to." is valid";
}
?>
html code:
<form action="formtoemail.php" method="post" enctype="text/plain">
<p>
<label for="author">Name:</label>
<input type="text" id="author" name="author" class="required input_field" />
</p>
<p>
<label for="email">Email:</label>
<input type="text" id="email" name="email" class="validate-email required input_field" />
</p>
<p class="no_margin_right">
<label for="subject">Subject:</label>
<input type="text" name="sub" id="sub" class="input_field" />
</p>
<div class="cleaner h20"></div>
<label for="text">Message:</label>
<textarea id="msg" name="msg" rows="0" cols="0" class="required"></textarea>
<div class="cleaner h20"></div>
<input type="submit" value="Send" id="submit" name="submit" class="submit_btn float_l" />
<input type="reset" value="Reset" id="reset" name="reset" class="submit_btn float_r" />
</form>
You are sending via POST and retriving via GET try $_POST instead of $_GET in PHP
OR in HTML -
Chenge from action to GET instead of POST
In your HTML code change "POST" method to "GET"
<form action="formtoemail.php" method="get" enctype="text/plain">
<p>
<label for="author">Name:</label>
<input type="text" id="author" name="author" class="required input_field" />
</p>
<p>
<label for="email">Email:</label>
<input type="text" id="email" name="email" class="validate-email required input_field" />
</p>
<p class="no_margin_right">
<label for="subject">Subject:</label>
<input type="text" name="sub" id="sub" class="input_field" />
</p>
<div class="cleaner h20"></div>
<label for="text">Message:</label>
<textarea id="msg" name="msg" rows="0" cols="0" class="required"></textarea>
<div class="cleaner h20"></div>
<input type="submit" value="Send" id="submit" name="submit" class="submit_btn float_l" />
<input type="reset" value="Reset" id="reset" name="reset" class="submit_btn float_r" />
</form>
Use "get" method:
<form action="formtoemail.php" method="get" enctype="text/plain">
You need to change this:
$headers .= "From: $email";
To this:
$headers = "From: ".$from;
And also change this:
$subject = "$subj";
$body = "$message";
mail($to, $subject, $body, $headers);
if(mail($to, $subject, $body, $headers)) {
echo "An e-mail was sent to $to with the subject: $subject";
} else {
echo "There was a problem sending the mail. Check your code and make sure that the e-mail address $to is valid";
}
To this:
$subject = $subj;
$body = $message;
if(mail($to, $subject, $body, $headers)) {
echo "An e-mail was sent to ".$to." with the subject: ".$subject;
} else {
echo "There was a problem sending the mail. Check your code and make sure that the e-mail address ".$to." is valid";
}
Related
This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 4 years ago.
I am fairly new to coding and am having an issue with my website contact form. It won't allow me to receive email when the form is filled and submit button clicked.
I know nothing about PHP and it's obvious something is missing or incorrect. I've included only the relevant portions of the file...thanks for any help I can get!
<div id="page_4">
<div id="wrapper">
<form action="contact form.php" method="post">
<label for="fname">Name</label>
<input type="text" name="name" placeholder="Full name...">
<label for="email">Email</label>
<input type="text" name="email" placeholder="...">
<label for="phone">Phone</label>
<input type="text" name="phone" placeholder="...">
<label for="subject">Subject</label>
<input type="text" name="subject" placeholder="...">
<label for="detail">Project Detail</label>
<textarea name="detail" placeholder="Please use detail if possible..." style="height:200px"></textarea>
<label for="deadline">Project Deadline (MO/DY/YR)</label>
<input type="text" name="deadline" placeholder="...">
<input type="submit" value="Submit">
</form>
</div>
</div>
<?php
if (isset($_POST['submit'])) {
$name = $_POST['name'];
$subject = $_POST['subject'];
$mailfrom = $_POST['mail'];
$message = $_POST['message'];
$mailTo = "mark#markabove.com";
$headers = "From: ".$mailFrom;
$txt = "You have recieved an e-mail from ".$name."\n\n".$message;
mail($mailTo, $subject, $txt, $headers);
header("Location: contact.html?mailsend");
}
?>
The URL for the form action tag has a space in it. Perhaps change it to contact-form.php.
I want to send an email via html form with the help of php. However, it keeps
throwing same error every time claiming it couldn't find php file. I've tried
everything:
Installing NodeJs and running the code in localhost.
changing name="name" name="email" to something else.
Linking to another php script in case
something wrong with it.
Reading every SO page on this issue and trying every solution from there.
No luck at all.
Still POST /mail.php Error (404): "Not found". What am I missing here?
Can you make it work on your system?
HTML:
<form name="contactform" method="POST" action="mail.php">
<div>
<input type="text" name="user_name" placeholder="name" required />
<input type="email" name="user_email" placeholder="email" required />
<textarea type="text" name="comments" placeholder="message" required></textarea>
</div>
<button type="submit">SEND</button>
</form>
PHP:
<?php
$name = $_POST['user_name'];
$email = $_POST['user_email'];
$message = $_POST['comments'];
$formcontent=" From: $name \n Message: $message";
$recipient = "********#gmail.com";
$subject = "Contact Form";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "Thank You!";
?>
Use this :-
<form name="contactform" method="POST" action="mail.php">
<input type="text" name="user_name" id="user_name" placeholder="name" required />
<input type="email" name="user_email" id="user_email" placeholder="email" required />
<textarea type="text" name="comments" id="comments" placeholder="message" required></textarea>
<input type="submit" id="submit" value="SEND" />
</form>
This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 5 years ago.
CSS y PHP of my contact-form.
The page works perfectly but I'm trying to send an email and the button of send doesn't work. In the image you can see that when I try to send an email the button stays white. At the same time the button Clear works perfect! I attach my code
CONTACT.HTML
<form action="mail.php" method="POST" class="contact-form">
<input type="text" name="name" placeholder="Name" class="required">
<input type="email" name="email" placeholder="Email address" class="contact-form-email required">
<input type="text" name="subject" placeholder="Subject" class="contact-form-subject">
<textarea name="message" placeholder="Message" class="required" rows="7"></textarea>
<div class="response-message"></div>
<button class="border-button" type="reset" id="reset" name="reset">Limpiar</button>
<button class="border-button" type="submit" id="submit" name="submit">Enviar</button>
MAIL.PHP
<?php $name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$formcontent="From: $name \n Message: $message";
$recipient = "sa*********#*******.com";
$subject = "Contact Form";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "Thank You!";
?>
mail function doesn't work on local server and also you need to set SMTP configuration in PHP to send mail.
Are you placing your form inside the form tag?
<form action="mail.php" method="post">
<input type="text" name="name" placeholder="Name" class="required">
<input type="email" name="email" placeholder="Email address" class="contact-form-email required">
<input type="text" name="subject" placeholder="Subject" class="contact-form-subject">
<textarea name="message" placeholder="Message" class="required" rows="7"></textarea>
<div class="response-message"></div>
<button class="border-button" type="reset" id="reset" name="reset">Limpiar</button>
<button class="border-button" type="submit" id="submit" name="submit">Enviar</button>
</form>
I've got a simple email form in PHP that is to send a form input to the email address. However, the textarea isn't included. The phone number is being sent, just the textarea is not. Here's my form:
<form id="form1" name="form1" method="post" action="send.php">
<p> <span id="sprytextfield1">Your name:
<label for="name"></label>
<input type="text" name="name" id="name" /></p>
<span class="textfieldRequiredMsg">A value is required.</span></span>
<p><span id="sprytextfield2">
<label for="email">Email address:</label>
<input type="text" name="email" id="email" />
<span class="textfieldRequiredMsg">A value is required.</span></span></p>
<p>
<label for="phone">Phone Number:</label>
<input type="text" name="phone" id="phone" />
</p>
<p><span id="sprytextarea1">
<label for="message">Message</label>
<br />
<textarea name="message" id="message" cols="45" rows="5"></textarea>
<span class="textareaRequiredMsg">A value is required.</span></span></p>
<p>
<input type="submit" name="Submit" id="Submit" value="Submit" />
<br />
</p>
</form>
After that, I have the validation for the form.
Here's the php
$name=$_POST[name];
$email=$_POST[email];
$phone=$_POST[phone];
$message=$_POST[message];
$from="From: $name<$email>\r\nReturn-path: $email";
$subject="Message sent using your contact form";
mail("my#email.com", $subject, $from, $phone, $message);
echo "Email sent!";
change your
$name=$_POST[name];
$email=$_POST[email];
$phone=$_POST[phone];
$message=$_POST[message];
to
$name=$_POST['name'];
$email=$_POST['email'];
$phone=$_POST['phone'];
$message=$_POST['message'];
your message will work with this way
Hmmm, you aren't using parameters in the correct order. As per mail documentation (http://php.net/manual/en/function.mail.php), it should be:
mail("my#email.com", $subject, $message, $from);
I am not sure where you want to use $phone but may be you can concatenate it to $message before sending it.
This worked, for some reason. Using $subject, $message, $from would only send the message. I got rid of the phone number.
$name=$_POST['name'];
$email=$_POST['email'];
$message=$_POST['message'];
$from="From: $name<$email>\r\nReturn-path: $email";
$subject="Someone from your website is contacting you";
mail("me#email.com", $subject, $from, $message); //weird, but it works
Hi guys i'm having a issue i hope you guys can help with, i'm typing in all the fields and then upon pressing submit i'm getting just "Error!" on my screen.
Please see the code:
HTML
<h2 class="formhead">Contact Form</h2>
<br>
<form class="form" action="mail.php" method="POST">
<p class="name">
<input type="text" name="name" id="name" placeholder="John Doe" />
<label for="name">Name</label>
</p>
<br>
<p class="email">
<input type="text" name="email" id="email" placeholder="mail#example.com" />
<label for="email">Email</label>
</p>
<br>
<p class="number">
<input type="text" name="number" id="number" placeholder="0774XXXXXXX" />
<label for="name">Contact Number</label>
</p>
<br>
<p class="web">
<input type="text" name="web" id="web" placeholder="www.example.co.uk" />
<label for="name">Website</label>
</p>
<br>
<p class="message">
<textarea name="message" id="message" placeholder="Write something to us" /> </textarea>
</p>
<br>
<p class="submit">
<input type="submit" value="Send"/>
</p>
</form>
PHP
<?php $name = $_POST['name'];
$email = $_POST['email'];
$number = $_POST['number'];
$message = $_POST['message'];
$website = $_POST['web'];
$formcontent="From: $name \n Contact: $number \n Website: $web \n Message: $message";
$recipient = "enquiries#c(hidden)y.co.uk";
$subject = "Contact Form";
$mailheader = "From: $email ";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "Thank You!" . " -" . "<a href='contact.html' style='text-decoration:none;color:#ff0099;'> Return Home</a>";
?>
Any help would be much appreciated!
Thanks
Sam
Your script always reporting 'Error!' because the mail() function always fails. That's because some index you're using in the php file doesn't match to the input names in your form:
Change these:
$website = $_POST['website'];
to:
$website = $_POST['web'];
Or change it in your form.
Also you have to specify a name for the message textarea:
<textarea name="message" id="message" placeholder="Write something to us" />
This may fail again if it can't connect to mailserver. This is probably you're case if The SMTP is Disabled.
As per my comment, here's an example of a better die statement:
<?
$your_function or die("Error! a") // Just replace the letter a with anything. It serves as a simple link to your function that only you know. so you can go back and check it