PHP Sending content of textarea through email using 'a href' tag - javascript

For HTML code,
<textarea name="email_content" rows="6" placeholder="Write something"></textarea>
Send it
For PHP code,
<?php
$email="abcde#abc.com";
$head = "From: email\r\n";
$head .= "Content-type: text/html\r\n";
$title= "Title Test";
$body = "The text in the textarea";
$success = mail($email, $title, $body, $head);
?>
I want the text in the textarea to go to $body in PHP code.
Also, I want to send email in utf-8.
Please help!

You can't do that because you're not POSTing the data (you could do it if the email you send was always the same). Like #David said, though, you can make the submit button look like a hyperlink.
Here's the HTML markup:
<form method="POST" action="./something.php">
<textarea name="email_content" rows="6" placeholder="Write something"></textarea>
<input type="submit" class="hyperlink" value="Send it">
</form>
Here's the CSS styling:
.hyperlink {
background-color: transparent;
text-decoration: underline;
border: none;
color: blue;
cursor: pointer;
}
This is how it would look: http://jsfiddle.net/VKuEF/1/.
And in the something.php PHP script, to grab the value of the the textarea you'd do
$body = $_POST['email_content'];
And then use it to send the email. To use UTF-8 encoding for your email, you could add the Content-Type: text/html; charset=UTF-8 header to your message body.

Related

Report Form not submitting (php, html)

I'm creating a small website and I have a one question report form. The problem is the email isn't sending. I'm not sure what I did wrong.
I think there might be something wrong with the php, or maybe the html, I'm not the best with forms. Thanks for the help!
Here's my code:
html
<h2 style="font-size:30px;"><i class="fa fa-exclamation-circle"></i> Report</h2>
</div>
<hr class="descriptionline3">
<div class="modal-body3">
<form action="report.php" id="errorform" onsubmit = "return validate();">
<p style="color:#363636;text-align:left;padding-left:10px;">What seems to be the problem?</p>
<input type="text" class="forminputproblem" id="name" placeholder="Write the problem here..." style="height:20px;font-size:14px;border-radius:6px;display:inline-block;border:1px solid #ccc;padding: 12px 20px;
">
<div style="height:10px;"></div>
<div style="color:red;" id="error_message"></div>
<div style="color:green;" id="success_message"></div>
<div style="height:10px;"></div>
<input name="sumbit" type="submit" value="SEND REPORT">
</form>
php
<?php
if (isset($_POST['submit'])) {
$name = $_POST['name'];
$mailTo = "contact#email.com";
$headers = "Report Form Message";
$txt = "You have received a report form message: ".$name.".\n\n";
mail($mailTo, $txt, $headers);
header("Location: /index.html?reportsent");
}
?>
javascript
function validate(){
var name = document.getElementById("name").value;
var error_message = document.getElementById("error_message");
error_message.style.padding = "10px";
var text;
if(name.length < 10){
text = "✖ Report message has to be at least 10 characters!";
error_message.innerHTML = text;
text = "";
success_message.innerHTML = text;
return false;
}
text = "";
error_message.innerHTML = text;
text = "✔ Report has been submitted! We will get back to you as soon as possible.";
success_message.innerHTML = text;
return true;
}
Thanks!
By checking out the mail function's parameters...
mail($to, $subject, $message, $headers);
It seems like the problem is that your variables are a bit mixed up. Also, "header" doesn't refer to the subject: email headers contain info and have their own syntax. Try this code:
<?php
if (isset($_POST['submit'])) {
$name = $_POST['name'];
$mailTo = "contact#rileysunblockedgames.com";
$subject = "Report Form Message";
$headers = 'From: noreply#rileysunblockedgames.com' . "\r\n" .
'Reply-To: noreply#rileysunblockedgames.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
$txt = "You have received a report form message: ".$name.".\n\n";
mail($mailTo, $subject, $txt, $headers);
header("Location: /games.html?reportsent");
}
?>
Also, on your input, you have to define the "name" attribute for it to work properly:
<input type="text" class="forminputproblem" name="name" id="name" placeholder="Write the problem here..." style="height:20px;font-size:14px;border-radius:6px;display:inline-block;border:1px solid #ccc;padding: 12px 20px;">
Also also (and funny enough this is the biggest barrier in your way right now), there is a typo in your submit button's name, so your PHP code will never run:
<input name="submit" type="submit" value="SEND REPORT">

500 error on localhost when submitting form using AJAX and PHP

