how to forward user text to an email - php

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.

Related

PHP mail script gives me syntax error

Im trying to get my php mailer embedded on my html page, I get no syntax errors in Dreamweaver but sendmail (http://glob.com.au/sendmail/) keeps giving me this error message "Syntax error in arguments
" Im hoping you can help me nail the issue
My PHP mailer:
//Email information
$admin_email = "------------ Secret";
$email = $_REQUEST['email'];
$subject = $_REQUEST['subject'];
$comment = $_REQUEST['comment'];
//send email
mail($admin_email, "$subject", $comment, "From:" . $email);
//Email response
echo "Thank you for contacting us!";
}
//if "email" variable is not filled out, display the form
else {
?>
Here is the relevant page code
<?php
//if "email" variable is filled out, send email
if (isset($_REQUEST['email'])) {
//Email information
$admin_email = "schlichtingr#yahoo.com";
$email = $_REQUEST['email'];
$subject = $_REQUEST['subject'];
$comment = $_REQUEST['comment'];
//send email
mail($admin_email, "$subject", $comment, "From:" . $email);
//Email response
echo "Thank you for contacting us!";
}
//if "email" variable is not filled out, display the form
else {
?>
<html>
<head>
</head>
<body>
<div id ="Email">
<form method="post"><br />
<h3><b>send us a message</b></h3>
<span>Any further questions or concerns? Send us an email!</span><br>
Email: <input name="email" type="text" /><br />
Subject: <input name="subject" type="text" /><br />
Message:<br />
<textarea name="comment" rows="15" cols="40"></textarea><br />
<input type="submit" value="Submit" />
</form>
</div>
<?php
}
?>
Any help you can give me would be much appreciated
I am new to php but still trying to learn
Basically I just want to make a mail form built onto an html page
I'm not familiar with sendmail, but I notice 1 thing:
"$subject" needs to be just $subject . You are capturing that variable for a reason. So turning it into a string of "$subject" makes no sense to me.
Hope this helps!
Try this code:
//if "email" variable is filled out, send email
if (isset($_REQUEST['email'])) {
//Email information
$admin_email = "user#example.com";
$email = $_REQUEST['email'];
$subject = $_REQUEST['subject'];
$comment = $_REQUEST['comment'];
$headers = "From: $email" . "\r\n";
mail($admin_email, $subject, $comment, $headers);
//Email response
echo "Thank you for contacting us!";
}

PHP Contact Form Not Sending All Data

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

HTML form working but need a php validation

i need a help with this. What i need to do to validate the email in this form? This is a landing page, I want to get the email of a visitor before he/she can visit my content here is this demo.
Can I get some tips or the right code to use for this page?
<form id="contact-form" action="send.php">
<input type="text" name="email" placeholder="you#yourmail.com" class="cform-text" size="40" title="your email" required>
<input type="submit" value="Enter Now" class="cform-submit">
</form>
send.php file:
$visitorEmail = $_GET['email'];
$email_from = "doctordongok#gmail.com";
$email_subject = "New Form Submission! - From: " + $visitorEmail;
// edit here
$email_body = "New visitor - $visitorEmail";
$to = "doctordongok#gmail.com";
$headers = "From: $email_from \r \n";
$headers .= "Reply-To: $visitorEmail \r \n";
mail($to, $email_subject, $email_body, $headers);
header("Location: http://www.de-signs.com.au");
Thank you!
Use the filter_var() function of PHP:
if(filter_var($visitorEmail, FILTER_VALIDATE_EMAIL) === false) {
// Invalid E-Mail address
} else {
// OK
}
So to mockup your new script. It will look something like this
$visitorEmail = $_GET['email'];
if(filter_var($visitorEmail, FILTER_VALIDATE_EMAIL) === false) {
die('Sorry that\'s no valid e-mail address');
} else {
$email_from = 'doctordongok#gmail.com';
$email_subject = 'New Form Submission! - From: ' . $visitorEmail;
// edit here
$email_body = 'New visitor - ' . $visitorEmail;
$to = 'doctordongok#gmail.com';
$headers = 'From: $email_from' . PHP_EOL;
$headers .= 'Reply-To: $visitorEmail' . PHP_EOL;
mail($to, $email_subject, $email_body, $headers);
header('Location: http://www.de-signs.com.au');
exit;
}
Your best bet is probably to validate on the client side using a Javascript and on the server side as well using a regular expression on both ends.
Validating an email with Javascript
HTML5's new email type for input is here to help you.
<input type="email" name="email" placeholder="you#yourmail.com" class="cform-text" size="40" title="your email" required />

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...

Why is this contact form not working?

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);

Categories