Send an email from a form - php

I am creating an assessment which will send results to the teacher and the student. I can't get the results to be sent to the student, only the teacher. Here is what I have below:
index.php
<form id="form1" name="form1" method="post" action="results.php" onsubmit="return validateForm() ;">
<label>First Name
<input type="text" name="fname" id="fname" tabindex="1" />
</label>
<label>Last Name
<input type="text" name="lname" id="lname" tabindex="2" />
</label>
<label>Title
<input type="text" name="title" id="title" tabindex="3" />
</label>
<br />
<br />
<hr />
<?php
$quiz = new Quiz( $questions );
echo $quiz->renderQuiz();
?>
<br/>
<label>Enter the email addresses you would like to receive your results, separated by semicolons.</label><br/>
<input type="text" name="email_to" tabindex="4" size="80" />
<input name="Submit" type="submit" value="Submit" />
</form>
results.php
$force_email = array( "test#test.com", test1#test.com);
function create_email_string( $force_email, $user_input )
{
if (!empty( $user_input ))
{
$email_string = $user_input . "; " . implode("; " , $force_email);
}
else
{
$email_string = implode("; " , $force_email);
}
return $email_string;
}
$ip = $_SERVER['REMOTE_ADDR']; // employee's Ip address
$time = date("d/m/y : H:i:s", time()); // current timestamp
$email_string = create_email_string( $force_email, $_POST['email_to'] );
$questions_correct = array();
$results_data_page = $_SERVER['SERVER_NAME'].$_SERVER["REQUEST_URI"];
$file_start = (strrpos($results_data_page, "/")+1);
$results_data_page = substr_replace($results_data_page, "results_data_page.php?test_name=" . rawurlencode($test_name), $file_start);
$variables = array();
$variables['fname'] = $_POST['fname'];
$variables['lname'] = $_POST['lname'];
$variables['test_name'] = $test_name;
$variables['score'] = $score;
$variables['table'] = create_results_table( $questions, $questions_correct );
$variables['results_data_page'] = $results_data_page;
$subject = $variables['fname'] . " " . $variables['lname'] . " results - " . $test_name;
$headers = "From: test#test.com\n";
$headers .= "Reply-To: test#test.com \n";
$headers .= "MIME-Version: 1.0 \n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1 \n";
$headers .= "Content-Transfer-Encoding: base64";
$message = getMessage( $variables );
$message = rtrim(chunk_split(base64_encode($message)));
foreach ($force_email as $to) {
mail($to, $subject, $message, $headers);
}

Related

Custom WP form which changes user role on submit

Basically I have a page which displays to a user only when they're logged in, it has a custom contact form on it. When the form is submitted, their user role should automatically change to "Author".
I literally have no idea where to start with this, if anyone has any ideas or can help me out a little that would be amazing because I'm really struggling with this.
EDIT
This is what I currently have, all it does at the minute is send emails.
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$name = trim(stripslashes($_POST['name']));
$email = trim(stripslashes($_POST['email']));
$subject = trim(stripslashes($_POST['subject']));
$message = trim(stripslashes($_POST['message']));
$email_from = $email;
$email_to = 'email#email.com';
$body = '<html><head></head><body>' .
'<b>Name:</b> ' . $name . '<br><br>' .
'<b>Email:</b> ' . $email . '<br><br>' .
'<b>Nature of Enquiry:</b> ' . $subject . '<br><br>' .
'<b>Message:</b> ' . nl2br($message) .
'</body></html>';
$headers = "From: " . strip_tags($email_from) . "\r\n";
$headers .= "Reply-To: ". strip_tags($email_from) . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
if( isset($_POST['checkbox']) ) {
mail($email_to, $subject, $body, $headers);
// Change user role to "author
echo '<p>You are now an author.</p>';
} else {
echo '<p>It appears you are a spambot, if this is a mistake please try again and check the "I am not a spambot" checkbox.</p>';
}
}
?>
<form method="post" action="#">
<label for="name">Name</label>
<input type="text" id="name" name="name" value="<?php echo $name; ?>" required>
<label for="email">Email Address</label>
<input type="email" id="email" name="email" value="<?php echo $email; ?>" required>
<label for="subject">Nature of Enquiry</label>
<input type="text" id="subject" name="subject" value="<?php echo $subject; ?>" required>
<label for="checkbox">I am not a spambot</label>
<input type="checkbox" id="checkbox" name="checkbox">
<label for="message">Message</label>
<textarea id="message" name="message" required><?php echo $message; ?></textarea>
<button type="submit">Submit</button>
</form>
This would be a matter of using the class WP_User and its set_role method.
$user_id = 2;
$role_name = 'author';
$user = new WP_User( $user_id );
$user->set_role( $role_name );