I'm fairly new to PHP and I'm having trouble sending test emails out from my local host.
I have a 3 field form where I'm hoping that I can have a user submit the form & be able to see a success message without the page refreshing. I get the error message that I have set in the code but for some reason, I'm unable to actually get the emails to send/have a success message appear.
Here's my code:
js
jQuery( document ).ready( function( $ ) {
var form = $('#ajax-contact');
var formMessages = $('#form-messages');
$(form).submit(function(e) {
e.preventDefault();
var formData = $(form).serialize();
$.ajax({
type: 'POST',
url: $(form).attr('action'),
data: formData
})
.done(function(response) {
$(formMessages).removeClass('error');
$(formMessages).addClass('success');
$(formMessages).text(response);
$('#name, #email, #number').val('');
})
.fail(function(data) {
$(formMessages).removeClass('success');
$(formMessages).addClass('error');
if (data.responseText !== '') {
$(formMessages).text(data.responseText);
} else {
$(formMessages).text('Oops! An error occured and your message could not be sent.');
}
});
});
});
mailer.php
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = strip_tags(trim($_POST["name"]));
$name = str_replace(array("\r","\n"),array(" "," "),$name);
$email = filter_var(trim($_POST["email"]), FILTER_SANITIZE_EMAIL);
$number = trim($_POST["number"]);
if ( empty($name) OR empty($number) OR !filter_var($email, FILTER_VALIDATE_EMAIL)) {
http_response_code(400);
echo "Sorry, there seems to be a problem submitting your details, please try again or contacts us";
exit;
}
$recipient = "example#example.co.uk";
$subject = "$name has given you their details to call them back";
$email_content = "
<html>
<style>
body {
background-color: white;
}
.email-container{
background-color:#c1c1c1;
max-width:400px;
margin:0 auto;
padding:30px 10px;
border-radius:10px;
border: 2px solid #515151;
}
</style>
<body>
<div class="email-container">
Dear example, <br />
<br />
A potential client has requested a call back. Their details are as follows: <br />
<br />
Name: $name\n <br />
Email: $email\n\n <br />
Telephone: \n$numer\n
</div>
</body>
</html>"
$email_headers = "From: $name <$email>";
if (mail($recipient, $subject, $email_content, $email_headers)) {
http_response_code(200);
echo "Thank You! Your message has been sent.";
} else {
http_response_code(500);
echo "Sorry, there seems to be a problem submitting your details, please try again or contacts us";
}
} else {
http_response_code(403);
echo "There was a problem with your submission, please try again.";
}
?>
HTML
<form class="requestcall-form" id="ajax-contact" method="post" action="mailer.php">
<input type="text" id="name" name="name" placeholder="Full Name" required>
<input type="text" id="email" name="email" placeholder="Email Address" required>
<input type="text" id="number" name="number" placeholder="Contact number" required>
<input class="submit" type="submit" name="submit" value="Submit">
</form>
<div id="form-messages"></div>
there is something wrong with how you output $email_content. String is not formatted correctly
check the <div class="email-container"> and change it to <div class='email-container'>
also, add a (;) after your $email_content
The other answer is correct, I thought I'd offer a better solution for dealing with large blocks of text, heredoc strings. No worrying about unescaped quotes ruining your day. Using this, your code would look like this:
...
$recipient = "example#example.co.uk";
$subject = "$name has given you their details to call them back";
$email_content = <<< HTML
<html>
<style>
body {
background-color: white;
}
.email-container{
background-color:#c1c1c1;
max-width:400px;
margin:0 auto;
padding:30px 10px;
border-radius:10px;
border: 2px solid #515151;
}
</style>
<body>
<div class="email-container">
Dear example, <br />
<br />
A potential client has requested a call back. Their details are as follows: <br />
<br />
Name: $name <br />
Email: $email <br />
Telephone: $numer
</div>
</body>
</html>
HTML;
$email_headers = "From: $name <$email>";
...

PHP and CKEditor included image does not show in the email

