PHP Mail HTML Comment Form Variables - php

Can you provide the proper syntax to pass the html form variables to php mailer?
The php is:
$comment = $_POST['comment'];
$email = $_POST['email'];
$to = 'sssff#gmail.com';
$subject = 'From Website';
$message = $comment;
$headers = 'From: $email';
mail($to, $subject, $message, $headers);
$message needs to contain the contents of $comment
$headers needs to display the contents of $email as return address
Can anyone help me with the proper syntax? Thank you
EDIT
To clarify, the email i receive from the php mailer does not contain the from address contained by $email, nor does the message contain the comments from $comment.
The email is sent fine, but does not contain those crucial elements.
If you'd like to look at the form it is:
<form class="cmxform" id="commentForm" method="POST" action="">
<label for="cname">Name</label>
<input id="cname" type="text" name="name" size="60" class="required" minlength="2" />
<label for="cemail">E-Mail</label>
<input id="cemail" type="text" name="email" size="60" class="required email" />
<label for="curl">URL</label>
<input id="curl" type="text" name="url" size="60" class="url" value="" />
<label for="ccomment">Your comment</label>
<textarea id="ccomment" type="text" name="comment" cols="72" rows="8" class="required"></textarea>
<div id="button2"><input class="submit" id="submit_btn" type="submit" value="Send Email"/></div>
</form>
Thank you for your help

$headers = 'From: $email';
^--- ^---
should be " instead. Single quoted strings do not interpolate variables, so you're using a literal $email as your From address, not someone#example.com.

$comment = $_POST['comment'];
$email = $_POST['email'];
$to = "$email";
$from = 'sender email';
$subject = 'message subject';
//Begin HTML Email Message
$message = "$comment";
//end of message
$headers = "From: $from\r\n";
$headers .= "Content-type: text\r\n";
mail($to, $subject, $message, $headers);

Related

