I know there is a few of these posts already up, and I read through them, but was unable to find the solution to my problem. The PHP and HTML looks good so I am not exactly sure why I am not receiving any email from the submitted contact form.
Here is the PHP:
<?php
$Fname = $_POST ['Fname'];
$Lname = $_POST ['Lname'];
$email = $_POST ['email'];
$message = $_POST ['message'];
$to = 'myName#mywebsite.com';
$subject = 'Contact From My Website';
$msg = " First Name: $Fname/n" .
"Last Name: $Lname/n" .
"Email: $email/n" .
"Message: $message";
mail ($to, $subject, $msg,"From: " . $Fname . $Lname);
$confirm = "Thank you for contacting us! Please allow 48 hours for a representative to respond. Click <a href='contact.php'>here</a> to return to the previous page.";
?>
And here is the HTML form code:
<form id="form1" name="form1" method="post" action="send.php">
<tr>
<label><td width="160px" class="labels">First Name: </td>
<td class="input"><input type="text" name="Fname" id="Fname"/></td>
</label>
</tr>
<tr>
<label><td class="labels">Last Name: </td>
<td class="input"><input type="text" name="Lname" id="Lname"/></td>
</label>
</tr>
<tr>
<label><td class="labels">Email: </td>
<td class="input"><input type="email" name="email" id="email"/></td>
</label>
</tr>
<tr>
<label><td class="labels">Message: </td>
<td class="input"><textarea name="message" id="message" cols="30" rows="5"></textarea></td>
</label>
</tr>
<tr>
<td class="labels"><input type="submit" name="submit" id="submit" value="Submit"/></td>
</tr>
</form>
Any help would be greatly apreciated
EDIT:
There were many errors.
Improper use of \n, mail() headers and trying to echo a success message at the end.
Here is a tested and working mail handler script.
NOTE: I added an if{isset condition.
Change email#example.com with your own E-mail address.
<?php
if(isset($_POST['submit']))
$Fname = $_POST['Fname'];
$Lname = $_POST['Lname'];
$email = $_POST['email'];
$message = $_POST['message'];
$to = "email#example.com"; // <<< change this to your own E-mail address
$subject = "Contact From My Website";
$msg = "First Name: $Fname\n" . "Last Name: $Lname\n" . "Email: $email\n" . "Message: $message";
$headers = "From: $email" . "\r\n" .
"Reply-To: $email" . "\r\n" .
"X-Mailer: PHP/" . phpversion();
mail($to, $subject, $msg, $headers);
echo "Thank you for contacting us! Please allow 48 hours for a representative to respond. Click <a href='contact.php'>here</a> to return to the previous page.";
?>
Supplement
Please read up on the mail( ) function and its proper use of headers by visiting:
http://php.net/manual/en/function.mail.php
There are plenty of examples on that page.
One major error is your use of /n and all need to be changed to \n if anything.
That alone will ultimately make your handler fail.
"From" should not include the name, instead it should include the from email address.
So, instead of
"From: " . $Fname . $Lname
You should be doing
"From: ". $myEmail
Here's a valid header from the php manual:
$headers = 'From: webmaster#example.com' . "\r\n" .
'Reply-To: webmaster#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
I guess Puggan and Fred get credit for this answer as-well, since they have posted out the obvious but not yet made it an answer.
You should also make sure that the id of the message labels match to $_POST['msg'], so you should not be using 'msg' in one place and 'message' in another.
FYI: skip the spaces ' ' between the function name and the parameter arguments. i.e.
$_POST ['stuff'];
Add
$headers = "From:" . $Fname . $Lname;
and use this:
mail ($to, $subject, $msg, $headers);
Related
This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 3 years ago.
I have an HTML form that posts to a PHP mail function. For some reason it does not send the content anymore. The emails display "This email has no content." Am I missing something?
//HTML - Post method form.
<form class="form" id="form" action="submitform.php" method="post">
<h2 style="padding-top: 10px;">Contact Us</h2>
<input class="input" type="text" placeholder="Name" name="name"><br>
<input class="input" type="text" placeholder="E-Mail Address" name="email"><br>
<input class="input" type="text" placeholder="Phone #" name="phone"><br>
<textarea class="input" placeholder="Questions, Specifications, etc."
name="message</textarea>
<input class="inputButton" type="submit">
</form>
//PHP - Gets posted HTML input data and sends it to the email, formatted with \n
<?php
$to = 'example#example.com' ;
$subject = 'Inquiry' ;
$name = $_POST['name'] ;
$email = $_POST['email'] ;
$phone = $_POST['phone'] ;
$message = $_POST['message'] ;
$message = "From: $name \nEmail: $email \nPhone: $phone \nMessage: $message \n";
$sent = mail ($to, $subject, $message) ;
if($sent == true) {
echo "<h3>Thank you for your inquiry.</h3>";
}else{
echo "<h3>Sorry, your message wasn't sent.</h3>;
}
?>
I've found defining headers, even if I just want default values, helps me get my mail delivered. I can't say this is what you need to do because your code looks like it should run successfully as is.
An example of setting the headers to (I believe) default values:
$to = $to_email;
$subject = $your_subject;
$message = $your_message;
$headers = "From: S.O.<no-reply#dontusethis.email>\r\n" .
"Reply-To: [can be nice, not needed of course]\r\n" .
"X-Mailer: PHP/" . phpversion();
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
mail($to,$subject,$message,$headers);
This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 5 years ago.
I want to get customers details through contact form and that would be sent to my mail id. so I am trying to code but its not working. I have tried by watching YouTube videos
This is my HTML Code
<!DOCTYPE html>
<html>
<head>
<title>Form</title>
</head>
<body>
<form action="send.php" method="POST">
Name: <input type="text" name="name" required=""></input><br><br>
Mobile: <input type="text" required="" name="mobile"></input><br><br>
Message: <textarea rows="10" cols="50" name="mes" required=""></textarea><br><br>
<input type="submit" value="Send Mail"></input>
</form>
</body>
</html>
This is my PHP Code
<?php
$name =$_POST['name'];
$mobile =$_POST['mobile'];
$mes =$_POST['mes'];
mail("imjeevajk#gmail.com","Contact From Site",$mes,"From: $name\r\n","Mobile: $mobile\r\n");
echo "Thanks for Contacting Us";
?>
mail function as described here http://php.net/manual/en/function.mail.php
Example:
<?php
$to = 'nobody#example.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster#example.com' . "\r\n" .
'Reply-To: webmaster#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
?>
So the header "From" must be an email address while you are putting a custom name.
How to get mail function delivery mail to 'to' address please read here
It is mostly correct.
You can't pass Mobile as the additional parameter, that will do nothing.
I have changed the script to append the mobile to the message body.
To set a from address, you need to have a from email, just a name by itself won't work.
Also depending on your php configuration and mail server configuration, you may not be able to change the from address and it will result in an error, or be ignored.
<?php
if ($_POST) {
$name = $_POST['name'];
$mobile = $_POST['mobile'];
$mes = $_POST['mes'];
// append mobile to message
$mes .= "\r\nMobile: $mobile\r\n";
$from_email = "imjeevajk#gmail.com';
mail("imjeevajk#gmail.com", "Contact From Site", $mes, "From: $name <$from_email>\r\n");
// mail("imjeevajk#gmail.com", "Contact From Site", $mes, "From: $name\r\n", "Mobile: $mobile\r\n");
echo "Thanks for Contacting Us";
exit;
}
Please set submit name field and update the php mail function as mentioned below.
<input type="submit" name="sendMail" value="Send Mail"></input>
<?php
if (isset($_POST['sendMail'])) {
$to = 'admin#dummyemail.com';
$subject = 'Contact From Site';
$from = "From: ".$_POST["name"]."\r\n";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: '.$from."\r\n".
'Reply-To: '.$from."\r\n" .
'X-Mailer: PHP/' . phpversion();
$message = '<html><body>';
$message = "Name: ".$_POST["name"]."";
$message .= "Mobile: ".$_POST["mobile"]."";
$message .= "Message: ".nl2br($_POST["mes"])."";
$message .= '</body></html>';
if(mail($to, $subject, $message, $headers)){
echo 'Thanks for Contacting Us.';
} else{
echo 'Unable to send email. Please try again.';
}
}
?>
Better use PHP Mailer
some servers providers block mail function and that can be reason why it doesn't work.
Im trying to create a contact form using php,and having some troubles.
the condition: isset($_POST['submit']) always produces false, even if I just submit a blank page.
here is my code:
contact.html part:
<form action='emailto.php' method='POST' enctype='text/plain'>
First Name:<br>
<input type='text' name='firstname'><br>
Last Name:<br>
<input type='text' name='lastname'><br>
Email Address:<br>
<input type='text' name='emailadd' ><br>
Subject:<br>
<input type='text' name='subject' ><br>
Message:<br>
<textarea name='message' rows=5 cols=30 ></textarea><br><br>
<input type='submit' value='Send' name='submit'>
</form>
emailto.php:
<?php
if (isset($_POST['submit'])) {
$to = "emailaddress";
$subject = $_POST['subject'];
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$Emailadd = $_POST['emailadd'];
$Message = $_POST['message'];
$Body= "";
$Body .= $firstname;
$Body .= $lastname;
$Body .= "\n";
$Body .= $Emailadd;
$Body .= "\n";
$Body .= $Message;
mail($to,$subject,$Body);
echo "Mail Sent! <a href='contact.html'>Go Back</a>";
} else {
header("Location: contact.html");
exit(0);
}
?>
Besides, what's weird is that if I delete the if-else statement in emailto.php,
after submitting, an error message will occur: undifined index: subject, firstname, lastname, emailadd, message...
I was totally confused..
Looking forward to hear some advice.
Thanks in advance.
Remove this enctype='text/plain' from your form and it will start working; I guarantee it. And if it fails, then you need to find out why that is and making sure that mail is available for you to use.
Sidenote: You should also check if any of the inputs are empty (and required which helps).
http://php.net/manual/en/function.empty.php
and use proper (full) headers for mail():
http://php.net/manual/en/function.mail.php
Otherwise, your mailout may be treated as spam or rejected altogether.
There should be a valid From: <email> as part of mail's 4th argument.
I.e. and from the manual:
<?php
$to = 'nobody#example.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster#example.com' . "\r\n" .
'Reply-To: webmaster#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
?>
"Besides, what's weird is that if I delete the if-else statement in emailto.php, after submitting, an error message will occur: undifined index: subject, firstname, lastname, emailadd, message..."
Again; this is caused by enctype='text/plain' in the form which isn't a valid enctype for using the POST array.
Edit:
Add an if/else to mail(). If it echos "Houston we have a problem", then there's a problem on your end.
If it echo "Mail Sent!" but no mail is received, then look at your spam box. Mail has done its job and you need to find out why it was never sent/received.
<?php
if (isset($_POST['submit'])) {
$to = "emailaddress";
$subject = $_POST['subject'];
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$Emailadd = $_POST['emailadd'];
$Message = $_POST['message'];
$Body= "";
$Body .= $firstname;
$Body .= $lastname;
$Body .= "\n";
$Body .= $Emailadd;
$Body .= "\n";
$Body .= $Message;
if(mail($to,$subject,$Body)){
echo "Mail Sent! <a href='contact.html'>Go Back</a>";
} else { echo "Houston, we have a problem"; }
} else {
header("Location: contact.html");
exit(0);
}
?>
I'm new to this site so i'll try and explain myself as clearly as possible!
I'm currently in the process of creating a web form which when submitted sends an email to an account I have made on my server.
All is working fine but was wondering if there was a way i could add multiple accounts for the submitted form to send too.
My HTML:
<table style="width:100%">
<form action="form_complete.php" method="post">
<tr>
<th>
<input type="text" required placeholder="Name" name="name" />
</th>
<th>
<input type="text" required placeholder="Email" name="email" />
</th>
</tr>
<tr>
<th>
<input type="text" placeholder="Contact Number" name="mobile" />
</th>
<th>
<input type="text" required placeholder="Subject" name="subject" />
</th>
</tr>
<tr>
<td colspan="2"><textarea id="ta" name="message" required placeholder="Message"></textarea>
</tr>
</table>
<input id="submit" type="submit" name="submit" value="submit" />
</form>
form_complete.php
if(isset($_POST['submit'])){
$to = "rtozer#tandtcivils.co.uk"; // this is your Email address
$from = $_POST['email']; // this is the sender's Email address
$name = $_POST['name'];
$mobile = $_POST['mobile'];
$subject = $_POST['subject'];
$subject2 = "Copy of your form submission";
$message = "Name: " .$name . "\n\n mobile number: " . $mobile . ".\n\n Message:" . "\n\n" . $_POST['message'];
$message2 = "Here is a copy of your message " . $name . "\n\n" . $_POST['message'];
$headers = "From:" . $from;
$headers2 = "From:" . $to;
mail($to,$subject,$message,$headers);
mail($from,$subject2,$message2,$headers2); // sends a copy of the message to the sender
header ('Location: http://www.tandtcivils.co.uk/form_complete.php');
// You can also use header('Location: thank_you.php'); to redirect to another page.
}
The code here is working correctly when sending an email to rtozer#tandtcivils.co.uk but i want to be able to add; dtozer#tandtcivils.co.uk to recieve a copy of the form submission also.
If you need any further information from me please leave a comment!
Thanks in advance,
Sam
You can have multiple TO addresses. For example:
$to = "rtozer#tandtcivils.co.uk," . $_POST['email'];
Or use CC or BCC headers:
$headers = "From:" . $from;
$headers .= "CC:" . $_POST['email'];
Or:
$headers .= "CC:rtozer#tandtcivils.co.uk," . $_POST['email'];
There are a number of ways to organize your email recipients, depending on whether you want them to be direct recipients, carbon copy recipients, or blind carbon copy recipients. Lots of examples are available in the PHP documentation.
The key benefit here, as opposed to what you attempted in your code, is that you need only send the email once as opposed to sending the same email multiple times. Just arrange multiple recipients on that one email.
Thanks to Reza Mamun try this:
//......
//...Other setting goes here....
// Always set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
// More headers
$headers .= 'From: My Name <myemail#example.com>'. "\r\n";
//Multiple CC can be added, if we need (comma separated);
$headers .= 'Cc: myboss1#example.com, myboss2#example.com' . "\r\n";
//Multiple BCC, same as CC above;
$headers .= 'Bcc: myboss3#example.com, myboss4#example.com' . "\r\n";
mail($to, $subject, $message, $headers);
So I have an email form using PHP:
<form method="post" action="contactus.php" autocomplete="off">
<label for="Name">Name:</label>
<input type="text" name="Name" id="Name" maxlength="60" required/>
<label for="email">Email:</label>
<input type="text" name="email" id="email" maxlength="120" required/>
<label for="message">Message:</label><br />
<textarea name="message" rows="20" cols="20" id="message" required></textarea>
<input type="submit" name="submit" value="Submit" class="submit-button" />
</form>
And the PHP to send to my email:
<?php
if($_POST["submit"]) {
// The message
$message=$_POST["message"];
$email=$_POST["email"];
// In case any of our lines are larger than 70 characters, we should use wordwrap()
$message = wordwrap($message, 70, "\r\n");
// Send
mail('myemail.com', 'Quick Pass', $message, $email);
}
?>
But the problem is only 'myemail.com' (which is my email address), 'Quick Pass' (the email subject) and $message (the email message). $email isn't being sent.
I have tried:
<?php
$to = "myemail.com";
$subject = "Quick Pass";
$message=$_POST["message"];
$email=$_POST["email"];
mail($to,$subject,$message,$email);
?>
With this I am receiving four emails (two with $message and two with $email).
The problem is I need all of this in one email. So the $message is sent along with $email somewhere in the email.
Any ideas? (hopefully using my form)
There is an example, direct from the php manual, which looks as follows:-
$to = 'nobody#example.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster#example.com' . "\r\n" .
'Reply-To: webmaster#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
The fourth parameter is NOT simply an email address - it is a header:value type arrangement with multiple headers separated using \r\n
In your code perhaps you could try:-
if($_POST["submit"]) {
// The message
$message=$_POST["message"];
$email=$_POST["email"];
$headers='From: '.$email."\r\n".
'Reply-To: '.$email;
// In case any of our lines are larger than 70 characters, we should use wordwrap()
$message = wordwrap($message, 70, "\r\n");
// Send
mail('myemail#example.com', 'Quick Pass', $message, $headers );
}
Make sure you getting your form input correctly. then try this.
// recipients
$to = 'to#example.com'; //your email address
$user_email = $_POST["email"]; //input from form
// subject
$subject = 'Subject';
// message
$message = $_POST["message"];
// set conent type
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: User Email <'.$user_email.'>' . "\r\n";
// Mail it
mail($to, $subject, $message, $headers);
As previous posters have said, the fourth parameter id for for headers so it should be "From: {$email}" or similar.
However, mail servers are sometimes configured to ignore From headers and set a default value. Also you could find that the message is being ignored somewhere along the email route (by your host's own relay server or by your email provider's incoming mail server) if the sending address doesn't match any DNS MX records for the domain in question. You will need to talk to your hosting provider(s) to investigate this.