I have the following page to send newsletters.
page
When I include an image from the toolbar icon. It displays correctly realtime. But however when I click Send, email gets the message without the image. Just only the message (formatted text by the editor shows correctly)
My image is located in this URL: http://raveen.comlu.com/sport.jpg
Also I print content made by the editor after sending message as well;
Even there also, image does not show on the page(see screenshot)
I have the following code;
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title> A simple page with CKEditor </title>
<script src="ckeditor/ckeditor.js"> </script>
</head>
<body>
<form method="post" action="">
<!-- Create a <textarea> element first -->
<textarea name="editor1" id="editor1" rows="10" cols="80">
This is my textarea to be replaced with CKEditor.
</textarea>
<script>
//replace the <textarea id="editor1"> with a CKEditor instance
CKEDITOR.replace( 'editor1' );
//Retrieve data from CKEditor instance with ID of editor1
var data = CKEDITOR.instances.editor1.getData()
</script>
<input type="submit" name="btnSubmit" value="Send">
</form>
<?php
if( isset($_POST['btnSubmit']) ){
$editor_data = $_POST['editor1'];
echo $editor_data;
$to = "receiver1#yahoo.com, receiver2#gmail.com";
$subject = "Here is the subject";
$message = $editor_data;
$header = "From: Sender Name <senderemail#gmail.com> \r\n";
$header .= "MIME-Version: 1.0 \r\n";
$header .= "Content-type:text/html;charset=UTF-8 \r\n";
$retval = mail( $to, $subject, $message, $header );
if( $retval == true ){
echo "Message sent successfully";
}
else{
echo "Message could not be sent!";
}
}
?>
</body>
All of your quotes from your editor data are being escaped so you can try adding this line before echo $editor_data;
$editor_data = stripslashes($editor_data);

how to submit form and cc to an email filled in the field

I am writing a html form which will address to "project#abc.com" and want to cc this form to the "budget_email".
<SCRIPT language=JavaScript src="../script/common.js"></SCRIPT>
<META content="MSHTML 6.00.2800.1595" name=GENERATOR>
<form action="" method="POST">
<input name="recipient" value="project#abc.com">
<input name="Budget Email" type="text" id="budget_email">
<input type="submit" value="Submit Form">
</form>
To send an email with the data of the form to two email addresses, you need to catch your form submit server side by using for example PHP, and then process the data and send an email using either the build in mail() function (see here) or using a library like PHPmailer (here)
To set a CC using the mail() function, you need to add
$headers .= 'CC: email#domein.com' . "\r\n";
To the headers argument of mail().
An example full code would be:
<?php
$to = $_POST['recipient'];
$subject = "My subject";
$txt = "Hello SO!";
$headers = "From: webmaster#example.com" . "\r\n" .
"CC: ".$_POST['Budget Email']."\r\n";
mail($to,$subject,$txt,$headers);
?>

Custom Text To Autofill Textarea When DropDown Option Selected

I need some help with the autofill in the textarea when I select the drop down. I tried it and it works perfect on jsfiddle but when I upload the script on my web hosting nothing is happening. I don't know where I am wrong.
<?php
$subject = $_POST['EmailSubject'];
$message = $_POST['EmailBody'];
// Always set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
// More headers
$headers .= 'From: SocialDealers <admin#admin.com>' . "\r\n";
$emailList = explode("\n",$_POST['EmailList']);
if(count($emailList) > 0){
foreach($emailList as $to){
$to = trim($to);
$sent = mail($to,$subject,$message,$headers);
if ($sent){
echo "<p>Sent: $to</p>";
}
else{
echo "<p>Not Sent: $to</p>";
}
}
}
else{
echo "<p>No email addresses</p>";
}
?>
<html>
<head>
<script>
var contentToInsert = 'Hi Amey';
$( "#listbox" ).change(function() {
if ($( "#listbox" ).val() == '2') {
$("#EmailBody").html(contentToInsert);
} else {
$("#EmailBody").html("");
}
});
</script>
</head>
<center>
<form method="post">
<br><strong>PHP Email Sender</strong><br><br><br>
Email List<br>
<textarea name="EmailList" placeholder="email#email.com (New Email Each Line)" rows="20" cols="50"></textarea><br><br>
Subject<br>
<input type="text" name="EmailSubject" placeholder="Your Subject Goes Here"><br><br>
Select Automated HTML Content<br>
<select id="listbox">
<option id="option1">Select Offers...</option>
<option id="option2">1</option>
<option id="option3">2</option>
</select>
<br><br>
Body<br>
<textarea name="EmailBody" id="EmailBody" placeholder="Write your content (HTML Accepted)" rows="20" cols="50"></textarea><br><br>
<input type="submit" value="Submit!">
</form><br><br>
</center>
</html>
I don't know where i missed it but i tried all day to figure out the problem and i am not understanding anything. Also if it would be possible i would want the selection text to be from another text file.
Example:
The main file name would be mail.php
Option 1 Select's some text which would be in other file named abctext.txt
If it's working in Jsfiddle and not in your hosted environment , then you need to make sure that you referenced the JQuery library in your code.
Get the html code of your compiled page and check if there is a reference to the Jquery JS file

Categories