php contact form not sending mail after validation

I'm using a contact form for my website, which I validate and then email to myself, the validation is working correctly and it emails me if the user enters all the details correctly first time. However if the user enters incorrect data, then corrects it and hits send again, it won't send an email, below is the form and PHP code I have so far.
HTML code for contact form
<form action="contact.php" method="post">
<fieldset>
<label for="name">Name:<span class="star">*</span></label> <br />
<input type="text" name="name" id="name" placeholder="Enter your name" maxlength="50" required />
<label for="email">Email:<span class="star">*</span></label> <br />
<input type="email" name="email" id="email" placeholder="Enter your email address" maxlength="100" required />
<label for="number">Telephone: </label><input type="tel" name="number" id="number" placeholder="Enter your phone number" maxlength="12" />
<label for="message">Message:<span class="star">*</span></label>
<textarea name="message" id="message" placeholder="Enter your message" cols="54" rows="5" required></textarea>
<p class="small"><span class="star">*</span> Denotes a required field </p>
<input type="submit" id="send" name="send" value="Send" />
</fieldset>
</form>
PHP code for sending the form
function fix_string($var)
{
if(get_magic_quotes_gpc()) $var = stripslashes($var);
$var = strip_tags($var);
return $var;
}
{
$details = array('name' => fix_string($_POST['name']),
'email' => fix_string($_POST['email']),
'number' => fix_string($_POST['number']),
'message' => fix_string($_POST['message']));
}
$send = $_POST['send'];
$message = "";
foreach ($details as $field => $detail)
$message .= $field . ": " . $detail . "<br />";
$to = "smokey.12345#hotmail.co.uk";
$subject = "Website contact form";
$message = wordwrap($message, 70, "/r/n");
$headers = "From ". $details['email'];
function trim_value(&$value)
{
$value = trim($value);
}
array_walk($details, 'trim_value');
if ($send)
{
foreach ($details as $field => $detail)
{
if (empty($detail) && $field!='number')
echo "<p class='error'>Please fill in the required field: " . ucfirst($field) . "<br /></p>";
}
}
else
{
mail($to, $subject, $message, $headers);
echo "<p class='success'>Mail was sent successfully</p>";
}
EDIT
In order for the code to work on the same page, you need to set the action to action=""
Otherwise, you need to use two pages. One for your form and one for contact.php which is your handler. I suggest you use two pages, but here is a version that will work inside one page.
<?php
if(isset($_POST['send'])) {
function fix_string($var)
{
if(get_magic_quotes_gpc()) $var = stripslashes($var);
$var = strip_tags($var);
return $var;
}
$details = array('name' => fix_string($_POST['name']),
'email' => fix_string($_POST['email']),
'number' => fix_string($_POST['number']),
'message' => fix_string($_POST['message']));
function trim_value(&$value)
{
$value = trim($value);
}
array_walk($details, 'trim_value');
foreach ($details as $field => $detail)
{
if (empty($detail) && $field!='number')
die("<p class='error'>Please fill in the required field: " . ucfirst($field) . "<br /></p>");
}
$message = "";
foreach ($details as $field => $detail)
$message .= $field . ": " . $detail . "\n";
$send = $_POST['send'];
$email = $_POST['email'];
$to = "smokey.12345#hotmail.co.uk";
$subject = "Website contact form";
$headers = "From: $email" . "\r\n" .
"Reply-To: $email" . "\r\n" .
"X-Mailer: PHP/" . phpversion();
mail($to, $subject, $message, $headers);
echo "<p class='success'>Mail was sent successfully</p>";
exit;
} // closing brace for if(isset($_POST['send'])) {
?>
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<form action="" method="post">
<fieldset>
<label for="name">Name:<span class="star">*</span></label> <br />
<input type="text" name="name" id="name" placeholder="Enter your name" maxlength="50" />
<label for="email">Email:<span class="star">*</span></label> <br />
<input type="email" name="email" id="email" placeholder="Enter your email address" maxlength="100" />
<label for="number">Telephone: </label><input type="tel" name="number" id="number" placeholder="Enter your phone number" maxlength="12" />
<label for="message">Message:<span class="star">*</span></label>
<textarea name="message" id="message" placeholder="Enter your message" cols="54" rows="5" ></textarea>
<p class="small"><span class="star">*</span> Denotes a required field </p>
<input type="submit" id="send" name="send" value="Send" />
</fieldset>
</form>
</body>
</html>
Original answer
This line is not properly formatted.
$message = wordwrap($message, 70, "/r/n");
change it to:
$message = wordwrap($message, 70, "\r\n");
You need to use \ instead of /
EDIT
The only way I could get your form to work, is to add a die function.
Try this now:
<?php
function fix_string($var)
{
if(get_magic_quotes_gpc()) $var = stripslashes($var);
$var = strip_tags($var);
return $var;
}
$details = array('name' => fix_string($_POST['name']),
'email' => fix_string($_POST['email']),
'number' => fix_string($_POST['number']),
'message' => fix_string($_POST['message']));
function trim_value(&$value)
{
$value = trim($value);
}
array_walk($details, 'trim_value');
foreach ($details as $field => $detail)
{
if (empty($detail) && $field!='number')
die("<p class='error'>Please fill in the required field: " . ucfirst($field) . "<br /></p>");
}
$message = "";
foreach ($details as $field => $detail)
$message .= $field . ": " . $detail . "\n";
$send = $_POST['send'];
$email = $_POST['email'];
$to = "smokey.12345#hotmail.co.uk";
$subject = "Website contact form";
$headers = "From: $email" . "\r\n" .
"Reply-To: $email" . "\r\n" .
"X-Mailer: PHP/" . phpversion();
mail($to, $subject, $message, $headers);
echo "<p class='success'>Mail was sent successfully</p>";
exit;
?>

