Trouble with a Simple PHP Contact Form [duplicate] - php

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 8 years ago.
I'm having trouble figuring out why my contact form isn't working. I'm relatively new to PHP and from what I can see, everything is linked, but when I go to send the email, nothing is sent to the designated delivery address. Any help would be appreciated!! Thank you!!
My code is below:
HTML
<table width="400" border="0" align="left" cellpadding="0" cellspacing="1">
<tr>
<td><form name="form1" method="post" action="mail.php">
<table width="100%" border="0" cellspacing="1" cellpadding="3">
<tr>
<td width="16%">Subject</td>
<td width="2%">:</td>
<td width="82%"><input name="subject" type="text" id="subject" size="50"></td>
</tr>
<tr>
<td>Message</td>
<td>:</td>
<td><textarea name="detail" cols="50" rows="4" id="detail"></textarea></td>
</tr>
<tr>
<td>Name</td>
<td>:</td>
<td><input name="name" type="text" id="name" size="50"></td>
</tr>
<tr>
<td>Email</td>
<td>:</td>
<td><input name="customer_mail" type="text" id="customer_mail" size="50"></td>
</tr>
<tr>
<td> </td>
<td> </td>
<td><input type="submit" name="Submit" value="Submit"> <input type="reset" name="Submit2" value="Reset"></td>
</tr>
</table>
</form>
</td>
</tr>
</table>
PHP
<?php
// Contact subject
$subject ="$subject";
// Details
$message="$detail";
// Mail of sender
$mail_from="$customer_mail";
// From
$header="from: $name <$mail_from>";
// Enter your email address
$to ='jordanmcowan#gmail.com';
$submit=mail($to,$subject,$message,$header);
// Check, if message sent to your email
// display message "We've recived your information"
if($submit){
echo "Thank you for contacting us at Allstar Therapies, Inc.<br /> We will be in touch shortly!";
}
else {
echo "ERROR";
}
?>

Your PHP Code seems wrong to me. This is a corrected version
<?php
// Contact subject
$subject = $_POST['subject'];
// Details
$message = $_POST['detail'];
// Mail of sender
$mail_from = $_POST['customer_mail'];
// From
$header = "from: " . $_POST['name'] . "<" . $_POST['customer_mail'] . ">";
// Enter your email address
$to = 'jordanmcowan#gmail.com';
$submit = mail($to, $subject, $message, $header);
// Check, if message sent to your email
// display message "We've recived your information"
if ($submit) {
echo "Thank you for contacting us at Allstar Therapies, Inc.<br /> We will be in touch shortly!";
} else {
echo "An error has been encountered while sending your message. We sincerely apologize and ask you to try again. If that fails as well, please contact us at XYZ-Y234-SADF";
}
The variables haven't been initialized. Please note that these symbols: "" mark the beginning and the end of a string.

These lines are wrong:
// Contact subject
$subject ="$subject";
// Details
$message="$detail";
// Mail of sender
$mail_from="$customer_mail";
// From
$header="from: $name <$mail_from>";
You're trying to assign the variables to themselves. Try:
$subject = $_POST['subject'];
$message = $_POST['detail'];
etc.

You need to use $_POST variable:
// Contact subject
$subject = isset($_POST["subject"]) ? ($_POST["subject"]) : "";

You haven't set your PHP variables (IE $subject) to your post variables yet. Try changing your PHP variables to use the $_POST variables.
<?php
// Contact subject
$subject = $_POST['subject'];
// Details
$message= $_POST['detail'];
// Mail of sender
$mail_from= $_POST['customer_mail'];
// From
$header="from: {$_POST['name']} <{$_POST['mail_from']}>";
// Enter your email address
$to ='jordanmcowan#gmail.com';
$submit=mail($to,$subject,$message,$header);
// Check, if message sent to your email
// display message "We've recived your information"
if($submit){
echo "Thank you for contacting us at Allstar Therapies, Inc.<br /> We will be in touch shortly!";
}
else {
echo "ERROR";
}
?>

You can access to POST parametars by $_POST / GET parametars by $_GET. First check if your redirect works correct.
type:
echo "welcome";
in mail.php
If redirect works the print all Post parametars:
print_r($_POST);
Last you can get one by one
$variable = $_POST['input_name_from_form'];
Hope this is solution for your problem

Related

