PHP Contact Form Not Sending All Data - php

I took the code below from a website but it only seems to send the Message and not the Name or Email. Can anyone tell me why?
<?php $action=$_REQUEST['action'];
if ($action=="") { ?>
<form action="" method="POST" enctype="multipart/form-data">
<input type="hidden" name="action" value="submit">
Your name:<br>
<input name="name" type="text" value="" size="30"/><br>
Your email:<br>
<input name="email" type="text" value="" size="30"/><br>
Your message:<br>
<textarea name="message" rows="7" cols="30"></textarea><br>
<input type="submit" value="Send email"/>
</form>
<?php
}
else {
$message=$name=$_REQUEST['name'];
$email=$_REQUEST['email'];
$message=$_REQUEST['message'];
if (($name=="")||($email=="")||($message==""))
{
echo "All fields are required, please fill the form again.";
}
else{
$from="From: dG<my#email.com>\r\nReturn-path: $email";
$subject="Message sent using your contact form";
mail("my#email.com", $subject, $message, $from);
echo "Email sent!";
}
}
?>
I thought that I could change
mail("my#email.com", $subject, $message, $from);
to be more like
mail("my#email.com", $subject, $name, $email, $message, $from);
but that didn't work.
As you can see, I don't know much about PHP, so please keep it as simple as you can.
Thanks.

The mail() function accepts these arguments:
to, subject, message, headers, parameters
So, to include the contents of the $name and $email variables in the message, you'll probably want to add them to the message variable. Something like:
$message = $name . $email . $message;
Then, when you send the e-mail with:
mail("my#email.com", $subject, $message, $from);
the $message will include the $name and $email contents.
Once you get that working, you'll probably want to add some space between the name, email and message.
Here is what the complete else block could look like:
else{
$from="From: dG<my#email.com>\r\nReturn-path: $email";
$subject="Message sent using your contact form";
$message = $name . $email . $message;
mail("my#email.com", $subject, $message, $from);
echo "Email sent!";
}
Here is a link to the full documentation on the mail() function:
http://php.net/manual/en/function.mail.php

Related

how to forward user text to an email

This is my first time trying to create a php forum. Let's say I have a first user that posts a subject and enters his email address.
<form action="whatever.php" method="post">
Discussion Content:
<br />
<input type="text" name="name" style="height:200px; width:800px" />
<br />
E-mail:
<input type="text" name="poster#emai.com" />
<br />
<input type="submit" />
</form>
Then I need the second user to be able to respond to the first user's post and second user's reply to be sent directly to first user's email. So this is what I've got and it's not even close. Appreciate any help.
<?php
$from = filter_var($_POST["email1"], FILTER_SANITIZE_EMAIL);
if ( !$from ) {
die('Invalid from email address');
}
$to = "email2";
$subject = 'Test email';
//data
$msg = $_POST['response'];
mail($to, $subject, $msg, 'From: ' . $from);
echo "Mail Sent.";
?>
Use the headers option in the mail function. You can check example #2 of the php mail function here.
<?php
$from = filter_var($_POST["replyersEmail"], FILTER_SANITIZE_EMAIL);
if ( !$from ) {
die('Invalid from email address');
}
$to = "postersEmail";
$subject = 'Test email';
//data
$msg = $_POST['reply'];
mail($to, $subject, $msg, 'From: ' . $from);
echo "Mail Sent.";
?>
Please be sure to sanitize your inputs! People can abuse this, so please read this on how to do so.

Html php email form not working