$_POST doesnt work

I have a simple contact me script, which fires an email on submit.
The email is sent, but there are no form values in the email (name: empty, ...).
Why doesn't my contact script work?
<?php
if ($action == "send") //isset wyslij
{
if (!$_POST[name] || !$_POST[email] || !$_POST[phone] || !$_POST[enquiry])
{
$problem = TRUE;
echo("<p>You have to fill all form.</p>");
}
if (! $problem)
{
$data = date("d.m.y");
$message = "
<p>Name: $_POST[name]</p>
<p>Phone: $_POST[phone]</p>
<p>Email: $_POST[email]</p>
<br>
<p>Enquiry: $_POST[enquiry]</p>";
$od = "contactmail#asdasdas.com";
$content = $message;
$header = "From: $od \r\n";
$header .= 'MIME-Version: 1.0' . "\r\n";
$header .= 'Content-type: text/html; charset=UTF-8' . "\r\n";
(mail('email#example.com', 'New message from website', $content, $header));
echo("<br><p>Message has been sent.</p>");
}
else
{
echo("<p>Try <a href=contact.php>again</a></p>");
}
}
?>
<form action="contact.php?action=send" method="post" enctype="text/plain">
<label for="name">Name</label><input type="text" name="name" /></br></br>
<label for="email">Email</label><input type="text" name="email" /></br></br>
<label for="phone">Phone</label><input type="text" name="phone" /></br></br>
<label for="enquiry">Enquiry</label><textarea name="enquiry" cols="20" rows="10"></textarea></br></br>
<input type="submit" id="contact_button" value="Send" />
</form>
Valid values for enctype in tag are:
application/x-www-form-urlencoded
multipart/form-data
you have text/plain and thats why it's not working.
simply remove the "enctype" argument from the HTML form code and try again?