Mail sending is failure in php contact us form [duplicate]

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 6 years ago.
I am creating a contact us form, In that i am using HTML and PHP .Now my doubt is after click the submit button the details are not sending to mail,I want to know where i am mistaken on my code.
Here my HTML code for create the form
<form action="sendmail.php" method="post" >
<table width="100%" border="0"align="center"cellpadding="3"cellspacing="1">
<tr>
<td width="10%">Subject</td>
<td width="2%">: </td>
<td width="82%"><input name="name" type="text" id="name" size="50"></td>
</tr>
<tr>
<td>Detail</td>
<td width="2%">: </td>
<td width="82%"><textarea name="detail" cols="50" rows="4"id="detail"></textarea></td>
</tr>
<tr>
<td>Email</td>
<td>: </td>
<td><input name="customer_mail" type="text" id="customer_mail" size="50"></td>
</tr>
<tr>
<td>Name</td>
<td>: </td>
<td><input name="name" type="text" id="name" size="50"></td>
</tr>
<tr>
<td><input type="submit" name="send" value="Submit">
<input type="reset" name="submit" value="Reset">
</td>
</tr>
</table>
</form>
PHP code is below:
<?php
$subject="$subject";
$message="$detail";
$mail_from="customer_mail";
//From
$header="from: $name <$mail_from>";
//Enter your email address
$to="simon#abcinfomedia.in";
$sendmail=mail($to,$subject,$message,$header);
//check mail send to ur mail
if($sendmail){
echo"success";
}
else{
echo"Error";
}
?>
please try this:
<?php
if(isset($_POST['send']) && !empty($_POST['send'])) {
$subject = $_POST['subject'];//if you sent by post method else put manually
$message = $_POST['detail'];
$mail_from = $_POST['customer_mail'];
$header = "from: $name <$mail_from>";
//Enter your email address
$to = "simon#abcinfomedia.in";
$sendmail = mail($to, $subject, $message, $header);
//check mail send to ur mail
if ($sendmail) {
echo "success";
} else {
echo "Error";
}
}
?>
This is not working because you're not filling the variables with the data from the form by using $_POST[''].
The HTML doesn't change, but try this PHP:
<?php
$subject = $_POST['subject'];
$message = $_POST['detail'];
$mail_from = $_POST['customer_mail'];
$header = "from: $name <$mail_from>";
$to = "simon#abcinfomedia.in";
//check mail send to ur mail
if(mail($to,$subject,$message,$header)){
echo"success";
}
else{
echo"Error";
}
?>
I have defined subject, message and mail_from using the $_POST data from the form. For the if statement, I have dropped the part where you store the mail() function as a variable and just put it straight into the if.
This will simultaneously send the email and check it at the same time.
In my test, the email worked but there were flaws because some of your fields are missing and not defined, but I believe the above solves your initial problem. :)

PHP email formatted content

I am creating an email form for my website which will allow a user to create an email and appear to be sent from any email address they wish. i.e. davidcameron#downingstreet.com.
The form works fine, until I made one small change. I previously allowed the user to submit the content of the email but was finding difficulty in formatting the content (i.e. making areas bold, using a table etc).
(Form code now)
<html>
<body>
<form method="GET" action="send.php">
<p>To: <input type="text" name="to" /></p>
<p>From-Name: <input type="text" name="name" /></p>
<p>From-Email: <input type="text" name="from" /></p>
<p>Subject: <input type="text" name="subject" /></p>
<input type="submit" value="Send E-Mail" ></p>
</form>
</body>
</html>
So I thought it would be easier to submit the content myself in my 'send.php' code that actually sends the email message. This is displayed below:
<html>
<body>
<?php
$to =$_REQUEST['to'];
$subject = $_REQUEST['subject'];
$name =$_REQUEST['name'];
$from = $_REQUEST['from'];
$content = ?><font face="arial"><b>Your question has been received/b>
Your booking has been confirmed with the supplier.
Please visit the Question Portal for more information.
<table>
<tr>
<td>Question subject</td>
<td> Room</td>
</tr>
<tr>
<td>Traveller</td>
<td>Maria Smith</td>
</tr>
<td>Requester</td>
<td>Tony Smith</td>
</tr>
</table>
<br/>
</font>
<?
$header="From: $from"."<$sender_email>\r\n";
mail($to,$subject,$content,$header);
echo 'sent successfully';
?>
</body>
</html>
However, when I click submit, my server finds an error. I can only presume it is because of my HTML element as this error was not occurring previously without it.
Can anyone advise how I can fix this / if you can suggest an easier way for me to format the email content I would like?
Many thanks
Set your content value to
$content = ''
Since your $content is undefined that is why you are getting error.
Hope this will fix your issue.
Try this
<?php
$to = $_REQUEST['to'];
$subject = $_REQUEST['subject'];
$name = $_REQUEST['name'];
$from = $_REQUEST['from'];
$content =
'<span style="font-family: arial"><b>Your question has been received</b>
<br>
Your booking has been confirmed with the supplier.
Please visit the Question Portal for more information.
<table>
<tr>
<td>Question subject</td>
<td> Room</td>
</tr>
<tr>
<td>Traveller</td>
<td>Maria Smith</td>
</tr>
<td>Requester</td>
<td>Tony Smith</td>
</tr>
</table>
<br/>
</span>';
$header = "From:".$from;
$result = mail($to,$subject,$content,$header);
if(isset($result))
{
echo 'sent successfully';
}
else
{
echo 'Failed';
}
?>
Note: <font> tag not supported in HTML5
EDIT 01
Include this lines too
$header .= 'MIME-Version: 1.0';
$header .= 'Content-Type: text/html; charset="ISO-8859-1';

