I will admit that I am not the best at PHP, but what I am doing usually works well enough for me because the clients just need the information sent to their emails, and the security question works well enough to keep spam away from their inbox. But for some reason, this code will not work and I cannot figure out why. This same code with different variables is on another site I have and is tested and working. Does anyone have any advice?
<form action="submit_form.php" method="POST" >
<p>Name<br /><input type="text" name="name" required /></p>
<p>Email Address<br /><input type="text" name="email" required /></p>
<p>Phone Number<br /><input type="text" name="phone" required /></p>
<p>County<br /><input type="text" name="county" required /></p>
<p>Annual Income<br /><input type="text" name="income" required /></p>
<p>What is 4 + 1? (anti-spam)<br /><input type="text" name="answer" required /></p>
<input type="submit" value="Take The First Step" />
</form>
<?php # BOOST
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$county = $_POST['county'];
$income = $_POST['income'];
$correct_answer = '5';
$answer = $_POST['answer'];
if ($correct_answer != $answer) {
die("You're not a valid user of this site!");
}
else {
$to = "glenn#boostbizseo.com";
$subject = "USDA LOANS";
$message = "USDA LOANS:\n
Name: $name
Email: $email
Phone: $phone
County: $county
Annual Income: $income";
$from = "USDA Loans";
mail($to,$subject,$message,$headers);
echo "Thank you for getting in contact with us. We will be in contact with you soon regarding your USDA Loan! <a href=http://www.usdaloansmo.com>Click Here</a> to go back to our website!";
}
?>
$from = "someonelse#example.com"; //email ID
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
I believe this will work.
This is wrong:
$from = "USDA Loans";
mail($to,$subject,$message,$headers);
You don't use $from and $headers is undefined.
Here is how you send email using php.
$to="some#domain.com";
$subject="Your subject";
$header="from: ABCName <me#mydomain.com>";
$message="Message \r\n";
$sentmail = mail($to,$subject,$message,$header);
if($sentmail)
{
echo "Done";
}
else
{
echo "Oops, Something went wrong";
}
Related
I'm trying to send an email using PHP mail(). I created a form that allows the user to input their information and press a submit button to send the email.
What I'm trying to do is to grab $email and set it to the $to header to send to the receiver. Everything else works perfectly, $name and $contact outputs just missing $email.
<form action="#" method="post">
<input type="text" name="name" placeholder="Your Name"><br/>
<input type="text" name="email" placeholder="Your Email"><br/>
<input type="text" name="contact" placeholder="Your Mobile"><br/>
<input type="submit" name="submit" value="Submit">
</form>
<?php
echo "Welcome: ". $_POST['name']. "<br />";
echo "Your Email is: ". $_POST["email"]. "<br />";
echo "Your Mobile No. is: ". $_POST["contact"];
?>
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$contact = $_POST['contact'];
$to = $email;
$subject = "Test Email";
$message = 'Hello '.$name.', contact: '.$contact.', this is a test email';
$headers = "From: Me <". strip_tags('test#mail.com') . ">\r\n";
mail($to,$subject,$message,$headers);
?>
Any tips would be appreciated
Solution:
<?php
if (!array_key_exists('Submitted',$_POST))
{
?>
<form method="post" action="#">
<input type="hidden" name="Submitted" value="true"><form action="#"
method="post">
<input type="text" name="name" placeholder="Your Name"><br/>
<input type="text" name="email" placeholder="Your Email"><br/>
<input type="text" name="contact" placeholder="Your Mobile"><br/>
<input type="submit" name="submit" value="Submit">
</form>
<?php
echo "Welcome: ". $_POST['name']. "<br />";
echo "Your Email is: ". $_POST["email"]. "<br />";
echo "Your Mobile No. is: ". $_POST["contact"];
?>
<?php
}
else
{
$name = $_POST['name'];
$contact = $_POST['contact'];
$to = $_POST['email'];
$subject = "Test Email";
$message = 'Hello '.$name.',<br /> contact #: '.$contact.', this is a test
email';
$headers = "From: Me <". strip_tags('test#mail.com') . ">\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
if(mail($to,$subject,$message,$headers))
{
echo "Message Sent";
}
else
{
echo "Message Not Sent";
}
}
?>
I want to set a Dynamic Header on mail header when i send mail. I don't want to do it with SMPT server and if it will be in codeigniter, so it will be greate. You will get idea what exactly i want by given image bellow.
from: Google < dynamic email#gmail.com >
My Code
<?php
if(isset($_POST['send'])) {
//Email information
$email = $_POST['email'];
$subject = $_POST['subject'];
$comment = $_POST['comment'];
//send email
mail($email, "$subject", $comment, "From:" . $email);
//Email response
echo "Thank you for contacting us!";
}
//if "email" variable is not filled out, display the form
else {
?>
<form method="post" name="testmail">
Email: <input name="email" type="text" />
Subject: <input name="subject" type="text" />
Message:<textarea name="comment" rows="15" cols="40"></textarea>
<input type="submit" name="send" value="Submit" />
</form>
<?php } ?>
use like this:
mail(to,subject,message,headers,parameters);
I need to make a contact form and I did, but when Im sending test message Im not reciving that message, I have check my email addres and test message never get there i also looked into spam folder but still no luck how to fix this? here is my code
<?php
$formMessage = "";
$senderName = "";
$senderEmail = "";
$senderMessage = "";
if ($_POST['username']) { // If the form is trying to post value of username field
// Gather the posted form variables into local PHP variables
$senderName = $_POST['username'];
$senderEmail = $_POST['email'];
$senderMessage = $_POST['msg'];
// Make sure certain vars are present or else we do not send email yet
if (!$senderName || !$senderEmail || !$senderMessage) {
$formMessage = "The form is incomplete, please fill in all fields.";
} else { // Here is the section in which the email actually gets sent
// Run any filtering here
$senderName = strip_tags($senderName);
$senderName = stripslashes($senderName);
$senderEmail = strip_tags($senderEmail);
$senderEmail = stripslashes($senderEmail);
$senderMessage = strip_tags($senderMessage);
$senderMessage = stripslashes($senderMessage);
and here is part which im not sure about
// End Filtering
$to = "test#gmail.com";
$from = "contact#your_website_here.com";
$subject = "You have a message from your website";
// Begin Email Message Body
$message = "
$senderMessage
$senderName
$senderEmail
";
// Set headers configurations
$headers = 'MIME-Version: 1.0' . "rn";
$headers .= "Content-type: textrn";
// Mail it now using PHP's mail function
mail($to, $subject, $message, $headers);
$formMessage = "Thanks, your message has been sent.";
$senderName = "";
$senderEmail = "";
$senderMessage = "";
} // close the else condition
} // close if (POST condition
?>
here is html form
<form id="form1" name="form1" method="post" action="contact.php">
<?php echo $formMessage; ?>
<br />
<br />
Your Name:<br />
<input name="username" type="text" id="username" size="36" maxlength="32" value="<?php echo $senderName; ?>" />
<br />
<br />
Your Email:
<br />
<input name="email" type="text" id="email" size="36" maxlength="32" value="<?php echo $senderEmail; ?>" />
<br />
<br />
Your Message:
<br />
<textarea name="msg" id="msg" cols="45" rows="5"><?php echo $senderMessage; ?> </textarea>
<br />
<br />
<br />
<input type="submit" name="button" id="button" value="Send Now" />
</form>
I'm trying to set up a simple contact form. Everything is styled correctly, but when I hit submit it doesn't take me anywhere, just attempts to open contact.php. I think there's something missing in the code that actually sends the message out. I'm sure it's something fairly simple that I'm missing, but this is a little over my head. Any help is appreciated.
<form action="mail.php" method="POST">
<p>Name</p> <input type="text" name="name">
<p>Company</p> <input type="text" name="company">
<p>Email</p> <input type="text" name="email">
<p>Phone</p> <input type="text" name="phone">
<p>Message</p><textarea name="message" rows="4" cols="25"></textarea><br />
<input type="submit" value="Submit">
</form>
<?php
$name = $_POST['name'];
$company = $_POST['company'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];
$formcontent="From: $name \n Message: $message";
$recipient = "______#gmail.com";
$subject = "Contact Form";
$mailheader = "From: $email \r\n";
$to ='______#gmail.com';
$send_contact=mail($to,$subject,$message,$header);
// Check, if message sent to your email
// display message "We've received your information"
if($send_contact){
echo "We've received your contact information";
}
else {
echo "ERROR";
}
?>
EDIT: I was able to receive an email finally after adding the complete url for mail.php...however none of the information except for the message was included. The sender was listed as Apache...how can I assure that information entered in the forms will be included in the email? Thanks for all the help thus far.
since you are specifying code within the same page, you can omit action in your form.
Also, put a condition to run PHP once the script has been run.
<?php
if(isset($_POST['submit'])){
$name = $_POST['name'];
$company = $_POST['company'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];
$formcontent="From: $name \n Message: $message";
$recipient = "______#gmail.com";
$subject = "Contact Form";
$mailheader = "From: $email \r\n";
$to ='______#gmail.com';
$send_contact=mail($to,$subject,$message,$header);
// Check, if message sent to your email
// display message "We've received your information"
if($send_contact){
echo "We've received your contact information";
}
else {
echo "ERROR";
} }
?>
<form action = "<?= $_SERVER["PHP_SELF"]; ?>"method="POST">
<p>Name</p> <input type="text" name="name">
<p>Company</p> <input type="text" name="company">
<p>Email</p> <input type="text" name="email">
<p>Phone</p> <input type="text" name="phone">
<p>Message</p><textarea name="message" rows="4" cols="25"></textarea><br />
<input type="submit" value="Submit" name="submit">
</form>
Hope it helps!
EDIT: I am not sure what is the current filename but to keep it dynamic, I have mentioned $_SERVER["PHP_SELF"] which is not a recommended usage.
However, Try it! :)
It seems everything is in place:
PHP:
<?php
if (!empty($_POST['name'])){
$msg = "name". $_POST['name'];
}else{
$fname = NULL;
echo "Name Required.<br />";
}
if (!empty($_POST['email'])){
$msg = "email". $_POST['email'];
}else{
$lname = NULL;
echo "Email Required.<br />";
}
if (!empty($_POST['www'])){
$msg = "Website". $_POST['www'];
}else{
$lname = NULL;
echo "Website Required.<br />";
}
if (!empty($_POST['comment'])){
$msg = "Comment". $_POST['comment'];
}else{
$email = NULL;
echo "A comment is required.<br />";
}
$recipient = "myemail#gmail.com";
$subject = "Form Feedback";
$mailheaders = "Reply-to". $_POST['email'];
//send the mail
mail($recipient, $subject, $msg, $mailheaders);
?>
HTML:
<div id="contact" style="height:280px; margin:1px 0;">
<form id="contactLP" method="post" action="inc/php/contact_validate.php">
<div class="align"><input type="text" name="name" tabindex="1" /></div>
<div class="align"><input type="text" name="email" tabindex="2" /></div>
<div class="align"><input type="text" name="www" tabindex="3" /></div>
<div class="align"><textarea id="txta" name="comment" cols="15" rows="5" tabindex="4"></textarea></div>
<span style="color:transparent;">test</span>
<br><br>
<div class="align"><input type="submit" class="submit" name="sendForm" id="SubmitContact" value="" tabindex="5" /></div>
</form>
</div><!--CONTACT-->
When I fill it out correctly and submit, it says "Thanks for your message" or something similiar, but then I get nothing in email.
I tried running this both on a server on the internet, along with on my local server running on my workstation.
Am I doing something wrong above???????
Yes, you are "name; $_POST['name'] "; should be "name". $_POST['name']; in every instance you use that string.
Your $msg is only holding the current value.
Try something like this for all your value assignment to $msg variable
$msg .= "Comment". $_POST['comment'];
mail function
You seem to have screwed up the $mailheaders variable slightly (reply-to section), try this code in a stand alone script. If even it fails, you may have to check your mail function and how it is set up on the server. (change the email addresses obviously)
<?php
$to = 'nobody#example.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster#example.com' . "\r\n" .
'Reply-To: webmaster#example.com';
mail($to, $subject, $message, $headers);