Box slider after 1 min

working when you click "send" email to come forward with a box which read "thanks for the information." and after the box slideup again after 1min.
when I click on "send" and then it just down but the slider is not up in some manner whatsoever.
<div class="kontakt_box">
<h3>Kontakt Mads Ørnfeldt Andersen</h3>
<p>Kom i kontakt med Mads Ørnfeldt Andersen</p>
<form action="#" method="post" name="kontakt_box">
<label>Navn<br /><input type="text" name="navn" required title="Angive et Navn" class="new" placeholder="Navn"></label><br />
<label>Efternavn<br /><input type="text" required title="Angive et efternavn" name="efternavn" class="new" placeholder="Efternavn"></label><br />
<label>Email<br /><input type="email" required title="Angive en email!" name="email" class="new" placeholder="Email"></label><br />
<label>Tekst<br /><textarea name="tekst" cols="35" rows="10" class="new" placeholder="Tekst til email"></textarea></label><br />
<label><input type="submit" name="send" value="Send" class="new send_email"></label>
</form>
<?php
if(isset($_POST["send"]))
{
$email_afsender = $mebe->email_afsender();
}
if($_SESSION["email_sendt"] == true)
{
echo "<div class=\"tak_email\">Tak</div>";
}
?>
</div>
<script>
$('.send_email').click(function() {
$('.tak_email').slideUp('slow', function() {
// Animation complete.
});
});
</script>
here are im my function:
function email_afsender()
{
$_SESSION["email_sendt"] = true;
$fra = $_POST["navn"] . " " . $_POST["efternavn"] . "<" . $_POST["email"] . ">";
$til = "jp#absb.dk";
$emne = "Kontakt blabla.dk - " . $_POST["navn"] . " " . $_POST["efternavn"];
$besked = $_POST["tekst"];
//headers
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=UTF-8\r\n";
$headers .= "From: " . $fra . "\r\n";
mail($til, $emne, $besked, $headers);
$TargetPage = "/kontakt/";
header('Location: ' . $TargetPage);
}
Your code is not valid. You did not specify the action file in your form and you are going to submit it??

Hide web page if no variable present in the url