How to send response email to user on form submit PHP

I have a rather basic php form that submits on the same page it is called "registration.php". I have it set to collect the form data, store it in the $to, $subject and $body variables then I call the mail() method to send off an email to myself.
please see code below ( Don't worry! there is a question comin' right up! ):
<?php
ob_start(); //output buffering because I like it.
if (isset($_POST['submit'])) {
// Process the form
$message = "Thank you for registering! We will respond to your request shortly";
$name = $_POST['name'];
$email = $_POST['email'];
$address = $_POST['address'];
$comments = $_POST['userComment'];
$date = gmdate("M d Y");
$to = "myemail#outlook.com";
$subject = "Registration Submission";
$body = " Date: $date \n Registrant Name/Name's: $name \n Registrant E-mail: $email \n \n User Comments: \n $comments \n \n";
mail($to,$subject,$body);
}
?>
<form id="contactForm" name="contactForm" action="registration.php" method="post">
<table>
<tr>
<td><label for="name"><strong>Registrant Name / Names</strong></label></td>
<td><input required='required' type="text" id="name" name="name" /></td>
</tr>
<tr>
<td><label for="email"><strong>Registrant E-mail</strong></label></td>
<td><input required='required' type="email" maxlength="40" id="email" name="email" /></td>
</tr>
<tr>
<td><label for="subject"><strong>Registrant Address</strong></label></td>
<td><input required='required' type="text" maxlength="100" id="address" name="address" /></td>
</tr>
<tr>
<td>Message:</td>
<td> </td>
</tr>
<tr>
<td id="textAreaInput" colspan="2"><textarea rows="3" id="userComment" name="userComment" placeholder="Let us know your thoughts!"></textarea></td>
</tr>
</table>
<br />
<button type="submit" name="submit" id="submit">Submit</button>
</form>
What I want to know is Can I send a custom response email to the users collected $email with a custom message using just php? can I send other variables through the mail() method? like $to2, $subject2, $body2 or must they be named $to, $subject & $body when passed?
I have a working solution using PHPMailer found here:
https://github.com/PHPMailer/PHPMailer
I am just curious as to weither or not there is an easy process using PHP that wouldn't rely on a required library.
I think all you have to do is
if(mail($to,$subject,$body)){
mail($email,"Thanks!!","Thank you very much for registering!!!")
}

sending email upon hitting submit in html or php

I have a simple site with 3 input forms: a first name, last name, and email address fields for the user to fill out. I want them to be able to fill those 3, and when hitting submit, an email to be generated with some autofilled text, for example:
email: what the user put as their email
the subject will be something I make, like, "regarding your _"
and the body of the email to use their first and last name they entered like so:
Dear __ __,
click here to fill out this bla bla bla etc
How would I go about doing this?
any helpful links or tutorials would be fantastic
you can make a simple html and process it by php. You can do like this :
index.php:
<form action='sendmail.php' method='post'>
<table>
<tr>
<td>Firstname</td>
<td><input name="firstName" /></td>
</tr>
<tr>
<td>LastName</td>
<td><input name="firstName" /></td>
</tr>
<tr>
<td>Lastname</td>
<td><input name="lastName" /></td>
</tr>
<tr>
<td>Email</td>
<td><input name="email" /></td>
</tr>
<tr>
<td colspan="2"><input type='submit' value='send' /></td>
</tr>
</table>
</form>
sendmail.php:
$firstName = $_POST['firstName'];
$lastName = $_POST['lastName'];
$email = $_POST['email'];
//build email content
$emailSubject = "Email subject";
$mailContent = "Regarding ".$firstName." ".$lastName;
$mailContent .= "more content here";
//send mail
if(mail($email,$emailSubject,$mailContent))
echo "mail send successful";
else
echo "mail send fail";
NOTICE: I suppose that all the input param is valid for send mail. You should add more validation for input field.
I would not use the basic mail function within PHP as it does not protect you against header inject (open relay etc).
Take a look at one of these:
- http://framework.zend.com/manual/2.0/en/modules/zend.mail.message.html
- http://symfony.com/doc/2.0/cookbook/email/email.html
NB: They can be used outside of the MVC framework, i.e just as a mail library
This short code for this...i hope its clear to you and will help to you, there is a lot of tutorials ......just try to search in google
foreach($_POST as $field_name=>$field_val)
{
$message = $message. $field_name . " : $field_val \n";
}
//email code
$headers = "From: Feedback <".$email.">";
mail($email_to, $subject, $message, $headers);