PHP Contact Form not working. Getting internal server error 500 [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
So I am trying to make a contact form for my website but i keep getting the internal server error 500. If anyone could look through the code and tell me if I am doing anything wrong that would be great. Thanks!
HTML name is index.html:
<form method="post" action="send_contact.php">
<p class="full-row">
<label for="name">Your Name:</label>
<input type="text" id="name" name="name">
</p>
<p class="full-row">
<label for="email">Email:</label>
<input type="text" id="email" name="email">
</p>
<p class="full-row">
<label for="subject">Subject:</label>
<input type="text" id="subject" name="subject">
</p>
<p class="full-row">
<label for="message">Message:</label>
<textarea name="message" id="message" rows="6"></textarea>
</p>
<input id="submit" class="mainBtn" type="submit" name="send" value="Send Message" onClick="">
</form>
PHP name is send_contact.php:
<?php
// Contact subject
$subject ="$subject";
// Details
$message="$detail";
// Mail of sender
$mail_from="$user_mail";
// From
$header="from: $name <$mail_from>";
// Enter your email address
$to ="example#example.com";
$link = "<script>window.open('confirm.html')</script>";
$send_contact=mail($to,$subject,$message,$header);
// Check, if message sent to your email
// display message "We've recived your information"
if($send_contact){
header('confirm.html');
}
else {
echo "ERROR";
}
?>
To get variable values in PHP, use $_POST['variableName']
for example to fetch this:
<input type="text" name="email">
You will need this:
$email= $_POST['email'];
<?php
// Contact subject
$subject = $_POST['subject'];
// Details
$message=$_POST['message'];
// Mail of sender
$mail_from=$_POST['email'];
// From
$header="from: ".$_POST['name']." <".$_POST['email'].">";
// Enter your email address
$to ="example#example.com";
$send_contact=mail($to,$subject,$message,$header);
// Check, if message sent to your email
// display message "We've recived your information"
if($send_contact){
header('Location: confirm.html');
}
else {
echo "ERROR";
}
?>
Also here are additional headers to help your email going in the inbox rather than the junk box:
$to = "webtest#website.com";
$subject = "Test Email";
$message = "Test Email";
// normal headers
$num = md5(time());
$headers = "From: Me <me#website.com>\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: multipart/mixed; ";
$headers .= "boundary=".$num."\r\n";
$headers .= "--$num\r\n";
// This two steps to help avoid spam
$headers .= "Message-ID: <".time()." TheSystem#".$_SERVER['SERVER_NAME'].">\r\n";
$headers .= "X-Mailer: PHP v".phpversion()."\r\n";
// With message
$headers .= "Content-Type: text/html; charset=UTF-8\r\n";
$headers .= "Content-Transfer-Encoding: 8bit\r\n";
$headers .= "".$message."\n";
$headers .= "--".$num."\n";
#mail($to, $subject, $message, $headers);
Cheers!
You are sending the form with post so in php you can access it like this $name = $_POST['name'].
You can access the data sent through the associative array [$_POST][1], like so:
$subject = $_POST["subject"];
HTML:
<form method="post" action="send_contact.php">
<p class="full-row">
<label for="name">Your Name:</label>
<input type="text" id="name" name="name"/>
</p>
<p class="full-row">
<label for="email">Email:</label>
<input type="email" id="email" name="email"/>
</p>
<p class="full-row">
<label for="subject">Subject:</label>
<input type="text" id="subject" name="subject"/>
</p>
<p class="full-row">
<label for="message">Message:</label>
<input type="text" name="message" id="message"/>
</p>
<input id="submit" class="mainBtn" type="submit" name="send" value="Send Message"/>
</form>
PHP:
//Check if the values requested were provided
if (isset($_POST["name"]) && isset($_POST["email"]) && isset($_POST["subject"]) && isset($_POST["message"])) {
$name = $_POST["name"];
$email = $_POST["email"];
$subject = $_POST["subject"];
$message = $_POST["message"];
$header = "from: $name <$email>";
$to = "example#example.com";
if (mail($to, $subject, $message, $header)) {
header("Location: confirm.html");
} else {
echo("Error");
}
}
?>
As others have suggested, check your error logs if you're still running in to trouble.

Not receiving email with PHP form

I'm trying to make a simple html form with php to send messages to a specific email. A friend sent me this form but since i'm very noobish in PHP i can't seem to figure out why i'm not receiving any emails...
This is my HTML CODE:
<div class="form">
<form id="contactform" action="mailer.php" method="post">
<p class="contact">NOME</p>
<input id="name" name="name" placeholder="Primeiro e Ășltimo nome" required type="text">
<p class="contact">EMAIL</p>
<input id="email" name="email" placeholder="exemplo#domĂ­nio.com" required type="email">
<p class="contact">MENSAGEM</p>
<textarea rows="6" required placeholder="Escreva aqui a sua mensagem"></textarea>
<br />
<input class="buttom" name="submit" id="submit" tabindex="5" value="ENVIAR" type="submit">
</form>
</div>
And this is my PHP:
<?php
$to = 'MYEMAIL#gmail.com';
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$headers = 'From: '.$_POST['email'].'' . "\r\n" .
'Reply-To: MYEMAIL#gmail.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $name, $subject, $message, "From: MYEMAIL#gmail.com\n");
?>
Any help, please?
Your form is wrong:
<h2>Feedback Form</h2>
<?php
// display form if user has not clicked submit
if (!isset($_POST["submit"]))
{
?>
<form method="post" action="<?php echo $_SERVER["PHP_SELF"];?>">
From: <input type="text" name="from"><br>
Subject: <input type="text" name="subject"><br>
Message: <textarea rows="10" cols="40" name="message"></textarea><br>
<input type="submit" name="submit" value="Submit Feedback">
</form>
<?php
}
else
// the user has submitted the form
{
// Check if the "from" input field is filled out
if (isset($_POST["from"]))
{
$from = $_POST["from"]; // sender
$subject = $_POST["subject"];
$message = $_POST["message"];
// message lines should not exceed 70 characters (PHP rule), so wrap it
$message = wordwrap($message, 70);
// send mail
mail("webmaster#example.com",$subject,$message,"From: $from\n");
echo "Thank you for sending us feedback";
}
}
?>
$name is not a parameter in the mail function. If you are trying to add the persons name somewhere, add it to the subject, message or define it as an additional parameter. Try taking $name out and see if it works without it.
PHP mail function

Php contact form not working well

So I have built my website and I planned on using a php based contact page to send emails to my email address I made this and I receive an e-mail but no content or summary...
so here is my html:
<form action="Php/sendemail.php" method="POST">
<div id="form-inner">
<input type="text" class="input" name="name" id="name" placeholder="Name">
<input type="email" class="input" name="email" id="email" placeholder="E-mail">
<textarea class="input textarea" name="message" id="message" placeholder="Your message here"></textarea>
<input type="submit" class="button" value="Send message" id="button">
</div>
</form>
and here is my php:
$subject = "Contact form submitted!";
$to = 'email#example.com';
$body = <<<HTML
$message
HTML;
$headers = "From: $email\r\n";
$headers .= "Content-type: text/html\r\n";
mail($to, $subject, $message, $headers);
header('Location: /Contact.php');
?>
I really do not understand why this is happening if you can please help ! Thnaks
This is because you are not geeting the value from the $_POST superglobal:
$body = <<<HTML
$message
HTML;
should be:
$message = $_POST['message'];
$body = <<<HTML
$message
HTML;
Of course there are other ways to do this like just putting $_POST['message']; in the message body but this illustrates how to get the submitted form values.
(You will also want to do things like clean up (i.e. trim(), etc) the submitted values and also defend against header injections as well).

Can't get data from Form to Email

I have a simple form and I've tried everything, from changing POST to GET to REQUEST. single quote, double quote. EOD... nothing worked. I get the confirmation page, I get subject and text back in the email, but no data. Please help!!
signup.html
<form action="/signupprocess.php" method="post" enctype="text/plain" name="signup"
target="_blank" id="signup">
<input name="email" type="text" id="email" size="35" maxlength="90">
<input name="name" type="text" id="name" size="35" maxlength="80">
<input name="address" type="text" id="address" size="35" maxlength="90">
signupprocess.php
$email = $_POST['email'];
$name = $_POST['name'];
$address = $_POST['address'];
$phone = $_POST['phone'];
$account = $_POST['account'];
$authorize = $_POST['authorize'];
$body = <<<EOD
Name: $name <br>
Address: $address <br>
Phone Number: $phone <br>
Account: $account <br>
Authorize: $authorize <br>
EOD;
$headers = "From: $to\r\n";
$headers .= "Content-type: text/html\r\n";
mail($to, $subject, $body, $headers);
echo '<META HTTP-EQUIV=Refresh CONTENT="0; URL='.$url.'">';
Get rid of the enctype attribute in the <form>

Send email using php emailing to multiple people

I have a php email script here but it will only take one email. How can i set this up to take multiple emails say upto 500? if i were to enter them as such.... email1#aol.com, email2#aol.com, email3#aol.com ect sperated by commas?
From Address: <input type="text" id="from" value="fakeemailer#albertyw.mit.edu" size="40"><br>
Recipient Address: <input type="text" id="to" size="40"><br>
Subject: <input type="text" id="subject" size="40"><br>
Message: <br><textarea id="message" cols="50" rows="10"></textarea><br>
<input type="submit" onclick="javascript:sendmail()" value="SEND">
$headers = $_POST['headers'];
$from = $_POST['from'];
$to = $_POST['to'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$headers .="From: $from";
mail($to, $subject, $message, $headers) or die("EMAIL FAIL");
echo $to."<br-->";
echo $subject."<br>";
echo $message."<br>";
echo $headers."<br>";
echo 'EMAIL SENT';
Exactly as you put it in your question. Commas separate multiple recipients.
Mail() in the PHP manual is a good place to start.
try this
<input type="text" id="to" value="fakeemailer#albertyw.mit.edu, email2, email3, email4" name="to">

Categories