I have created a form that pulls a variable from a url using the following code:
<?php $id = $_GET['id']; echo $id; ?>
Using this is link as an example (http://www.mydomain.co.uk/customerform.php?id=12) it would display 12 on the webpage.
What I would like to know, is it possible to stop the web page from loading if there is no varible in the URL e.g ?id=12 ?
Either in php, javascript?
So If you visited the following link, the web page would display:
http://www.mydomain.co.uk/customerform.php?id=12
and if you visited this link (without the variable), the page would not display:
http://www.mydomain.co.uk/customerform.php
Thank you for any help.
Update below...
I have added Krishnanunni's code and it successfully prevents the web page from displaying if the is no vartiable in the url. But adding this code prevents the form submitting...
Is there a way of keeping the url variable when the form is submitted?
Can anyone help? Thanks
<body id="main_body" >
<div id="form_container">
<?
/* only load page is variable is presnt in the URL */
if (!isset($_GET['id']) || empty($_GET['id']))
{
die("Bad Request");
}
?>
<?
/* Mailer with Attachments */
$action = $_REQUEST['action'];
global $action;
function showForm() {
?>
<form id="contact" class="appnitro" name="idform" enctype="multipart/form-data" method="post" action="<?=$_SERVER['PHP_SELF']?>">
<input type="hidden" name="action" value="send" />
<input type="hidden" name="MAX_FILE_SIZE" value="10000000"/>
<input name="to_email" type="hidden" id="to_email" value="myemail#domain.co.uk"/>
<div>
<input id="invoice" name="invoice" class="element text medium" type="hidden" maxlength="255" value="<?php $id = $_GET['id']; echo $id; ?>"/>
</div>
<div>
<input id="subject" name="subject" class="element text medium" type="hidden" maxlength="255" value="Enquiry Form - (Invoice: <?php $id = $_GET['id']; echo $id; ?>)"/>
</div>
<label class="description" for="from_name">Name </label>
<div>
<input id="from_name" name="from_name" class="element text medium" type="text" maxlength="255" value=""/>
</div>
<label class="description" for="position">Position </label>
<div>
<input id="position" name="position" class="element text medium" type="text" maxlength="255" value=""/>
</div>
<label class="description" for="uemail"> Email </label>
<div>
<input id="email" name="email" class="element text medium" type="text" maxlength="255" value=""/>
</div>
<label class="description" for="phone"> Phone </label>
<div>
<input id="phone" name="phone" class="element text medium" type="text" maxlength="255" value=""/>
</div>
<input name="Reset" type="reset" class="contactform_small" value="Reset" />
<input name="Submit2" type="submit" class="contactform_small" id="Submit" value="Submit" />
</form>
<?
}
function sendMail() {
if (!isset ($_POST['to_email'])) { //Oops, forgot your email addy!
die ("<p>Oops! You forgot to fill out the email address! Click on the back arrow to go back</p>");
}
else {
$to_name = "MY BUSINESS";
$from_name = stripslashes($_POST['from_name']);
$subject = Trim(stripslashes($_POST['subject']));
$to_email = $_POST['to_email'];
$EmailFrom = Trim(stripslashes($_POST['email']));
// get posted data into local variables
$position = Trim(stripslashes($_POST['position']));
$position2 = Trim(stripslashes($_POST['position2']));
$phone = Trim(stripslashes($_POST['phone']));
$invoice = Trim(stripslashes($_POST['invoice']));
//Let's start our headers
$headers = "From: $EmailFrom<" . $_POST['email'] . ">\n";
$headers .= "Reply-To: <" . $_POST['email'] . ">\n";
$headers .= "MIME-Version: 1.0\n";
$headers .= "Content-Type: multipart/related; type=\"multipart/alternative\"; boundary=\"----=MIME_BOUNDRY_main_message\"\n";
$headers .= "X-Sender: $from_name<" . $_POST['email'] . ">\n";
$headers .= "X-Mailer: PHP4\n";
$headers .= "X-Priority: 3\n"; //1 = Urgent, 3 = Normal
$headers .= "Return-Path: <" . $_POST['email'] . ">\n";
$headers .= "This is a multi-part message in MIME format.\n";
$headers .= "------=MIME_BOUNDRY_main_message \n";
$headers .= "Content-Type: multipart/alternative; boundary=\"----=MIME_BOUNDRY_message_parts\"\n";
$message = "------=MIME_BOUNDRY_message_parts\n";
$message .= "Content-Type: text/plain; charset=\"iso-8859-1\"\n";
$message .= "Content-Transfer-Encoding: quoted-printable\n";
$message .= "\n";
/* Add our message, in this case it's plain text. You could also add HTML by changing the Content-Type to text/html */
$message .= "Invoice: ";
$message .= $invoice;
$message .= "\n";
$message .= "\n";
$message .= "My Business Enquiry Form";
$message .= "\n";
$message .= "\n";
$message .= "Contact Details: ";
$message .= "\n";
$message .= "\n";
$message .= "Name: ";
$message .= $from_name;
$message .= "\n";
$message .= "Position: ";
$message .= $position;
$message .= "\n";
$message .= "Email: ";
$message .= $EmailFrom;
$message .= "\n";
$message .= "Phone: ";
$message .= $phone;
$message .= "\n";
$message .= "\n";
// send the message
mail("$to_name<$to_email>", $subject, $message, $headers);
print "Form sent successfully";
}
}
?>
<?
switch ($action) {
case "send":
sendMail();
showForm();
break;
default:
showForm();
}
?>
<div id="footer">
</div>
</div>
</body>
Start your php page with this code
if (!isset($_GET['id']) || empty($_GET['id']))
{
die("Bad Request");
}
This should solve your problem

Categories