Send Contact Form not Working

I've followed a simple tutorial for a send contact form, however it appears to be not working. Please can someone assist please.
Form below:
<table width="400" border="0" cellspacing="1" cellpadding="0" align="center">
<tbody>
<tr>
<td>
<form action="send.php" method="post" name="form1">
<table width="100%" border="0" cellspacing="1" cellpadding="3">
<tbody>
<tr>
<td width="16%">Name</td>
<td width="2%">:</td>
<td width="82%"><input id="Name" type="text" name="Name" size="50" /></td>
</tr>
<tr>
<td>Email</td>
<td>:</td>
<td><input id="customer_mail" type="text" name="customer_mail" size="50" /></td>
</tr>
<tr>
<td>Subject</td>
<td>:</td>
<td><input id="Subject" type="text" name="Subject" size="50" /></td>
</tr>
<tr>
<td>Detail</td>
<td>:</td>
<td><textarea id="detail" cols="50" name="detail" rows="4"></textarea></td>
</tr>
<tr>
<td></td>
<td></td>
<td><input type="submit" name="Submit" value="Submit" /> <input type="reset" name="Submit2" value="Reset" /></td>
</tr>
</tbody>
</table>
</form>
</td>
</tr>
</tbody>
</table>
And here is my PHP:
<?php
$to ='kirsty.harris1985#gmail.com';
$header="from: $name <$mail_from>";
$mail_from="$customer_mail";
$Subject="$Subject";
$detail="$detail";
$send_contact=mail($to,$header,$Subject,$detail);
if($send_contact){
echo "We've recived your contact information";
} else {
echo "ERROR";
}
?>
This is the error:
Server error
The website encountered an error while retrieving http://nqmedia.co.uk/send_contact.php. It may be down for maintenance or configured incorrectly.
Here are some suggestions:
Reload this webpage later.
HTTP Error 500 (Internal Server Error): An unexpected condition was encountered while the server was attempting to fulfill the request.
And also the website is www.nqmedia.co.uk for people to see it.
Given that your inputs are named accordingly
try this:
$name = $_POST['Name'];
$mail_from = $_POST['customer_mail'];
$header = "from: $name <$mail_from>";
$Subject = $_POST['Subject'];
$detail = $_POST['detail'];
You never actually grab all the post values.
Make this your code for send.php:
<?php
$name = $_POST['Name'];
$mail_from = $_POST['customer_mail'];
$subject = $_POST['Name'];
$body = $_POST['detail'];
$to ='kirsty.harris1985#gmail.com';
$header="from: $name <$mail_from>";
$send_contact=mail($to,$Subject,$body,$header);
if($send_contact){
echo "We've received your contact information";
}
else {
echo "ERROR";
}
?>
Also, you misspelling "received" on your echo, so I fixed the spelling mistake.
Change your PHP to the following:
<?php
$to ='kirsty.harris1985#gmail.com';
$name = $_POST['name'];
$customer_mail = $_POST['customer_mail'];
$Subject = $_POST['Subject'];
$detail = $_POST['detail'];
$header="From: $name <$customer_mail>";
$send_contact=mail($to,$Subject,$detail,$header);
if($send_contact){
echo "We've recived your contact information";
} else {
echo "ERROR";
}
?>
No variable was defined properly, you needed to use $_POST to retrive the values from the form.
$mail_from (now $customer_mail) was used before it was defined.
The parameters used in the in the mail() function were used in the wrong order. Look at http://php.net/manual/en/function.mail.php
Check the action attribute on your form. It says "send.php", but the error shows "send_contact.php"

Categories