Have got a simple HTML form with file upload field:
<form action="<?php bloginfo('template_url'); ?>/demo-contacts.php" method="post" id="sky-form" class="sky-form" enctype="multipart/form-data">
<header>Submit an <strong>Application!</strong></header>
<fieldset>
<div class="row">
<section class="col col-6">
<label class="label">Name</label>
<label class="input"> <i class="icon-append icon-user"></i>
<input type="text" name="name" id="name">
</label>
</section>
<section class="col col-6">
<label class="label">E-mail</label>
<label class="input"> <i class="icon-append icon-envelope-alt"></i>
<input type="email" name="email" id="email">
</label>
</section>
</div>
<div class="row">
<section class="col col-6">
<label class="label">Telephone Number</label>
<label class="input"> <i class="icon-append icon-phone"></i>
<input type="text" name="telephone" id="telephone">
</label>
</section>
<section class="col col-6">
<label class="label">Address</label>
<label class="input"> <i class="icon-append icon-envelope-alt"></i>
<input type="text" name="address" id="address">
</label>
</section>
</div>
<section>
<label class="label">Subject</label>
<label class="input"> <i class="icon-append icon-tag"></i>
<input type="text" name="subject" id="subject" value="Technician Application">
</label>
</section>
<section>
<label class="label">Covering Letter</label>
<label class="textarea"> <i class="icon-append icon-comment"></i>
<textarea rows="4" name="message" id="message"></textarea>
</label>
</section>
<div class="row">
<section class="col col-6">
<label class="label">Attach your CV</label>
<label class="input"> <i class="icon-append icon-file"></i>
<input type="file" name="cv" id="cv">
</label>
</section>
</div>
<input type="hidden" name="recipient" value="<?php the_field('contact_form_recipient'); ?>">
</fieldset>
<footer>
<button type="submit" class="button">Send</button>
</footer>
<div class="message"> <i class="icon-ok"></i>
<p>Your message was successfully sent!</p>
</div>
</form>
And have the following PHP to process the form which worked well prior to adding file upload field:
$EmailFrom = "email address here";
$EmailTo = Trim(stripslashes($_POST['recipient']));
$Name = Trim(stripslashes($_POST['name']));
$Subject = Trim(stripslashes($_POST['subject']));
$Email = Trim(stripslashes($_POST['email']));
$Telephone = Trim(stripslashes($_POST['telephone']));
$Address = Trim(stripslashes($_POST['address']));
$Message = Trim(stripslashes($_POST['message']));
$CV = Trim(stripslashes($_POST['cv']));
// prepare email body text
$Body = "";
$Body .= "Name: ";
$Body .= $Name;
$Body .= "\n";
$Body .= "Email: ";
$Body .= $Email;
$Body .= "\n";
$Body .= "Telephone: ";
$Body .= $Telephone;
$Body .= "\n";
$Body .= "Address: ";
$Body .= $Address;
$Body .= "\n";
$Body .= "Message: ";
$Body .= $Message;
$Body .= "\n";
$Body .= "CV: ";
$Body .= $CV;
$Body .= "\n";
// send email
$success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");
// redirect to success page
if ($success){
print "<meta http-equiv=\"refresh\" content=\"0;URL= URL here\">";
}
else{
echo "failed";
}
At present, the e-mail that comes through just states the name of the file that's been uploaded, i'd like to have the file attached to the email, could someone guide me please?
Thanks
Related
I have two html forms that both send emails successfully using PHP. However, one form echo successful submission text, but the other one does not.
Here is the form that is part of the index.html page, which does not echo any text after submission. Please note, the email still sends:
<form class="booking-form" id="myForm" action="#">
<div class="row">
<div class="col-lg-12 d-flex flex-column">
<input name="name" placeholder="Your name" onfocus="this.placeholder = ''" onblur="this.placeholder = 'Your Name'" class="form-control mt-20" required="" type="text">
</div>
<div class="col-lg-6 d-flex flex-column">
<input name="subject" placeholder="subject" onfocus="this.placeholder = ''" onblur="this.placeholder = 'subject'" class="form-control mt-20" required="" type="text">
</div>
<div class="col-lg-6 d-flex flex-column">
<input name="email" placeholder="Email" onfocus="this.placeholder = ''" onblur="this.placeholder = 'Email'" class="form-control mt-20" required="" type="email">
</div>
<div class="col-lg-12 flex-column">
<textarea rows="5" class="form-control mt-20" name="message" placeholder="Messege" onfocus="this.placeholder = ''" onblur="this.placeholder = 'Messege'" required=""></textarea>
</div>
<div class="col-lg-12 d-flex justify-content-end send-btn">
<button type="submit" class="genric-btn primary mt-20 text-uppercase ">Send</button>
</div>
</div>
</form>
The following is the PHP mailer code:
<?php
$to = 'myEmail#gmail.com';
$firstname = $_POST["name"];
$email= $_POST["email"];
$text= $_POST["message"];
$subject= $_POST["subject"];
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= "From: Email coming from Brave Towing " . $email . "\r\n"; // Sender's E-mail
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$message = "
<html>
<head>
</head>
<body style=\"background-color:#fafafa;\">
<div style=\"padding:20px;\">
Customer Email: <span style=\"color:#888\">$email</span>
<br>
Contact Subject: <span style=\"color:#888\">$subject</span>
<br>
Contact Message: <div style=\"color:#888\">$text</div>
</div>
</body>
</html>
";
if (#mail($to, $email, $message, $headers))
{
echo '<h1>The message has been sent</h1>';
}else{
echo 'failed';
}
?>
I have created a simple Html page index.html where the user can submit a message. An additional PHP file email_form.php is created to capture the form submissions and send the form contents to my email address.
index.html:
<form name="contactform" method="POST" action="email_form.php">
<div class="form-group">
<div class="form-row">
<div class="col" id="prenom">
<input type="text" class="form-control" placeholder="Prénom" required="True" name="prenom">
</div>
<div class="col" id="nom">
<input type="text" class="form-control" placeholder="Nom" required="True" name="nom">
</div>
</div>
<div class="col" id="age">
<input type="number" class="form-control" placeholder="Age" required="True" name="age">
</div>
<div class="form-row">
<div class="col" id="tel">
<input type="tel" class="form-control" placeholder="Téléphone" name="tel">
</div>
<div class="col" id="mail">
<input type="email" class="form-control" placeholder="Email" name="email">
</div>
</div>
<textarea class="form-control" id="exampleFormControlTextarea1" rows="3" placeholder="Your message" name="message"></textarea>
<div class="row">
<div class="col-xs-6" id="button">
<button type="submit" name="submit" class="btn btn-primary">Send</button>
</div>
</div>
</div>
</form>
email_form.php:
<?php
if(isset($_POST['submit'])) {
$email_to = "info#mysite.com";
$email_subject = "Email subject";
$prenom = $_POST['prenom'];
$nom = $_POST['nom'];
$age = $_POST['age'];
$tel = $_POST['tel'];
$email = $_POST['email'];
$message = $_POST['message'];
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message .= "Prénom: ".clean_string($prenom)."\n";
$email_message .= "Nom: ".clean_string($nom)."\n";
$email_message .= "Age: ".clean_string($age)."\n";
$email_message .= "Telephone: ".clean_string($tel)."\n";
$email_message .= "Message: ".clean_string($message)."\n";
// create email headers
$headers = "From: ".$email."\n\n";
mail($email_to, $email_subject, $email_message, $headers);
header("Location: index.html");
}
?>
On the website, when I try to submit a message, it redirects me to a link looking like this: mysite.com/email_form.php with an error saying "page not found". I don't know much about PHP and I can't tell what I'm doing wrong.
First of all you should have a closing form tag </form>
Then:
Paste the php code to the top of your HTML file and
Change the file's extension to .php
Change the form's action to index.php
Your index.php should look something like this:
<?php
if(isset($_POST['submit'])) {
$email_to = "info#mysite.com";
$email_subject = "Email subject";
$prenom = $_POST['prenom'];
$nom = $_POST['nom'];
$age = $_POST['age'];
$tel = $_POST['tel'];
$email = $_POST['email'];
$message = $_POST['message'];
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message .= "Prénom: ".clean_string($prenom)."\n";
$email_message .= "Nom: ".clean_string($nom)."\n";
$email_message .= "Age: ".clean_string($age)."\n";
$email_message .= "Telephone: ".clean_string($tel)."\n";
$email_message .= "Message: ".clean_string($message)."\n";
// create email headers
$headers = "From: ".$email."\n\n";
mail($email_to, $email_subject, $email_message, $headers);
header("Location: index.html");
}
?>
<form name="contactform" method="POST" action="index.php">
<div class="form-group">
<div class="form-row">
<div class="col" id="prenom">
<input type="text" class="form-control" placeholder="Prénom" required="True" name="prenom">
</div>
<div class="col" id="nom">
<input type="text" class="form-control" placeholder="Nom" required="True" name="nom">
</div>
</div>
<div class="col" id="age">
<input type="number" class="form-control" placeholder="Age" required="True" name="age">
</div>
<div class="form-row">
<div class="col" id="tel">
<input type="tel" class="form-control" placeholder="Téléphone" name="tel">
</div>
<div class="col" id="mail">
<input type="email" class="form-control" placeholder="Email" name="email">
</div>
</div>
<textarea class="form-control" id="exampleFormControlTextarea1" rows="3" placeholder="Your message" name="message"></textarea>
<div class="row">
<div class="col-xs-6" id="button">
<button type="submit" name="submit" class="btn btn-primary">Send</button>
</div>
</div>
</div>
</form>
I have resolved the problem by changing headerin email_form.php. Now it looks like this:
header("Location: https://example.com");
And it works fine.
I am currently trying to send an HTML contact form or the contents of it using php mail () function. Just hang up and ask for help.
How can I e.g. Your entered text and the email address specified in the contact form via PHP?
So my code looks currently and it works .. Only I do not know how I the data entered and send it with ..
HTML CODE:
<form class="" action="senden.php" method="post">
<div class="">
<label>Name</label>
<input type="text" name="von" placeholder="Bitte geben Sie Ihren Namen ein" value="">
<label>E-Mail Adresse</label>
<input type="text" name="email" placeholder="Bitte geben Sie Ihre Email-Adresse ein" value="">
<label>Telefonnummer</label>
<input type="tel" name="tel" placeholder="Bitte geben Sie Ihre Telefonnummer ein" value="">
</div>
<div class="">
<label>Nachricht</label>
<textarea placeholder="Bitte geben Sie Ihre Nachricht ein" name="nachricht" rows="8" cols="40"></textarea>
<button type="submit" name="submit" value="">Nachricht Abschicken</button>
</div>
</form>
`
PHP CODE:
<?php
$empfaenger = "mymail#gmail.com";
$betreff = "Mail Function";
$from = "From: Max Reimers <absender#domain.de>";
$text = "Email Email Email Email Text Text Text";
mail($empfaenger, $betreff, $text, $from);
?>
Here is Code:
<form class="form-horizontal" action="" id="contact-form" method="post">
<fieldset>
<legend class="text-center colheader">Email Us</legend>
<div class="form-group">
<div class="col-md-10 col-md-offset-1">
<input id="name" name="name" type="text" placeholder="Full Name" class="form-control" required>
</div>
</div>
<div class="form-group">
<div class="col-md-10 col-md-offset-1">
<input id="email" name="email" type="text" placeholder="Email Address" class="form-control" required>
</div>
</div>
<div class="form-group">
<div class="col-md-10 col-md-offset-1">
<input id="phone" name="phonenumber" type="text" placeholder="Phone" class="form-control" required>
</div>
</div>
<div class="form-group">
<div class="col-md-10 col-md-offset-1">
<input id="subject" name="subject" type="text" placeholder="Subject" class="form-control">
</div>
</div>
<div class="form-group">
<div class="col-md-10 col-md-offset-1">
<textarea class="form-control" id="message" name="messag" placeholder="Enter your massage for us here. We will get back to you within 2 business days." rows="7" required></textarea>
</div>
</div>
<div class="form-group">
<div class="col-md-12 text-center">
<button type="submit" name="submit" class="btn btn-primary btn-lg">Submit</button>
</div>
</div>
</fieldset>
</form>
<?php
if(isset($_POST['submit'])) {
$to = 'ourmail#example.com';
$from = $_POST['email'];
$subject ='Adlivetech Contact Form';
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phonenumber'];
$subject = $_POST['subject'];
$messag = $_POST['messag'];
$message = '<html><body>';
$message .='<table>';
$message .='<tr><td>Name:</td><td>'. $name .'</td></tr>';
$message .='<tr><td>Email:</td><td>'. $email .'</td></tr>';
$message .='<tr><td>Phone:</td><td>'. $phone .'</td></tr>';
$message .='<tr><td>Subject:</td><td>'. $subject .'</td></tr>';
$message .='<tr><td>Message:</td><td>'. $messag .'</td></tr>';
$message .='</table>';
$message .='</body></html>';
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= "To: {$to}\r\n";
$headers .= "From: {$name} <{$from}>\r\n";
$headers .= "Reply-To: <{$to}>\r\n";
$headers .= "Subject: {$subject}\r\n";
$headers .= "X-Mailer: PHP/".phpversion()."\r\n";
mail($from, $subject, $message, $headers);
}
?>
So I have a from that basically when it submits it runs through what i have on "contact-process.php" and when clicking on submit it will go to that page and then show the success or error, how do I make the submit show the success or error message on the same page and clear the form to show it's sent. here's the code I have on my web page.
<form class="form-horizontal" method="post" action="contact-process.php">
<div class="form-group">
<label for="name" class="col-sm-2 control-label">Name</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="name" name="name" placeholder="First & Last Name">
</div>
</div>
<div class="form-group">
<label for="email" class="col-sm-2 control-label">Email</label>
<div class="col-sm-10">
<input type="email" class="form-control" id="email" name="email" placeholder="example#gmail.com">
</div>
</div>
<div class="form-group">
<label for="email" class="col-sm-2 control-label">Subject</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="subject2" name="subject2" placeholder="Logo Design - Request">
</div>
</div>
<div class="form-group">
<label for="message" class="col-sm-2 control-label">Message</label>
<div class="col-sm-10">
<textarea class="form-control" rows="4" name="message" placeholder="Please explain your request in detail. Provide screenshots & links."></textarea>
</div>
</div>
<div class="form-group">
<label for="human" class="col-sm-2 control-label">5 x 2 = ?</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="human" name="human" placeholder="Your Answer">
</div>
</div>
<div class="form-group">
<div class="col-sm-10 col-sm-offset-2">
<input type="submit" class="btn-primary btn" id="submit" name="submit" value="Send">
</div>
</div>
</form>
And then this is the code I have on "contact-process.php"
<?php
if(isset($_POST['submit'])){
$name = $_POST["name"];
$email = $_POST["email"];
$subject2 = $_POST["subject2"];
$message = $_POST["message"];
$human = intval($_POST['human']);
$EmailTo = "example#outlook.com";
$Subject = "Message Received";
if ($human !== 10) {
$errHuman = 'Your anti-spam is incorrect';
}
// prepare email body text
$Body = "Name: ";
$Body .= $name;
$Body .= "\n";
$Body .= "Email: ";
$Body .= $email;
$Body .= "\n\n\n";
$Body .= "Subject: ";
$Body .= $subject2;
$Body .= "\n";
$Body .= "Message: ";
$Body .= $message;
$Body .= "\n";
// send email
$success = mail($EmailTo, $Subject, $Body, "From:".$email);
// redirect to success page
if ($success){
echo "<div class='alert alert-success'>Thank You! I will be in touch</div>";
}else{
echo "<div class='alert alert-danger'>Sorry there was an error sending your message. Please try again later.</div>";
}
}
?>
Put the html to your PHP script as following:
<form class="form-horizontal" method="post" action="#">
<div class="form-group">
<label for="name" class="col-sm-2 control-label">Name</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="name" name="name" placeholder="First & Last Name">
</div>
</div>
<div class="form-group">
<label for="email" class="col-sm-2 control-label">Email</label>
<div class="col-sm-10">
<input type="email" class="form-control" id="email" name="email" placeholder="example#gmail.com">
</div>
</div>
<div class="form-group">
<label for="email" class="col-sm-2 control-label">Subject</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="subject2" name="subject2" placeholder="Logo Design - Request">
</div>
</div>
<div class="form-group">
<label for="message" class="col-sm-2 control-label">Message</label>
<div class="col-sm-10">
<textarea class="form-control" rows="4" name="message" placeholder="Please explain your request in detail. Provide screenshots & links."></textarea>
</div>
</div>
<div class="form-group">
<label for="human" class="col-sm-2 control-label">5 x 2 = ?</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="human" name="human" placeholder="Your Answer">
</div>
</div>
<div class="form-group">
<div class="col-sm-10 col-sm-offset-2">
<input type="submit" class="btn-primary btn" id="submit" name="submit" value="Send">
</div>
</div>
</form>
<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
if($_POST["name"] != "" AND $_POST["email"] != "" AND $_POST["subject2"] != "" AND $_POST["message"] != "") {
$name = $_POST["name"];
$email = $_POST["email"];
$subject2 = $_POST["subject2"];
$message = $_POST["message"];
$human = intval($_POST['human']);
$EmailTo = "example#outlook.com";
$Subject = "Message Received";
if ($human !== 10) {
echo "<div class='alert alert-danger'>Your anti-spam is incorrect</div>";
}else{
// prepare email body text
$Body = "Name: ";
$Body .= $name;
$Body .= "\n";
$Body .= "Email: ";
$Body .= $email;
$Body .= "\n\n\n";
$Body .= "Subject: ";
$Body .= $subject2;
$Body .= "\n";
$Body .= "Message: ";
$Body .= $message;
$Body .= "\n";
// send email
$success = mail($EmailTo, $Subject, $Body, "From:".$email);
// redirect to success page
if ($success){
echo "<div class='alert alert-success'>Thank You! I will be in touch</div>";
}else{
echo "<div class='alert alert-danger'>Sorry there was an error sending your message. Please try again later.</div>";
}
}
}else{
echo "<div class='alert alert-success'>Data incomplate</div>";
}
}else{
// do nothing
}
?>
EDIT
action will be:
action="<?php echo $_SERVER['PHP_SELF']; ?>"
OR
action="#"
OR
action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>"
Seek ye first ... then asks
You need to know a little Ajax is a technology that allows you to communicate with php javascript / JSON
Example:
https://scotch.io/tutorials/submitting-ajax-forms-with-jquery
Make a script with alert function then inside the script put location.reload in the success part.
For some reason name, email and message appear empty when I receive an email.
HTML
<form method="post" action="send.php" class="form-horizontal">
<fieldset>
<!-- Text input-->
<div class="form-group">
<label class="col-md-4 control-label" for="name">Name</label>
<div class="col-md-4">
<input id="name" name="name" type="text" class="form-control input-md" required="">
</div>
</div>
<!-- Text input-->
<div class="form-group">
<label class="col-md-4 control-label" for="email">Email</label>
<div class="col-md-4">
<input id="email" name="email" type="email" class="form-control input-md" required="">
</div>
</div>
<!-- Textarea -->
<div class="form-group">
<label class="col-md-4 control-label" for="message">Message</label>
<div class="col-md-4">
<textarea class="form-control" id="message" name="message"></textarea>
</div>
</div>
<!-- Button -->
<div class="form-group">
<label class="col-md-4 control-label" for="submit"></label>
<div class="col-md-4">
<button id="submit" name="submit" type="submit" value="Submit" class="btn btn-outline-inverse btn-lg">Submit</button>
</div>
</div>
</fieldset>
</form>
PHP Code Simplified example
<?php
$emailto = "example#example.com";
$subject = "Example subject";
$name = Trim(stripslashes($_POST['name']));
$email = Trim(stripslashes($_POST['email']));
$message = Trim(stripslashes($_POST['message']));
$headers = "From: example#example.com\r\n";
$headers .= "Reply-To: example#example.com\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1" . "\r\n";
$body = "<p>You have received a new message:</p>
<p><strong>Ime: </strong> {$name} </p>
<p><strong>Email Address: </strong> {$email} </p>
<p><strong>Poruka: </strong> {$message} </p>";
$success = mail($emailto, $subject, $body, $headers);
if ($success){
print "<meta http-equiv=\"refresh\" content=\"0;URL=thanks.html\">";
}
else{
print "<meta http-equiv=\"refresh\" content=\"0;URL=error.html\">";
}
?>
For some reason variables $name, $email and $message appear empty when I receive an email from this contact form/mail function. The rest is shown correctly.
You also need to close the " in the $body variable.
$body = "<p>You have received a new message:</p>
<p><strong>Ime: </strong> {$name} </p>
<p><strong>Email Address: </strong> {$email} </p>
<p><strong>Poruka: </strong> {$message} </p>";
The problem was in my htaccess file and not in the code, I had some lines which were removing php and html extensions for estethics, so its all works fine now.
Try this.
$body = "<p>You have received a new message:</p>
<p><strong>Ime: </strong> ".$name." </p>
<p><strong>Email Address: </strong> ".$email." </p>
<p><strong>Poruka: </strong> ".$message." </p>";