I programmed a contact form in PHP and I want to include paragraphs in the message that gets send, so that the user would have to write in certain paragraphs and not just one block of text.
I've tried to split the message into multiple textareas, but I don't know how I should adjust the PHP code so that these textareas would get send inside one message in the right order. Each textareashould've presented one paragraph.
This is my PHP:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: mywebsite.com';
$to = 'myemailadress#adress.com';
$subject = 'subject line';
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html\r\n";
$headers .= 'From: '. $email. "\r\n" .
$headers .= "Reply-To: ". $email. "\r\n";
$headers .= "X-Mailer: PHP/" . phpversion();
$status = mail($to, $subject, $message, $headers);
if ($status) {
echo '<p style="color: white">Your Message was sent!</p>';
} else {
echo '<p style="color: white">Something went wrong. Please try again.</p>';
}
?>
This is my HTML:
<div class="contact-form">
<form id="contact-form" method="post" action="contact-form-handler.php">
<input name="name" type="text" class="form-control" placeholder="Your Name" required>
<br>
<input name="email" type="email" class="form-control" placeholder="Your Email">
<br>
<textarea name="message" class="form-control" placeholder="Message" rows="30" required></textarea><br>
<input type="submit" class="form-control" value="SEND MESSAGE">
</form>
</div>
https://www.php.net/manual/en/function.nl2br.php
When you use nl2br you can change the "\n" from the textarea to a "".
You can send a content-type: text/plain mail to and don't have to transform the newline characters into tags.
I don't see multiple textareas in your code so i guess nl2br is the function you're looking for.
I have add:
$message = nl2br($message); // Inserts HTML line breaks <br />
this should Insert HTML line breaks.
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: mywebsite.com';
$to = 'myemailadress#adress.com';
$subject = 'subject line';
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html\r\n";
$headers .= 'From: ' . $email . "\r\n" .
$headers .= 'Reply-To: ' . $email . "\r\n";
$headers .= 'X-Mailer: PHP/' . phpversion();
$message = nl2br($message); // <-- added to Inserts HTML line breaks.
$status = mail($to, $subject, nl2br($message), $headers);
if ($status) {
echo '<p style="color: white">Your Message was sent!</p>';
} else {
echo '<p style="color: white">Something went wrong. Please try again.</p>';
}
Related
I'm not good at PHP so please be patient and understandable. The e-mails from my contact-form always go into the spam folder. I ran a test on this site and it tells me that
there is no HTML tag in my HTML-only message (HTML_MIME_NO_HTML_TAG)
my message only has text/html MIME parts and I should add text/plain (MIME_HTML_ONLY)
my message has a missing header: date (MISSING_DATE)
they don't have a mail server (MX Record) behind my domain name and I should post a DNS record (MX type) for my domain name
I know that some of these questions have been asked before, but I don't know what to do and I think I need personal help. Thanks!
EDIT: I have two more problems:
there is no SPF record and I should add my domain name to my DNS zone file
the message is not signed by DKIM
My HTML:
<div class="contact-form">
<form id="contact-form" method="post" action="contact-form-handler.php">
<input name="name" type="text" class="form-control" placeholder="Your Name" required>
<br>
<input name="email" type="email" class="form-control" placeholder="Your Email">
<br>
<textarea name="message" class="form-control" placeholder="Message" rows="40" required></textarea><br>
<input type="submit" class="form-control" value="SEND MESSAGE">
</form>
</div>
My PHP:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: mywebsite.com';
$to = 'someone#something.net';
$subject = 'Subject-line';
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html\r\n";
$headers .= 'From: '. $email. "\r\n" .
$headers .= "Reply-To: ". $email. "\r\n";
$headers .= "X-Mailer: PHP/" . phpversion();
$message = nl2br($message);
$status = mail($to, $subject, $message, $headers);
if($status)
{
echo '<p>Your Message has been send!</p>';
} else {
echo '<p>Something went wrong. Please try again.</p>';
}
?>
I would like to send a confirmation email to the user who submitted the form and another email to me with the details which the user inputted, here is my form:
<form class="" action="." id="dateForm" method="POST">
<input type="text" class="form-control" id="dfName" placeholder="Name" required>
<input type="email" class="form-control" id="dfEmail" placeholder="Email" required>
<input type="tel" class="form-control" id="dfPhone" placeholder="Phone Number" required>
<input type="text" class="form-control" id="dfDate" placeholder="Schedule a call" required>
<textarea id="dfMessage" rows="5" class="form-control" placeholder="Your Message" required></textarea>
<button type="submit" class="">SUBMIT</button>
</form>
And here is my php which works perfect for sending the emails to me.
$name = trim($_POST['name']);
$email = trim($_POST['email']);
$phone = trim($_POST['phone']);
$date = trim($_POST['date']);
$message = trim($_POST['message']);
function is_email_valid($email) {
return filter_var($email, FILTER_VALIDATE_EMAIL);
}
if( isset($name) && isset($email) && isset($phone) && isset($date) && isset($message) && is_email_valid($email) ) {
$to = "myemail#gmail.com";
$subject = "New inquiry request from Olho";
$body = <<<EOD
<strong>Name:</strong> $name <br>
<strong>Email:</strong> $email <br> <br>
<strong>Phone:</strong> $phone <br>
<strong>Booking Date:</strong> $date <br>
<strong>Message:</strong> $message <br>
EOD;
$headers = "From: $name <$email>\r\n";
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
mail($to, $subject, $body, $headers);
So what I would like is to receive another email after the one send to me which will contain a confirmation message like "Thank you for contacting us, we will get back to you as soon as possible."
I've tried to use this but I am not receiving the confirmation email:
$conf_subject = 'Your recent enquiry';
$conf_sender = 'Olho';
$msg = $name . ",\n\nThank you for your recent enquiry. A member of our
team will respond to your message as soon as possible.";
$headers2 = "From: $conf_sender <myemail#gmail.com>\r\n";
$headers2 .= 'MIME-Version: 1.0' . "\r\n";
$headers2 .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
mail( $email, $conf_subject, $msg, $headers2 );
You should wait for first mail to be completed and then should send second email as below code:
if (mail($to, $subject, $body, $headers)) {
$conf_subject = 'Your recent enquiry';
$conf_sender = 'Olho';
$msg = $name . ",\n\nThank you for your recent enquiry. A member of our
team will respond to your message as soon as possible.";
$headers2 = "From: $conf_sender <myemail#gmail.com>\r\n";
$headers2 .= 'MIME-Version: 1.0' . "\r\n";
$headers2 .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
mail( $email, $conf_subject, $msg, $headers2 );
}
Hope it helps you.
I am trying to send a message from HTML form. But for some reason I am not getting anything. Could someone please help me ?
Here is my HTML form:
<form method="post" action="subb.php">
<div class="field half first">
<label for="Name">Name</label>
<input type="text" name="Name" id="name" />
</div>
<div class="field half">
<label for="Email">Email</label>
<input type="text" name="Email" id="email" />
</div>
<div class="field">
<label for="Message">Message</label>
<textarea name="Message" id="message" rows="5"></textarea>
</div>
<ul class="actions">
<button type "submit" name="submit" id="submit" class="button submit">Send message</button>
and the PHP:
<?php
$Name = $_POST['Name'];
$Email = $_POST['Email'];
$Message = $_POST['Message'];
$to = "combatstriker111#gmail.com";
$subject="new message";
mail($to , $subject , $Message, "From :" . $Name . $Email);
echo "Your message has been Sent";
?>
I have named the PHP file subb.php and listed them both in the same directories but its still not working for some reason. Any help is very much appreciated.
Something in your code was wrong mail($to , $subject , $Message, "From :" . $Name . $Email);
Mail function SYNTAX :
mail ( string $to , string $subject , string $message [, string $additional_headers [, string $additional_parameters ]] )
So,
<?php
if(isset($_POST['submit'])) {
$Name = $_POST['Name'];
$Email = $_POST['Email'];
$Message = "Name : ".$Name."<br />"
$Message .= $_POST['Message'];
$to = "combatstriker111#gmail.com";
$subject="new message";
// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Additional headers
$headers .= 'To: Name <$to>' . "\r\n";
$headers .= 'From: $Name <$Email>' . "\r\n";
$headers .= 'Cc: birthdayarchive#example.com' . "\r\n";
$headers .= 'Bcc: birthdaycheck#example.com' . "\r\n";
if(mail($to, $subject, $Message, $headers)) {
echo "Your message has been Sent";
} else {
echo "Mesage Error";
}
}
?>
Note : Use any mail library for prevent vulnerable to header injection like PHPMailer
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];;
$subject = $_POST['subject'];
$message = $_POST['message'];
$to = 'info#fullertoncomputerepairwebdesign.com';
$subject = 'Message From Website';
$headers = 'From: info#fullertoncomputerepairwebdesign.com' . "\r\n" .
'Reply-To: info#fullertoncomputerepairwebdesign.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
$themessage = "Name: ".$name."</br>Email: ".$email."</br>Phone: ".
$phone."</br>Subject: ".$subject.
"</br>Message: ".$message;
//echo $themessage;
if(mail($to, $subject, $themessage, $headers)){
echo "message sent";
header( 'Location: http://www.fullertoncomputerepairwebdesign.com/contactus.php?smresponse=Message Sent' ) ;
}else{
echo "we have a error charlie!";
}
;
test this out and if it doesn't work it means your hosting blocks mail function.
Recently I've been having problems with my PHP contact form. It's worked great for about two years, and I haven't changed anything, so I don't really understand what the problem is. Here's the code:
<?php
// Check for header injections
function has_header_injection($str) {
return preg_match ( "/[\r\n]/", $str );
}
if(isset ($_POST['contact_submit'])) {
$name = trim($_POST['name']);
$email = trim($_POST['email']);
$tel = trim($_POST['tel']);
$msg = $_POST['message'];
// check to see if name or email have header injections
if (has_header_injection($name) || has_header_injection($email)){
die();
}
if ( !$name || !$email || !$msg ) {
echo '<h4 class="error">All Fields Required</h4>Go back and try again';
exit;
}
// add the recipient email to a variable
$to = "example#example.net";
// Create a subject
$subject = "$name sent you an email";
// construct your message
$message .= "Name: $name sent you an email\r\n";
$message .= "Telephone: $tel\r\n";
$message .= "Email: $email\r\n\r\n";
$message .= "Message:\r\n$msg";
$message = wordwrap(message, 72);
// set the mail header
$headers = "MIME=Version: 1.0\r\n";
$headers .= "Content-type: text/plain; charset=iso-8859-1\r\n";
$headers .= "\r\nFrom: " . $name . " \r\n\r\n" . $tel . " \r\n\r\n " . $msg . "\r\n\r\n <" . $email . "> \r\n\r\n";
$headers .= "X-Priority: 1\r\n";
$headers .= "X-MSMail-Priority: high\r\n\r\n";
// Send the Email
mail( $to, $subject, $message, $headers );
?>
<!--- END PHP CONTACT FORM -->
<!-- Show Success message -->
<h2>Thanks for contacting Us!</h2>
<p align="center">Please allow 24 hours for a response</p>
<p>« Go to Home Page</p>
<?php } else { ?>
<form method="post" action="" id="contact-form">
<label for="name">Your Name</label>
<input type="text" id="name" name="name">
<label for="tel">Your Phone Number</label>
<input type="tel" id="tel" name="tel">
<label for="email">Your Email</label>
<input type="email" id="email" name="email">
<label for="message">the date/time you wish to sign up for</label>
<textarea id="message" name="message"></textarea>
<br>
<input type="submit" class="button next" name="contact_submit" value="Sign Up">
</form>
<?php } ?>
However, when the contact form is submitted, instead of sending the information to the body of the email, it sends it in the "From" section of the email. For example, the email might say:
To: Web Developer
From: Bob Smith 888-888-8888 mondays, wednesdays fridays
Subject: Bob Smith sent you an email!
Body:
X-Priority: 1X-MSMail-Priority: high
message
I don't really know what's going on, so any help would be appreciated!
You are adding all that info in the "from" header.
$headers .= "\r\nFrom: " . $name . " \r\n\r\n" . $tel . " \r\n\r\n " . $msg . "\r\n\r\n <" . $email . "> \r\n\r\n";
Change your headers to this:
$headers = "MIME=Version: 1.0\r\n";
$headers .= "Content-type: text/plain; charset=iso-8859-1\r\n";
$headers .= "From: {$name} <{$email}>\r\n"; // Removed all extra variables
$headers .= "X-Priority: 1\r\n";
$headers .= "X-MSMail-Priority: high\r\n";
and it should work.
You are already sending the $message, containing all the above data in the body as well.
Why you haven't experienced this before is however a mystery.
NOTE: You only need to have one \r\n after each header.
You should also change this row:
$message = wordwrap(message, 72);
to
$message = wordwrap($message, 72); // Adding $ in front of the variable.
So, I've been messing with this for a day now and I've read a lot tutorials and even posts on stackoverflow. I know it's possible to have all your php form processing steps on the same page as your form, but for some reason, my form won't send an email. Any help?
<div id="mc_embed_signup">
<form method="post" action="index.php" enctype="text/plain" name="emailform">
<input type="text" name="name" placeholder="First Name or Alias"><br>
<input type="text" name="email" placeholder="Email Address"><br>
<textarea name="message" rows="10" size="50" placeholder="Placeholder Text."></textarea><br>
<input type="submit" value="Submit →" name="submit" class="button round">
</form>
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = $_POST['email'];
$to = 'email#address.com'; //set to the default email address
$subject = 'Hello';
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
$headers = "From: $email" . "\r\n" .
"Reply-To: $email" . "\r\n" .
"X-Mailer: PHP/" . phpversion();
if($_POST['submit']) {
mail ($to, $subject, $body, $headers); //mail sends it to the SMTP server side which sends the email
echo "<p>Your message has been sent!</p>";
}
else {
echo "<p>Something went wrong, go back and try again!</p>";
}
?>
</div>
Remove the enctype="text/plain" that's a main factor.
Plus, you also need to check if fields were left empty using (if)empty().
Not doing so, you may receive emails that either do not contain the user's email, name, message or a combination of all.
Assuming this file is called index.php if not, use action=""
<div id="mc_embed_signup">
<form method="post" action="index.php" name="emailform">
<input type="text" name="name" placeholder="First Name or Alias"><br>
<input type="text" name="email" placeholder="Email Address"><br>
<textarea name="message" rows="10" size="50" placeholder="Placeholder Text."></textarea><br>
<input type="submit" value="Submit →" name="submit" class="button round">
</form>
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = $_POST['email'];
$to = 'email#example.com'; //set to the default email address
$subject = 'Hello';
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
$headers = "From: $email" . "\r\n" .
"Reply-To: $email" . "\r\n" .
"X-Mailer: PHP/" . phpversion();
if(isset($_POST['submit']) && !empty($_POST['name']) && !empty($_POST['email']) && !empty($_POST['message']) ){
// check if mail was sent
if(mail ($to, $subject, $body, $headers)){ //mail sends it to the SMTP server side which sends the email
echo "<p>Your message has been sent!</p>";
}
else {
echo "<p>Mail was not sent. Check your logs.</p>";
}
}
else {
echo "<p>Something went wrong, go back and try again!</p>";
}
?>
</div>
This might help,
if(isset($_POST['submit'])) {
if(empty($_POST[name]) && empty($_POST[email]) && empty($_POST[message])){
echo 'Fields cannot be empty';
} else{
$name = $_POST['name'];
$to = $_POST['email'];
$subject = "Hello";
$message = $_POST['message'];
$from = $_POST['email'];
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
mail ($to, $subject, $body);
}
}else{
echo "<p>Something went wrong, go back and try again!</p>";
}