Hello I am trying to add a html email form to my website but cannot get it to work. The following is the code I currently have but when I try the form I don't receive an email.
<?php
$action=$_REQUEST['action'];
if ($action=="") /* display the contact form */
{
?>
<form action="" method="POST" enctype="multipart/form-data">
<input type="hidden" name="action" value="submit">
Your name:<br>
<input name="name" type="text" value="" size="30"/><br>
Your email:<br>
<input name="email" type="text" value="" size="30"/><br>
Your message:<br>
<textarea name="message" rows="7" cols="30"></textarea><br>
<input type="submit" value="Send email"/>
</form>
<?php
}
else /* send the submitted data */
{
$name=$_REQUEST['name'];
$email=$_REQUEST['email'];
$message=$_REQUEST['message'];
if (($name=="")||($email=="")||($message==""))
{
echo "All fields are required, please fill the form again.";
}
else{
$from="From: $name<$email>\r\nReturn-path: $email";
$subject="Message sent using your contact form";
mail("****************", $subject, $message, $from);
echo "Email sent!";
}
}
?>
I recommend using PHPMailer.
It will save you a lot of trouble and there are plenty of guides, with freely available default configs where all you need to do is modify the contents, sender and recipient of the email.
first - use $_POST, not $_REQUEST;
second - is there right settings on your server? maybe there is no mail support;
and the last - 4th argument of mail function is not "from", it's headers where you can also set mime-type and others..
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
the error is in this line :
mail("****************", $subject, $message, $from);
replace the asterisks with your email
mail function returns boolean, check whether it is sending or not sending.
Change your mail code like below and try to know whats happening.
`
if (mail("himmerz#gmail.com", $subject, $message, $from)) {
echo 'send';
} else {
echo 'not send';
}
`
mail("****************", $subject, $message, $from);
change to:
mail($email, $subject, $message, $from);

php contact form not working with some email addresses?

I have a really simple php contact form on one of my sites, the problem is that it won't work when sending emails to some addresses.
It works fine sending to my gmail address, but it doesn't work with iCloud (#me.com) addresses or other domain specific emails that I have set up.
<?php
$action=$_REQUEST['action'];
if ($action=="") /* display the contact form */
{
?>
<form action="" method="POST" enctype="multipart/form-data">
<input type="hidden" name="action" value="submit">
Your name:<br>
<input name="name" type="text" value="" size="30"/><br>
Your email:<br>
<input name="email" type="text" value="" size="30"/><br>
Your message:<br>
<textarea name="message" rows="7" cols="30"></textarea><br>
<input type="submit" value="Send email"/>
</form>
<?php
}
else /* send the submitted data */
{
$name=$_REQUEST['name'];
$email=$_REQUEST['email'];
$message=$_REQUEST['message'];
if (($name=="")||($email=="")||($message==""))
{
echo "All fields are required, please fill the form again.";
}
else{
$from="From: $name<$email>\r\nReturn-path: $email";
$subject="Message sent using your contact form";
mail("myemailaddress#me.com", $subject, $message, $from);
echo "Email sent!";
}
}
?>
If it didn't work at all I'd know there was a syntax error, but I get the 'Email Sent!' confirmation.
add if statement to the mail function :
if( mail("myemailaddress#me.com", $subject, $message, $from)) echo "Email sent!";
else echo "failed" ;
this way you'll know if it was sent or not .
then start checking the problem .
you man check your php.ini file :
check
sendmail_from = '';
sendmail_path = '';
and fill them with needed data ... maybe some address doesnt accept emails with no full data in the header . maybe they found it as a spam or somthing else .
Just have a look at your else part where you are using mail .Just below that is the echo so it always get executed as soon as it enters the else block.So, check for mail by if & else condition and then show the massage accordingly

PHP contact form not working, syntax error on line 7

I am trying to make a contact form in PHP, when submitted I get this error: Parse error: syntax error, unexpected ')' in /home/aginther14/aginther14.interactivedesignlab.com/contact.php on line 7
I am new to PHP, how do I fix this? Thanks.
<?php
$to = "gintherthegreat#gmail.com";
$subject = "AdamGinther.com Message";
$email = $_REQUEST['email'] ;
$message = $_REQUEST['message'] ;
$headers = "From: $email";
$sent = mail($to, $subject, $message, $headers, $email,) ;
if($sent)
header("Location: contactconfirmed.html");
else
{print "We encountered an error sending your mail"; }
?>
<form name="contact" action="contact.php" method="post" autocomplete="off">
Name: <input type="text" name="Usersname"><br><br>
E-Mail: <input type="text" name="email"><br><br>
Comment:<br> <textarea name="message"></textarea><br><br>
<input type="submit" value="Send Me a Message!" id="button">
Line No 7 is :
$sent = mail($to, $subject, $message, $headers, $email,) ;
^--- remove this comma
It should be like:
$sent = mail($to, $subject, $message, $headers, $email) ;
<?php
if (isset($_REQUEST['email']))
//if "email" is filled out, send email
{
//send email
$email = $_REQUEST['email'] ;
$subject = "AdamGinther.com Message";
$message = $_REQUEST['message'] ;
mail("gintherthegreat#gmail.com", $subject,
$message, "From:" . $email);
echo "Thank you for using our mail form";
}
else
//if "email" is not filled out, display the form
{
echo "<form method='post' action='contact.php'>
Email: <input name='email' type='text'><br>
Subject: <input name='subject' type='text'><br>
Comment:<br>
<textarea name='message' rows='15' cols='40'>
</textarea><br>
<input type='submit'>
</form>";
}
?>

Contact form. How do I get the name along with the email?

It's my first time trying to make a contactform. And I've got a few problems
It's works, I get the email, but I don't get the name the name field with me in the email.
HTML:
<form method="post" name="contactform" action="scripts/contact.php">
<h3>Name</h3>
<input type="text" name="name">
<h3>Email Address</h3>
<input type="text" name="email">
<h3>Message</h3>
<textarea name="message"></textarea>
<br/><input class="submit" type="submit" value="Send Form">
</form>
PHP:
<?php
$to = "name#domane.com";
$subject = "Contact Us";
$name = $_POST['name'];
$email = $_POST['email'] ;
$message = $_POST['message'] ;
$headers = "From: $email";
$sent = mail($to, $subject, $message, $name, $headers) ;
if($sent)
{print "Your mail was sent successfully"; }
else
{print "One of the field are not filled as requirred"; }
?>
$name is my problem. I've I have it in, the email comes from hostmaster#domane.com, If I delete it, everything works fine. But I wan't the name to be sent to me. How?
Or should I do it completely different?
Also, if you leave all the fields blank, the "user" doesn't get any error message, and a blank email is sent to me.
Hope you can help me. :)
Michael Berkowski is correct. What you'll need to do is add the name to your message's body (not in the sense of the input name= attribute, rather the body of the email).
Something like this:
<?php
$to = "name#domane.com";
$subject = "Contact Us";
$name = $_POST['name'];
$email = $_POST['email'] ;
$message = $_POST['message'] ;
$headers = "From: $email";
$body = "Name: $name\r\n";
$body .= "Message: $message";
$sent = mail($to, $subject, $body, $headers) ;
if($sent)
{print "Your mail was sent successfully"; }
else
{print "One of the field are not filled as requirred"; }
?>
Revised:
HTML:
<form method="post" name="contactform" action="scripts/contact.php">
<label for="name">Name</label>
<input type="text" name="name" id="name" />
<label for="email">Email Address</label>
<input type="text" name="email" id="email" />
<label for="message">Message</label>
<textarea name="message" id="message"></textarea>
<br/><input class="submit" type="submit" value="Send Form" />
</form>
PHP:
<?php
$name = $_POST['name'];
$email = $_POST['email'] ;
$message = $_POST['message'] ;
$body = "Name: $name\r\n";
$body .= "Message: $message";
$to = "name#domane.com";
$from = "automailer#mydomainname.com (Website Automailer)";
$subject = "Contact Us";
$headers = "From: $from\r\n" .
"Reply-To: $email ($name)";
$sent = mail($to, $subject, $body, $headers) ;
if($sent) { echo "Your mail was sent successfully"; }
else { echo "One of the field are not filled as requirred"; }
?>
You should read the mail function documentation on php.net.
Have a look at the function signature:
bool mail ( string $to , string $subject , string $message [, string $additional_headers [, string $additional_parameters ]] )
Now you're placing $name as the "$additional_headers" argument. You should pass $name and any extra relevant data in a $message argument instead.
Having that said, here's the correct code to send a message:
$sent = mail($to, $subject, "A message from $name: $message", $headers);
You should read more about how email messages are constructed. Instead of just putting a user defined message in there you probably want to specify some email headers, containing a more beautiful FROM: and the like...

Categories